简体   繁体   English

如何返回neo4j中具有一定关系的节点,然后返回与第一个节点具有不同关系的节点?

[英]How do I return nodes in neo4j that have a certain relationship, and then return nodes that have a different relationship with the first nodes?

I have a bunch of nodes that "refer" to other nodes. 我有一堆“引用”其他节点的节点。 The nodes referred to (refer_to being the relationship) may then have a relationship to another node called changed_to. 然后,所引用的节点(refer_to是该关系)可以与另一个节点changed_to有关系。 Those nodes that are related by the changed_to relationship may also have another changed_to relationship with yet another node. 与changed_to关系相关的那些节点也可能与另一个节点具有另一个changed_to关系。 I want to return the nodes referred to, but also the nodes that the referred nodes were changed into. 我想返回所引用的节点,还要返回所引用的节点被更改为的节点。 I tried a query that returns referred to nodes combined with a union with an optional match of ReferencedNode changed to ResultNode, but I don't think this will work as it will only get me the referenced node plus the first changed to node and nothing after that, assuming that would work at all to begin with. 我尝试了一个查询,该查询返回引用的节点并结合了一个联合,并带有一个将ReferencedNode的可选匹配项更改为ResultNode的联合,但是我认为这不会奏效,因为它只会让我获得引用的节点加上第一个更改为node的节点,之后便什么也没有首先,假设这完全可行。 How can I make a query with the described behavior? 如何用所描述的行为进行查询?

Edit: This is an example of the relationships going on. 编辑:这是正在进行的关系的示例。 I would like to return the referenced to node and the node that the referenced node was ultimately became, with some indicator showing that it became that node ultimately. 我想返回被引用的节点和被引用的节点最终成为的节点,并有一些指示符表明它最终成为了该节点。

在此处输入图片说明

Can you give some examples of queries you've tried? 您能否举一些您尝试过的查询示例? Here's what I'm thinking: 这就是我的想法:

MATCH path=(n)-[:refer_to]->(o)-[:changed_to*1..5]->(p)
WHERE n.id = {start_id}
RETURN nodes(path), rels(path)

Of course I don't know if you have an id property so that might need to change. 当然,我不知道您是否具有id属性,因此可能需要更改。 Also you'll be passing in a start_id parameter here. 另外,您start_id在此处传递start_id参数。

If you want to return the "references" node and the last "changed_to" node if there is one, you can first match the relationship you know is there, then optionally match at variable depth the path that may be there. 如果要返回“引用”节点和最后一个“ changed_to”节点(如果存在),则可以首先匹配已知的关系,然后以可变深度匹配可能存在的路径。 If there is more than one "changed_to" relationship you will have more than one result item at this point. 如果存在多个“ changed_to”关系,那么此时您将有多个结果项。 If you want all the "changed_to" nodes you can return now, but if you want only the last one you can order the result items by path depth descending with a limit of 1 to get the longest path and then return the last node in that path. 如果需要所有“ changed_to”节点,则可以立即返回,但是如果只希望最后一个节点,则可以按路径深度(限制为1)降序排列结果项以获取最长路径,然后返回该路径中的最后一个节点路径。 That query could look something like 该查询可能看起来像

MATCH (n)-[:REFERENCES]->(o)
WHERE n.uid = {uid}
OPTIONAL MATCH path=o-[:CHANGED_TO*1..5]->(p)
WITH n, o, path
ORDER BY length(path) DESC
LIMIT 1
RETURN n, o, nodes(path)[-1]

This returns the start node, the "references" node and 这将返回起始节点,“引用”节点和

  • nothing when there is no "changed_to" node 没有“ changed_to”节点时什么都没有
  • the one "changed_to" node when there is only one 只有一个时的一个“ changed_to”节点
  • the last "changed_to" node when there is more than one 当存在多个节点时,最后一个“ changed_to”节点

You can test the query in this console . 您可以在此控制台中测试查询。 It contains these three cases and you can test them by replacing {uid} above with the values 1 , 5 and 8 to get the starting nodes for the three paths. 它包含这三种情况下,你可以通过更换测试它们{uid}与值高于158 ,以获得为三个路径的起始节点。

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

相关问题 过滤与某个节点没有关系的Neo4j节点 - Filter Neo4j nodes that have no relationship with a certain node 查找没有特定关系的节点(Cypher / neo4j) - Finding nodes that do not have specific relationship (Cypher/neo4j) 如何返回与Neo4j具有多个关系的节点? - How do I return nodes with more than one relationship with Neo4j? 在 Neo4j 中如何返回特定节点或与密码的关系? - In neo4j how to return specific nodes or relationship with cypher? neo4j cypher:查找带有没有特定传出关系的末端节点的所有路径吗? - neo4j cypher: find all paths with end-nodes that do not have a certain outgoing relationship? Cypher / Neo4j:如何匹配与所有相关节点有关系的节点 - Cypher/Neo4j: How to match nodes that have relationship to all related nodes 如何设计与其他节点有序关系的neo4j db节点 - How to design neo4j db nodes that have ordered relationship with other nodes neo4j cypher - 如何查找与节点列表有关系的所有节点 - neo4j cypher - how to find all nodes that have a relationship to list of nodes Neo4j查询最短路径卡住(不工作)如果我在图形节点中有2路关系并且节点是相互关联的 - Neo4j query for shortest path stuck (Do not work) if I have 2way relationship in graph nodes and nodes are interrelated Neo4j:路径中节点之间的返回关系 - Neo4j : return relationship between nodes in a path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM