密码查询无限循环

时间:2019-02-28 03:12:34

标签: neo4j cypher

我在neo4j / cypher上遇到了一些麻烦。 例如,此查询:

MATCH
    (n71613:Concept)-[r0:similarTo]->(n5230:Concept),
    (n5230:Concept)-[r1:similarTo]->(n90576:Concept),
    (n5230:Concept)-[r2:similarTo]->(n121858:Concept),
    (n5230:Concept)-[r3:similarTo]->(n126486:Concept),
    (n126486)-[r:similarTo]->(child:Concept)
WHERE
    (child <> n90576
    AND child <> n121858
    AND child <> n126486
    AND child <> n71613
    AND child <> n5230)
AND
    (n90576 <> n121858
    AND n90576 <> n126486
    AND n90576 <> n71613
    AND n90576 <> n5230
    AND n121858 <> n126486
    AND n121858 <> n71613
    AND n121858 <> n5230
    AND n126486 <> n71613
    AND n126486 <> n5230
    AND n71613 <> n5230)
RETURN *
LIMIT 1

陷入了无止境的循环, 同时以这种方式运行它不会:

MATCH
    (n71613:Concept)-[r0:similarTo]->(n5230:Concept),
    (n5230:Concept)-[r1:similarTo]->(n90576:Concept),
    (n5230:Concept)-[r2:similarTo]->(n121858:Concept),
    (n5230:Concept)-[r3:similarTo]->(n126486:Concept),
    (n126486)-[r:similarTo]->(child)
WHERE
    ('Concept' IN labels(child))
AND
    (child <> n90576
    AND child <> n121858
    AND child <> n126486
    AND child <> n71613
    AND child <> n5230)
AND
    (n90576 <> n121858
    AND n90576 <> n126486
    AND n90576 <> n71613
    AND n90576 <> n5230
    AND n121858 <> n126486
    AND n121858 <> n71613
    AND n121858 <> n5230
    AND n126486 <> n71613
    AND n126486 <> n5230
    AND n71613 <> n5230)
RETURN *
LIMIT 1

我真的不知道这是怎么回事, 我发现这种解决方法非常幸运。

因此,一段时间之后,我又遇到了无限循环的问题。 几乎相同的查询,但模式不同:

MATCH
    (n0:Company)-[r0:produced]->(n1:Document),
    (n0:Company)-[r1:produced]->(n2:Document),
    (n0:Company)-[r2:produced]->(n3:Document),
    (n0:Company)-[r3:produced]->(n4:Document),
    (n0:Company)-[r4:produced]->(n5:Document),
    (n0)-[r:produced]->(child)
WHERE
    ((child <> n0) AND (child <> n1) AND (child <> n2) AND (child <> n3) AND (child <> n4) AND (child <> n5))
AND
    ((n0 <> n1) AND (n0 <> n2) AND (n0 <> n3) AND (n0 <> n4) AND (n0 <> n5) AND (n1 <> n2) AND (n1 <> n3) AND (n1 <> n4) AND (n1 <> n5) AND (n2 <> n3) AND (n2 <> n4) AND (n2 <> n5) AND (n3 <> n4) AND (n3 <> n5) AND (n4 <> n5))
AND
    ('Document' IN labels(child))
RETURN n0 AS n0_0, r0, n1 AS n1_0, n0 AS n0_1, r1, n2 AS n2_1, n0 AS n0_2, r2, n3 AS n3_2, n0 AS n0_3, r3, n4 AS n4_3, n0 AS n0_4, r4, n5 AS n5_4, n0 AS parent, r, child
LIMIT 1

导致另一个所谓的无穷循环, 我恰巧通过这种方式“克服了”:

MATCH
    (n0:Company)-[r0:produced]->(n1:Document),
    (n0:Company)-[r1:produced]->(n2:Document),
    (n0:Company)-[r2:produced]->(n3:Document),
    (n0:Company)-[r3:produced]->(n4:Document),
    (n0:Company)-[r4:produced]->(n5:Document)
WHERE 
    ((n0 <> n1) AND (n0 <> n2) AND (n0 <> n3) AND (n0 <> n4) AND (n0 <> n5) AND (n1 <> n2) AND (n1 <> n3) AND (n1 <> n4) AND (n1 <> n5) AND (n2 <> n3) AND (n2 <> n4) AND (n2 <> n5) AND (n3 <> n4) AND (n3 <> n5) AND (n4 <> n5))
OPTIONAL MATCH
    (n0)-[r:produced]->(child:Document)
WITH *
WHERE
    (r IS NOT NULL)
AND
    ((child <> n0) AND (child <> n1) AND (child <> n2) AND (child <> n3) AND (child <> n4) AND (child <> n5))
RETURN n0 AS n0_0, r0, n1 AS n1_0, n0 AS n0_1, r1, n2 AS n2_1, n0 AS n0_2, r2, n3 AS n3_2, n0 AS n0_3, r3, n4 AS n4_3, n0 AS n0_4, r4, n5 AS n5_4, n0 AS parent, r, child
LIMIT 1

但是此查询导致第一个模式(我发布的第一个查询)无休止的循环(没有,甚至没有“ labels()”技巧使之起作用)。

我只需要一种快速进行模式匹配的方法,而不必出于明显的原因而更改查询。

我不太了解这里发生了什么, 我希望你能有所帮助并帮我忙。

谢谢

1 个答案:

答案 0 :(得分:1)

您正在反复匹配相同的模式并创建笛卡尔乘积。

与其编写多个Match,不如使用一个。我不清楚您要达到什么目的,如果您可以分享详细信息,我可以建议您如何编写Query。

编辑: 试试这个:

MATCH (n:Company)-[r:produced]->(m:Document) 
WITH n, COLLECT(DISTINCT m) as ms
WHERE size(ms) = 6 
MATCH (n:Company)-[r:produced]->(m:Document) 
RETURN n, r, m 
LIMIT 1