9

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?

1
  • Can you provide a more concrete example? What kind of question are you trying to answer on your data? I think the answer is that you may need to model your data differently to reflect the answer to the question you are attempting to ask. Mar 5, 2014 at 0:08

3 Answers 3

5

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
1
  • Any plans you know of to provide an unwind operation? Mar 5, 2014 at 0:22
4

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.

1
  • that looks nicer, i'll check it out Feb 8, 2015 at 10:43
0

Another option is to return the desired nodes as rows instead of a collection, and then do the further match with the rows of node. To return the nodes as rows, first specifies the nodes on the path and then compute the distance from a node:NODELINK to the node User, if the distance is longer than the distance from the starting node (e.g.'cc') to the end user node, then it should be excluded from the result.

MATCH path=(:NODELINK { linkId:'cc' })-[:LINK*]->(:USER)
WITH length(path) AS longest
MATCH p=(:NODELINK { linkId:'cc' })-[:LINK*]->(nodeLinks:NODELINK), p1 =(nodeLinks)-[:LINK*]->(:USER)
WITH nodeLinks, length(p1) AS n2u, longest
WHERE n2u <= longest
WITH nodeLinks
MATCH nodeLinks-[:PERSONLINK]->persons
RETURN persons

See the result from the console, http://neo4j-console-20.herokuapp.com/?id=87v0fy

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.