首页 > 解决方案 > Neo4j 中的多级全文搜索

问题描述

当我在索引上调用 db.index.fulltext.queryNodes() 时,我可以对结果运行另一个全文查询吗?我需要在我的数据库中搜索跨不同标签的 7-8 个不同属性,每个属性具有不同的搜索参数。我该怎么做?如果我使用 reduce() 函数或 apoc.coll.intersection 并尝试获得一个交集,例如...

CALL db.index.fulltext.queryNodes("first_name", "manas~") 
YIELD node as first_names
WITH collect(first_names) as first_name_list
CALL db.index.fulltext.queryNodes("aliases", "boncha~") 
YIELD node as aliases
WITH collect(aliases) as alias_list,first_name_list
RETURN apoc.coll.intersection(first_name_list, alias_list) AS output
LIMIT 5

这不会导致内存膨胀吗?

标签: neo4jfulltext-index

解决方案


如果使用 Neo4j 4.1+,最好使用子查询:

CALL {
    CALL db.index.fulltext.queryNodes("first_name", "manas~") 
    YIELD node, score
    RETURN node, score

    UNION ALL
    CALL db.index.fulltext.queryNodes("aliases", "boncha~") 
    YIELD node, score
    RETURN node, score
}
RETURN node, sum(score) AS totalScore
ORDER BY totalScore DESC

对于交集,您可以计算每个节点它们有多少匹配,因此如果它们在两个查询中匹配,则计数为 2:

CALL {
    CALL db.index.fulltext.queryNodes("first_name", "manas~") 
    YIELD node, score
    RETURN node, score

    UNION ALL
    CALL db.index.fulltext.queryNodes("aliases", "boncha~") 
    YIELD node, score
    RETURN node, score
}
WITH node, count(*) AS matches, sum(score) AS totalScore
WITH node, matches, totalScore
WHERE matches = 2
ORDER BY totalScore DESC
RETURN node, totalScore

推荐阅读