gpt4 book ai didi

java - Neo4j - 按相关性排序

转载 作者:行者123 更新时间:2023-11-30 06:48:28 30 4
gpt4 key购买 nike

我想在 Neo4j 中按相关性对返回的数据进行排序。

就我而言,相关性可以简化为“我正在搜索的词的索引”,其中索引越低相关性越高。

示例

我有这三个节点:

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

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

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

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

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
}
}

我目前拥有的:

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}) 进行排序?

最佳答案

在 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

要有效利用上述...

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

CREATE INDEX ON :PROD(name_lc)

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

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

关于java - Neo4j - 按相关性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250810/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com