简体   繁体   English

Neo4j Cypher:如何从路径中解压缩节点以允许进一步匹配?

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

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. 查询列表时,我必须使用路径语句,因为列表是循环的,我不想从u:USER节点开始检索节点。 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: 一旦我检索了路径,我想进一步匹配该路径中的节点(NODELINK的),如下所示:

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? 如何从路径中解压缩NODELINK类型的节点,以便对它们进行进一步的MATCH查询?

Try this one... kind of hacky but until there's an unwind operation, it will work. 尝试这个...有点hacky但是直到有一个展开操作,它才会起作用。

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

There's now an UNWIND operator, so this should work: 现在有一个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

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. WITH UNWIND nodes(path) AS nodeLinks以为我们可以用WITH UNWIND nodes(path) AS nodeLinks ,但这让我在Neo4j 2.2.0-M03中出现了一个神秘的Conversion = '''错误。

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 (eg'cc') to the end user node, then it should be excluded from the result. 要将节点作为行返回,首先指定路径上的节点,然后计算距节点的距离:NODELINK到节点User,如果距离长于从起始节点(例如'cc')到结束的距离用户节点,然后应从结果中排除。

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 请参阅控制台的结果, http://neo4j-console-20.herokuapp.com/?id = 87v0fy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM