Neo4J: Query to return nodes grouped by relation attributes?

左心房为你撑大大i 提交于 2019-12-11 10:51:06

问题


In my case, nodes are connected with one or more relations of type similar. This relation has two attributes fldName and value. For example:

(x)-[r:similar {fldName:'e-mail', value: 'name@x.y.com' }]->(y)
(x)-[r:similar {fldName:'phone', value: '123-45-67' }]->(y)
(q)-[r:similar {fldName:'e-mail', value: 'other@x.y.com' }]->(p)
(q)-[r:similar {fldName:'phone', value: '891-23-45' }]->(p)
(s)-[r:similar {fldName:'e-mail', value: 'neo@x.y.com' }]->(t)

Questions:

1) What is the query to return the following separate groups of results :

group 1: x,y; number of relations: 2
group 2: q,p; number of relations: 2
group 3: s,t; number of relations: 1

2) Is it possible to write this query without a priori knowledge the values of relation attributes?


回答1:


For what you want to achieve, I suggest you use Labels. You could populate the database as follows:

merge (:x)-[:similar {fldName:'e-mail', value: 'name@x.y.com' }]->(:y)
merge (:x)-[:similar {fldName:'phone', value: '123-45-67' }]->(:y)
merge (:q)-[:similar {fldName:'e-mail', value: 'other@x.y.com' }]->(:p)
merge (:q)-[:similar {fldName:'phone', value: '891-23-45' }]->(:p)
merge (:s)-[:similar {fldName:'e-mail', value: 'neo@x.y.com' }]->(:t)

Here you would create nodes with Labels x, y, q, p, s and t.

You can group the results with the following query:

MATCH (x)-[r:similar]->(y)
RETURN labels(x), labels(y), count(r) as relCount

See the Neo4j console for the result.



来源:https://stackoverflow.com/questions/23205781/neo4j-query-to-return-nodes-grouped-by-relation-attributes

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