0

I've been playing around with Neo4j and have a problem for which I do not have a solution, hence my question here.

For my particular problem I'll describe a simplified version that captures the essence. Suppose I have a graph of locations that are connected either directly or via a detour:

  • direct: (A)-[:GOES_TO]->(B)
  • indirect: (A)->[:GOES_THROUGH]->(C)-[:COMES_BACK_TO]->(B)

If I want to have everything between "Go" and the "Finish" with a GOES_TO relationship I can easily use the Cypher query:

START a=node:NODE_IDX(Id = "Go"), b=node:NODE_IDX(Id = "Finish)
MATCH a-[r:GOES_TO*]->b
RETURN a,r,b

Here, NODE_IDX is an index on the nodes (Id).

Where I get stuck is when I want to have all the paths between "Go" and "Finish" that are not GOES_TO relationships but rather multiple GOES_THROUGH-->()-->COMES_BACK_TO relationship combinations (of variable depth).

I do not want to filter out the GOES_TO relationships because there are many more relationships among the nodes, and I do not want to accommodate removing all of them (dynamically). Is it possible to have a variable-depth, multi-relationship MATCH that I envisage?

Thanks!

devnull
  • 65
  • 5
  • is this not quite similar? http://stackoverflow.com/questions/14104682/cypher-query-finding-all-paths-between-two-nodes-filtered-by-relationship-prope – Michal Bachman Aug 13 '13 at 08:04
  • No, not really. What I'm looking for is something like `(a)-[([:GOES_THROUH]->()-[:COMES_BACK_TO])*]->(b)`, i.e. I want a repetition of not one relationship, which would be `[:RELATIONSHIP*]` but a combination of several in a particular pattern. – devnull Aug 13 '13 at 17:22
  • I just noticed this group (https://groups.google.com/forum/#!topic/neo4j/MIb2uCcJy1I) where the same question is asked. It seems there is currently no support for this type of query in Cypher. Too bad! – devnull Aug 16 '13 at 17:38
  • right, thanks for that link, I finally see what you wanted to do :-) I guess a custom plugin is your only option (+ core Java API). What language do you use with Neo? – Michal Bachman Aug 18 '13 at 09:55
  • I use the Neo4jClient for C# and of course the Neo4j web admin for Cypher queries. I guess I have to invest a bit of time to learn Java and the Neo4j Java API. – devnull Aug 18 '13 at 11:12

1 Answers1

1

Let me restate what I believe is being asked.

"If there is a path of the form (a)-[:X]->(b), find all other paths from a to b."

The answer is simple:

MATCH p=(a)-[:X]->(b), q=(a)-[r*]->(b) 
WHERE p<>q 
RETURN r;
cybersam
  • 63,203
  • 6
  • 53
  • 76