【问题标题】:Searching for matches from all nodes to all nodes in Neo4j在 Neo4j 中搜索从所有节点到所有节点的匹配项
【发布时间】:2015-03-11 01:50:42
【问题描述】:

我有一组代表文件系统位置之间链接的节点。我可以加载所有节点,并尝试通过在每个节点中查找目标目录和源目录之间的匹配项来将它们链接在一起。

伪代码查询:

For all Interface nodes in Neo4j, 
search all other nodes for where other.sourcedir = this.destdir.  
If a match is found, create a SENDS_TO relationship from A to B.

天真的查询: MATCH (a:Interface), (b:Interface) WHERE a.destdir == b.sourcedir MERGE (a)-[r:SENDS_TO]->(b)

当我运行该查询时,Neo4j 似乎进入了一个无限循环,我在 10 分钟后终止了该循环。

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:

    你可以试试这个:

    MATCH (a:Interface)
    with a
    MATCH (b:Interface)
    WHERE a.destdir == b.sourcedir and a <> b
    MERGE (a)-[:SENDS_TO->(b);
    

    这可能会执行得更好,但您正在运行的查询会将每个接口与其他每个接口进行比较。所以对我来说似乎 O(n^2),如果你有 80 个无数节点,它会很慢。在运行此之前,您可能需要确保索引存在于 destdirsourcedir

    【讨论】:

    • 这是最终工作的脚本: MATCH (a:Interface) with a MATCH (b:Interface) WHERE a.destdir = b.sourcedir and a b MERGE (a)-[ :SENDS_TO]->(b)
    • 即使有 1400 个节点要搜索,它也几乎是瞬间完成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多