如何避免在neo4j中多次访问具有相同属性的节点?

时间:2017-02-26 02:38:24

标签: neo4j cypher py2neo anormcypher

我的Cypher查询:

MATCH p =(o:Order)-[r:seeks*2..8]->(o:Order)
WHERE o.Name="000093" AND ALL(x IN tail(nodes(p)) WHERE SINGLE(y IN    tail(nodes(p)) WHERE x=y))
RETURN extract(n IN nodes(p)| n.Name) AS OrderID, extract(u IN nodes(p)| u.UserName) AS UserName,length(p), endNode(r[0])
ORDER BY length(p)

我想避免路径中具有相同属性值的节点,如何避免它们?

["000093","000090","000096","000097","000107","000091","000089","000093"]
["yunis","gio","Anhar","Jhon","**shakilbit**","xalima","**shakilbit**","yunis"]

所以,订单0000107和000089正在使用相同的用户名shakilbit,有什么办法可以避免在同一条路径上有这样的订单,谢谢! NEO4J ..据我所知,非常帮助社区。

1 个答案:

答案 0 :(得分:0)

使用APOC Procedures,您可能希望将集合作为一个集合(消除重复值)并比较大小。如果存在重复项,则集合的大小将更小。

MATCH p =(o:Order)-[r:seeks*2..8]->(o:Order)
WHERE o.Name="000093" AND ALL(x IN tail(nodes(p)) WHERE SINGLE(y IN    tail(nodes(p)) WHERE x=y))
WITH p, o, r, extract(u IN nodes(p)| u.UserName) AS UserName
// need to make some adjustments since first and last nodes are same
WHERE size(UserName) - 1 = size(apoc.coll.toSet(tail(UserName)))
RETURN extract(n IN nodes(p)| n.Name) AS OrderID, UserName, length(p), endNode(r[0])
ORDER BY length(p)

替代方法是重复你的ALL(x in tail...) WHERE single()...谓词,但是在UserName集合上(或者在现有的ALL()谓词中包含这个检查,尽管这可能很昂贵)。您可能想要查看每个,并查看哪个更高效。