2

While querying the ne04j for the traversing order. it's traversing multiple times few nodes. I am attaching the cypher query to create nodes and relationships and results it returns. Any help is appreciated.

CREATE (a:Package {name:'A'})
CREATE (b:Package {name:'B'})
CREATE (c:Package {name:'C'})
CREATE (d:Package {name:'D'})
CREATE (e:Package {name:'E'})
CREATE (f:Package {name:'F'})
CREATE (g:Package {name:'G'})
CREATE (h:Package {name:'H'})
CREATE (i:Package {name:'I'})
CREATE (o:Package {name:'O'})
CREATE (z:Package {name:'Z'})
CREATE (j:SEG2 {name:'India'})
CREATE (k:SEG1 {name:'Natural Account'})
CREATE (l:SEG3 {name:'Engineering'})
CREATE (m:SEG4 {name:'Currency'})


create (a)-[:DEPENDS_ON]->(b)
create (a)-[:DEPENDS_ON]->(d)
create (d)-[:DEPENDS_ON]->(e)
create (b)-[:DEPENDS_ON]->(c)
create (b)-[:DEPENDS_ON]->(f)
create (b)-[:DEPENDS_ON]->(g)
create (h)-[:DEPENDS_ON]->(g)
create (i)-[:DEPENDS_ON]->(a)
create (z)-[:DEPENDS_ON]->(a)
create (i)-[:DEPENDS_ON]->(z)
create (o)-[:DEPENDS_ON]->(i)
create (j)-[:MAP_TO]->(a)
create (k)-[:MAP_TO]->(a)
create (l)-[:MAP_TO]->(a)
create (m)-[:MAP_TO]->(a)
return a,b,c,d,e,f,g,h,i,j,k,l,m,o,z

cypher query :

MATCH (s3:SEG3{name:'Engineering'})-[:MAP_TO]->(ANODE:Package{name:'A'}) 
WITH ANODE 
MATCH (s4:SEG4{name:'Currency'})-[:MAP_TO]->(ANODE)  
MATCH (GG)-[:DEPENDS_ON*0..]->(ANODE) 
RETURN collect(GG.name)

Result:

["A", "I", "O", "Z", "I", "O"]

I am expecting the

["A","Z", "I", "O"]
Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
user1844634
  • 1,221
  • 2
  • 17
  • 35

1 Answers1

1

This is because you can get to your result through multiple paths in your data. Based on the chat you need the order in which dependencies can be resolved. To get this order you can sort by maximum path length from root:

MATCH (s3:SEG3{name:'Engineering'})-[:MAP_TO]->(ANODE:Package{name:'A'}),
  (s4:SEG4{name:'Currency'})-[:MAP_TO]->(ANODE),
  p=(GG)-[:DEPENDS_ON*0..]->(ANODE) 
WITH GG.name as name,max(length(p)) as l
ORDER BY l
RETURN collect(name)

Also note that you don't have to repeat the MATCH clause for every part of the pattern.

František Hartman
  • 14,436
  • 2
  • 40
  • 60
  • Thanks for the response. Will my result set is according to the dependency. ? If you see my dependency relation Z->A ,I->A O->I,I->Z . It means Z needs to be processed before I. so result should be [A Z I O] – user1844634 Sep 27 '19 at 12:56
  • I tried your query the response is ["A", "I", "O", "Z"] But expected is ["A","Z", "I", "O"] – user1844634 Sep 27 '19 at 12:59
  • You need to describe the rule for that better, e.g why is it not `A I Z O`? Note that you have direct link from A to I in your data. – František Hartman Sep 27 '19 at 13:02
  • I agree A has direct link to I . As A->Z ,A->I but I->Z so Z needs to be processed. How to define the rule so that i get in correct order. ? – user1844634 Sep 27 '19 at 13:07
  • But what is the correct order? Please update your question with explanation for the order you want. – František Hartman Sep 27 '19 at 13:11
  • The correct order i want is ["A","Z", "I", "O"] that i mentioned in question already. – user1844634 Sep 27 '19 at 13:13
  • There are 2 paths for `(GG)-[:DEPENDS_ON*0..]->(ANODE) `: A,Z,I,O and A,I,O. You need to describe how you get the result you want from these 2 paths. Otherwise the solution may not work in other cases. – František Hartman Sep 27 '19 at 13:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200071/discussion-between-user1844634-and-frantisek-hartman). – user1844634 Sep 27 '19 at 13:20