本文介绍了Neo4j - 按相关性排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Neo4j中按相关性订购返回的数据。

I'd like to order returned data by relevance in Neo4j.

为了我的目的,相关性可以简化为我要搜索的单词的索引 ,较低指数的相关性较高。

For my purpose, relevance can be simplified to "Index of the word I'm searching for", where the lower index the higher relevance.

示例

我有这三个节点:

node : {
    Label: PROD
    properties : {
        name: "Bearing replacement Skateboard"
    }
}

node : {
    Label: PROD
    properties : {
        name: "Skateboard"
    }
}

node : {
    Label: PROD
    properties : {
        name: "L7 Skateboard"
    }
}

我希望它们与此订单一起退货:

I want them to be returned with this order:

node : {
    Label: PROD
    properties : {
        name: "Skateboard" // name.indexOf("Skateboard") = 0
    }
}

node : {
    Label: PROD
    properties : {
        name: "L7 Skateboard"  // name.indexOf("Skateboard") = 3
    }
}

node : {
    Label: PROD
    properties : {
        name: "Bearing replacement Skateboard"  // name.indexOf("Skateboard") = 19
    }
}

到目前为止我所拥有的:

What I have so far:

String query = "MATCH (n:PROD) where LOWER(n.name) CONTAINS LOWER({textToSearch}) RETURN n ORDER BY LOWER(n.name) ASC LIMIT 15";

String textToSearch = "Skateboard";

Map<String, Object> queryParams = new HashMap<>();
queryParams.put("textToSearch", textToSearch);
try (
    Transaction ignored = database.beginTx();
    Result resultSet = database.execute(query, queryParams)
) {
    Iterator<Node> results = resultSet.columnAs("n");
    while (results.hasNext()) {
        Node node = results.next();
        /* data processing here */
    }
}

这只是通过名称上升来命令结果。有没有办法告诉neo4j基于 n.name.indexOf({textToFind})进行排序?

This just orders results by name ascendant. Is there a way to tell neo4j to sort based on n.name.indexOf({textToFind})?

推荐答案

如何在Cypher中做这样的事情

How about doing something like this in Cypher

MATCH (n:PROD)
WHERE n.name_lc CONTAINS toLower({textToSearch})
WITH n, SPLIT(n.name_lc, toLower({textToSearch})) as parts
RETURN n.name, SIZE(parts[0]) AS leading
ORDER BY leading

为了有效利用上述......

To make effective use of the above...

在属性的小写版本上创建索引

Create an index on a lowercase version of the property

CREATE INDEX ON :PROD(name_lc)

将常规名称复制为小写版本

Copy the regular name to a lowercase version

MATCH (n:PPOD)
SET n.name_lc = toLower(n.name)

这篇关于Neo4j - 按相关性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 08:15