Neo4j Cypher: How do you unpack nodes out of the way to provide further matching?

This question is dedicated to the next question here

I have a graph that has a circular linked list. ( see here for an example ). Each node in a linked list points to a user. When querying a list, I have to use the path operator since the list is round and I don't want to retrieve nodes starting with u: USER node. To get the nodes of interest, my query looks like this:

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

As soon as I get back to the path, I would like to do further matching with the nodes in this path (NODELINK), something 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 to 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 to unpack nodes of type NODELINK from a path to make further MATCH queries against them?

+4
3

... - , , .

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
+4

UNWIND, :

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

, WITH UNWIND nodes(path) AS nodeLinks, Conversion = ''' Neo4j 2.2.0-M03.

+2

Another option is to return the desired nodes as strings instead of the collection, and then perform further matching with the node strings. To return nodes as strings, first specify the nodes on the path, and then calculate the distance from node: NODELINK to user node, if the distance is greater than the distance from the initial 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

0
source

Source: https://habr.com/ru/post/1530121/


All Articles