简体   繁体   中英

Find node having two distinct relationship in Cypher

I am a newbie in Cypher. I am trying to find a query which will returns nodes having more than one relationship to other nodes. Please refer to http://neo4j.com/docs/snapshot/images/cypher-match-graph.svg for the structure. I am trying to find the person who has acted in some movie(s) and also a father. When I am running below query, it is giving me 0 records:

START n=node(*)
MATCH (n)-[r]->()
WHERE type(r)="ACTED_IN" AND type(r)="FATHER"
RETURN n,count(n); 

However, when I am running below query, it does return data but that I think is not what I wanted:

START n=node(*)
MATCH (n)-[r]->()
WHERE type(r)="ACTED_IN" OR type(r)="FATHER"
RETURN n,count(n);

So basically, I need a query which will fetch only Charlie Sheen since he acted in Wall Street movie and also father of Martin Sheen

I have tried to follow what others are saying in Cypher query to find nodes that have 3 relationships or How to retrieve the nodes with multiple relationships using cypher query or Cypher query to find nodes that have 3 relationships but not able to fathom the trick :(

Any help will be of much appreciation !

The right way to query this is:

MATCH (p:Person)-[:ACTED_IN]->(), (p)-[:FATHER]->()
RETURN p

You're looking for a :Person node having and ACTED_IN relationship and (since p is used again) having a :FATHER relationship. I'm using anonymous nodes at the other end, since we're not interested in the endpoint - just the fact that it is connected is interesting.

An alternative way to express that:

MATCH ()<-[:FATHER]-(p:Person)-[:ACTED_IN]->()
RETURN p

Note that you shouldn't use START (unless querying manual indexes) any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM