Extract subgraph from Neo4j graph with Cypher

送分小仙女□ 提交于 2020-07-07 11:42:09

问题


Suppose I have a collection of 5 nodes in Neo4j, such that each node in the collection is connected to at least one other node in the collection. I want to extract the subgraph formed by the collection of nodes and their interactions from Neo4j. Currently, I'm using a really primitive method that involves attempting to find a match from each node in the system to every other node:

MATCH p=(n)-[]->(m)
WHERE id(n) IN [3,4,5,6,7] AND id(m) IN [3,4,5,6,7]
RETURN relationships(p);

However, this query is both redundant and inefficient; it must go through every permutation of nodes in the collection (e.g. it will match node #3 and #4, and ALSO #4 and #3). Is there any more efficient way of getting the subgraph formed by these nodes using Cypher ONLY (no Java)?

Here's an example of what I mean by "subgraph": I want Cypher to return all the relationships marked in green. Please note that the nodes in the collection don't have to be isolated from the rest of the graph; they can still have relationships with other nodes in the graph, but I want Cypher to return only the interactions in green.

I want Cypher to return all the relationships marked in green. Please note that the nodes in the collection don't have to be isolated from the rest of the graph; they can still have relationships with other nodes in the graph, but I want Cypher to return only the relationships in green.


回答1:


I want to extend on Richards answer, you can also restrict it to nodes whose id's are in separate groups.

MATCH (n) WHERE id(n) IN [3,4,5,6,7]
MATCH p=(n)-->(m) 
WHERE id(n) < id(m) AND id(m) IN [3,4,5,6,7]
RETURN relationships(p);

results in

enter image description here




回答2:


This modification of your query helps with the redundancy of arrays.

WITH [3,4,5,6,7] AS arr
MATCH p=(n)-[]->(m)
WHERE id(n) IN arr AND id(m) IN arr
RETURN relationships(p);

You can use labels of nodes or types of relationships for differentiation (eventual subgraphs) and efficient querying but it depends on your case.



来源:https://stackoverflow.com/questions/29571966/extract-subgraph-from-neo4j-graph-with-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!