Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Cypher: How do you unpack nodes from a path to allow for further matching?

Tags:

neo4j

cypher

This question is a follow on to the question here

I have a graph that has a circular linked list. (see here for an example) Each node in the linked list points to a User. When querying the list I have to use a path statement as the list is circular and I do not want to retrieve nodes beginning from the u:USER node. In order to get the nodes of interest my query looks like this:

MATCH path=(nl:NODELINK { linkId:'cc' })-[:LINK*]->(u:USER)
RETURN nodes(path)

Once I have retrieved the path I would like to do further matching against the nodes in that path (the NODELINK's), somthing like the following:

MATCH path=(nl:NODELINK { linkId:'cc' })-[:LINK*]->(u:USER)
WITH nodes(path) AS nodeLinks
MATCH nodeLinks-[:PERSONLINK]->persons
RETURN persons

but if I try I get an error:

Error: Type mismatch: nodeLinks already defined with conflicting type Collection<Node> (expected Node) (line 3, column 7)
"MATCH nodeLinks-[:PERSONLINK]->persons"

How do I unpack the nodes of type NODELINK from the path in order to do further MATCH queries against them?

like image 928
Aran Mulholland Avatar asked Mar 04 '14 22:03

Aran Mulholland


2 Answers

Try this one... kind of hacky but until there's an unwind operation, it will work.

MATCH path=(nl:NODELINK { linkId:'cc' })-[:LINK*]->(u:USER)
WITH [x in nodes(path) | id(x)] AS nodeLinkIds
MATCH (n1:NODELINK)
WHERE id(n1) in nodeLinkIds // this does efficient id lookups for the nodes in the list
MATCH n1-[:PERSONLINK]->persons
RETURN persons
like image 63
Eve Freeman Avatar answered Oct 03 '22 21:10

Eve Freeman


There's now an UNWIND operator, so this should work:

MATCH path=(nl:NODELINK { linkId:'cc' })-[:LINK*]->(u:USER)
WITH nodes(path) AS x UNWIND x AS nodeLinks
MATCH nodeLinks-[:PERSONLINK]->persons
RETURN persons

I'd have thought we could do WITH UNWIND nodes(path) AS nodeLinks, but that gives me a cryptic Conversion = ''' error in Neo4j 2.2.0-M03.

like image 32
Ken Williams Avatar answered Oct 03 '22 22:10

Ken Williams