-1

I have a graph which looks like this:

enter image description here

Here is the link to the graph in the neo4j console: http://console.neo4j.org/?id=av3001

Basically, you have two branching paths, of variable length. I want to match the two paths between orange node and yellow nodes. I want to return one row of data for each path, including all traversed nodes. I also want to be able to include different WHERE clauses on different intermediate nodes.

At the end, i need to have a table of data, like this:

  • a - b - c - d
  • neo - morpheus - null - leo
  • neo - morpheus - trinity - cypher

How could i do that? I have tried using OPTIONAL MATCH, but i can't get the two rows separately.

I have tried using variable length path, which returns the two paths but doesn't allow me to access and filter intermediate nodes. Plus it returns a list, and not a table of data.

I've seen this question: Cypher - matching two different possible paths and return both

It's on the same subject but the example is very complex, a more generic solution to this simpler problem is what i'm looking for.

3 Answers 3

2

You can define what your end node by using WHERE statement. So in your case end node has no outgoing relationship. Not sure why you expect a null on return as you said neo - morpheus - null - leo

MATCH p=(n:Person{name:"Neo"})-[*]->(end) where not (end)-->()
RETURN extract(x IN nodes(p) | x.name)

Edit:

may not the the best option as I am not sure how to do this programmatically. If I use UNWIND I get back only one row. So this is a dummy solution

MATCH p=(n{name:"Neo"})-[*]->(end) where not (end)-->() 
with nodes(p) as list 
return list[0].name,list[1].name,list[2].name,list[3].name
1
  • I need to get rows of data at the end (in table format), not a list. Is there any way to do that? Jul 25, 2017 at 19:21
1

You can use Cypher to match a path like this MATCH p=(:a)-[*]->(:d) RETURN p, and p will be a list of nodes/relationships in the path in the order it was traversed. You can apply WHERE to filter the path just like with node matching, and apply any list functions you need to it.

I will add these examples too

// Where on path
MATCH p=(:a)-[*]-(:d) WHERE NONE(n in NODES(p) WHERE n.name="Trinity") WITH NODES(p) as p RETURN p[0], p[1], p[2], p[3]

// Spit path into columns
MATCH p=(:a)-[*]-(:d) WITH NODES(p) as p RETURN p[0], p[1], p[2], p[3]

// Match path, filter on label
MATCH p=(:a)-[*]-(:d) WITH NODES(p) as p RETURN FILTER(n in p WHERE "a" in LABELS(n)) as a, FILTER(n in p WHERE "b" in LABELS(n)) as b, FILTER(n in p WHERE "c" in LABELS(n)) as c, FILTER(n in p WHERE "d" in LABELS(n)) as d

Unfortunately, you HAVE to explicitly set some logic for each column. You can't make dynamic columns (that I know of). In your table example, what is the rule for which column gets 'null'? In the last example, I set each column to be the set of nodes of a label.

2
  • I tried using path, but I want to apply individual filter to different intermediate nodes in the path. I also need to have rows of data at the end, not a list. I am updating my question to make that clearer. Jul 25, 2017 at 19:17
  • @FlorentChatterji I added some more examples of using paths. I'm not sure what WHERE logic you want to use. The last example is the closest to what you asked. If you need dynamically generated columns, you will probably have to settle for one column with a list of nodes instead.
    – Tezra
    Jul 25, 2017 at 19:33
0

I.m.o. you're asking for extensive post-processing of the results of a simply query (give me all the paths starting from Neo). I say this because :

  1. You state you need to be able to specify specific WHERE clauses for each path (but you don't specify which clauses for which path ... indicating this might be a dynamic thing ?)
  2. You don't know the size of the longest path beforehand ... but you still want the result to be a same-size-for-all-results table. And would any null columns then always be just before the end node ? Why (for that makes no real sense other then convenience) ?
  3. ...

Therefore (and again i.m.o.) you need to process the results in a (Java or whatever you prefer) program. There you'll have full control over the resultset and be able to slice and dice as you wish. Cypher (exactly like SQL in fact) can only do so much and it seems that you're going beyond that.

Hope this helps,

Regards, Tom

P.S. This may seem like an easy opt-out, but look at how simple your query is as compared to the constructs that have to be wrought trying to answer your logic. So ... separate the concerns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.