首页 文章

返回不同的路径

提问于
浏览
0

我想从我的图中获得独特的模式,但如果节点在相同路径中的排序方式不同,neo4j认为这些路径是不同的 .

这是我想要找到的模式:

(a:Store)-[:SELLS]->(:Product)-[:SIMILAR]-(:Product)<-[:SELLS]-(b:Store)
                         |                     |
                    [:BUNDLED]            [:BUNDLED]
                         |                     |
(a:Store)-[:SELLS]->(:Product)-[:SIMILAR]-(:Product)<-[:SELLS]-(b:Store)

我试过这个查询:

match (a:Store)-[:SELLS]->(p1:Product)-[:BUNDLED]-(p2:Product)<-[:SELLS]-(a),
      (b:Store)-[:SELLS]->(p3:Product)-[:BUNDLED]-(p4:Product)<-[:SELLS]-(b),
      (p1)-[:SIMILAR]-(p3), (p2)-[:SIMILAR]-(p4)
return distinct apoc.coll.sortNodes(a + collect(distinct b),'name'), p1, p2, p3, p4

当我只想要一个时输出4个路径:

[[JojaMarket, PierreStore], apple, orange, banana, kiwi]
[[JojaMarket, PierreStore], orange, apple, kiwi, banana]
[[JojaMarket, PierreStore], banana, kiwi, apple, orange]
[[JojaMarket, PierreStore], kiwi, banana, orange, apple]

我怎样才能有效地要求neo4j返回独特的模式?

1 回答

  • 1

    对于这些以不同顺序返回值的对称匹配问题,有助于根据节点的id添加一些限制,这自然应该排除一些找到的路径 . 这也可以是在两个节点之间获取定义顺序的方法,因此您可以使用它来代替排序a和b .

    试试这个:

    MATCH (a:Store)-[:SELLS]->(p1:Product)-[:BUNDLED]-(p2:Product)<-[:SELLS]-(a),
          (b:Store)-[:SELLS]->(p3:Product)-[:BUNDLED]-(p4:Product)<-[:SELLS]-(b),
          (p1)-[:SIMILAR]-(p3), (p2)-[:SIMILAR]-(p4)
          WHERE id(a) < id(b) AND id(p1) < id(p2) 
    RETURN DISTINCT [a, b], p1, p2, p3, p4
    

相关问题