The pattern 'start node and last edge are known, find end node' could be stated in Cypher as
START a=node:nodeIndex({indexQueryParam})
MATCH a-[?*]->()-[:SHARES]->d
RETURN d
The ?
states that this part of the pattern is optional, and *
that it can be of any length. You may not need both, since variable length can mean a length of zero, but keep both in mind as you flesh out your pattern further. Since you don't really want the B
node, you don't have to bind it--empty parentheses is fine.
However, this pattern is very general, and depending on your data it can be expensive (it's a bit like fishing with dynamite). You give examples of other relationship types you might expect to find in the graph, KNOWS
and LIKES
. Anything like that which you can introduce to specify your pattern will make your query perform better.
MATCH a-[?:KNOWS|LIKES*]->()-[:SHARES]->d
or if you know in which order these will occur
MATCH a-[?:KNOWS]->()-[LIKES*]->()-[:SHARES]->d
here the KNOWS
part of the pattern is only one deep, while the LIKES
part is zero to infinity deep. The KNOWS
part is optional, which means that the entire part of the pattern linked to a
through this optional part, is also optional.
Finally, it is usually not a good idea to let the variable depth range between zero and infinity. Introduce an upper and or lower limit that makes sense for your data, like so
MATCH a-[?:KNOWS]->()-[LIKES*1..4]->()-[:SHARES]->d
You will have to look at the patterns that you find in (or impose on) your data, and develop your cypher query patterns accordingly, always specifying the cypher pattern as much as you can, leaving only those parts undefined which you want to be filled in by the graph.