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

这不会导致内存膨胀吗?

解决方法

如果使用 Neo4j 4.1+,最好使用 subqueries :

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,sum(score) AS totalScore
ORDER BY totalScore DESC

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

CALL {
    CALL db.index.fulltext.queryNodes("first_name",score
}
WITH node,count(*) AS matches,sum(score) AS totalScore
WITH node,matches,totalScore
WHERE matches = 2
ORDER BY totalScore DESC
RETURN node,totalScore