how can i avoid visiting a node with same property more than once in neo4j?

197 views Asked by At

My Cypher query:

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)

i want to avoid having nodes with the same property values in the path, how to avoid them ?

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

so, Order 0000107 and 000089 is being placed by the same username shakilbit, is there any way i can avoid to have those kind of orders in the same path, Thanks! NEO4J.. Very Helping Community as far as i can tell.

1

There are 1 answers

14
InverseFalcon On BEST ANSWER

With APOC Procedures, you may want to get your collection as a set (where duplicate values are eliminated) and compare sizes. If duplicates are present, the size of the set will be smaller.

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)

The alternate is to do a repeat of your ALL(x in tail...) WHERE single()... predicate but on the UserName collection (or include this checking in your existing ALL() predicate, though this could be expensive). You may want to PROFILE each and see which is more performant.