Strange behavior in neo4j when I try to get all the nodes which relate with a master node

时间秒杀一切 提交于 2019-12-11 19:58:46

问题


I am trying to write a query which will give me all the nodes which are connected with a "master" node. Lets say that the node connect like this:

      A -> B
      B -> C
      C -> D
      C -> E
      B -> F

What I want initially is to write a query which will return to me all the node's names under the master node A. All the nodes are related with a "contain" relationship.

I wrote this query:

MATCH (n {gid:'58749'})-[:contains*]-(z) RETURN z as names

When I run this query in the neo4jServer I get a nice graph, which shows indeed all the nodes which are related. BUT when I check the "table" vizualisation of the date (not the graph) I get much more results. As you can see in the pictures in the first one on the button it says: displaying 8 nodes 9 relationships but in the second one (for the same think) it says: "returned 48 rows). So, the question is: what the heck is going on??

EDITED

Changing the query to this:

      MATCH (n {gid:'58749'})-[:contains*]-(z) RETURN distinct(z) as names

solves partially the problem. Nodes with the same name (id) will be aggregated.


回答1:


Keep in mind that what's returned in the graph is a representation of the total result set; this includes all sub-graphs/sub-paths that comprise the result set. In the tabular representation, you're going to get all sub-paths, etc. the comprise the result set.

E.g. A->B->C->D is in the graph, but in the table, you're going to see, A->B, A->B->C, A->B->C->D, etc. This is because of the Cypher statement you have which is looking for all nodes that have a "contains" relationship of ANY depth (also, the query looks for such relationships in either direction).

So, while you may only have 8 nodes and 9 relationships in total, the Cypher pattern you're matching is going to be a combination of those 8 nodes and 9 relationships.



来源:https://stackoverflow.com/questions/27966939/strange-behavior-in-neo4j-when-i-try-to-get-all-the-nodes-which-relate-with-a-ma

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