Return 不同的路径
Return distinct paths
我想从我的图表中获得独特的模式,但如果节点在相同路径中的排序不同,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 return 独特的模式?
对于这种对称匹配返回值顺序不同的问题,根据节点的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
我想从我的图表中获得独特的模式,但如果节点在相同路径中的排序不同,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 return 独特的模式?
对于这种对称匹配返回值顺序不同的问题,根据节点的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