Neo4j cypher to count and display all the relationship between two given nodes

僤鯓⒐⒋嵵緔 提交于 2020-01-23 07:57:08

问题


Here I am using neo4j rest api, in first step I want to collect information like how many relationships are there between two given nodes.

Sample : MATCH (n:Node {id: {parameter1}})-[r:someType]-(m:Node {id: {parameter2}}) RETURN COUNT(r)

Then I would like to collect all the values assigned to the edges so I can calculate further calculations. I need all the different types of relationships and their properties between two given nodes.

If it is possible I would like to do it in single cypher.


回答1:


Then I would like to collect all the values assigned to the edges

MATCH (n:Node {id: {parameter1}})-[r:someType]-(m:Node {id: {parameter2}})
RETURN COUNT(r) AS count, COLLECT(r) AS rels 

Note that the only thing I changed was adding collect(r) AS rels to the return, which gives you a collection of Relationship objects representing all edges with label someType between these nodes.

To get all edges of any type:

MATCH (n:Node {id: {parameter1}})-[r]-(m:Node {id: {parameter2}})
RETURN COUNT(r) AS count, collect(r) AS rels ORDER BY labels(r)

Remove the label requirement from the MATCH to return a collection of all relationships of any type. Order that collection by label, so that the list of relationships returned is sorted by type, making it easy for you to distinguish between them as needed for the purposes of your "further calculations"

This code is untested, and I'm not 100% sure you can call labels on a collection. If not, let me know and I'll provide an alternate solution.



来源:https://stackoverflow.com/questions/24105454/neo4j-cypher-to-count-and-display-all-the-relationship-between-two-given-nodes

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