How to retrieve the nodes with multiple relationships using cypher query

孤人 提交于 2019-12-25 03:38:15

问题


Can some one please help me on this.

I am using cypher query to get the nodes that are having relationship as either 'hive' or 'hadoop' and i am able to get the nodes.

but when i try to get the nodes that are having relationship as both 'hive' and 'hadoop' i am not able to get the nodes.

This is the query i am using

start n=node(*) match (n)-[r]-() where type(r)="Hadoop" and type(r)="hive" return n,count(n);

This query returns 0 rows.

Is my query wrong or do i need to do it the other way.

Thanks in advance.

Gouse


回答1:


This should do it:

start n=node(*) match ()-[:Hadoop]-(n)-[:hive]-() return n,count(n)



回答2:


I think your query should be:

start n=node(*) match (n)-[r1]-(),(n)-[r2]-() where type(r1)="Hadoop" and type(r2)="hive" return n,count(n);




回答3:


You can achieve it like this

match (n)-[r:Hadoop|hive]-()
return n,count(n);

It will provide you what you are expecting. It is better to conditions in match rather to have it in where clause.




回答4:


First, the reason your query gets no results is because the part where type(r)="Hadoop" and type(r)="hive" says you are looking for an instance of r where r.type = "Hadoop" and "hive" simultaneously. Since r.type can only have one value at any time, it is impossible for it to equal Hadoop and Hive at the same time; so the statement can be logically simplified to "where false" or basically, drop all matches.

If you are looking for all nodes that have either relationship, than Satish Shinde's answer is the right way to specify it

match (n)-[:Hadoop|hive]-()
return n,count(n);

Or, with direction

match (n)-[:Hadoop|hive]->()
return n,count(n);

If you need BOTH to be present, than you need to match two separate relationship edges like follows

match ()-[:hive]-(n)-[:Hadoop]-()
return n,count(n);

Or, with direction

match ()<-[:hive]-(n)-[:Hadoop]->()
return n,count(n);

And for completeness, if you want to check both exist in a where close, you can use remigio's answer

start n=node(*) match ()-[r2]-(n)-[r1]-() where type(r1)="Hadoop" and type(r2)="hive" return n,count(n);


来源:https://stackoverflow.com/questions/15159100/how-to-retrieve-the-nodes-with-multiple-relationships-using-cypher-query

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