Reputation: 217
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
Upvotes: 0
Views: 4298
Reputation: 8833
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);
Upvotes: 1
Reputation: 2996
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.
Upvotes: 1
Reputation: 3435
This should do it:
start n=node(*) match ()-[:Hadoop]-(n)-[:hive]-() return n,count(n)
Upvotes: 3
Reputation: 4211
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);
Upvotes: 2