1
votes

I have a Neo4j graph which looks like this

enter image description here

It has a hierarchy of relations using the CHILD_OF relationship.
All the green nodes (see) are entitlements on Books.
All the books which a parent can see are also visible to the child but not the other way round.
Also, there is a possibility for child to see the books directly without involving the parent.

The ask here is to find the shortest path from the Child to the books.
In this case C1 has 2 paths (via entitlements) to Book1 but only 1 to Book2. And none to Book3.

enter image description here

The Neo4j Cypher query should return only the shortest paths from C1 to all the books.

I tried using the Neo4j shortestPath function but it does not work.

MATCH (c:Child {name:'C1'} ),
  (b:Book),
  p = shortestPath((c)-[*]-(b))
RETURN p

Output of this query returns this:

enter image description here

Desired final graph should look like this:

enter image description here

1
You put the expected result, a sample graph and detailed question; good job, Amitjose_bacoy

1 Answers

0
votes

You need to make the relationship to be directional from Child to Book. This is because when you put *, it will include a path from book3 to library which you don't need.

MATCH (c:Child {name:'C1'} ),
  (b:Book),
  p = shortestPath((c)-[*]->(b))
RETURN p