0

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 !

Community
  • 1
  • 1
joy d
  • 408
  • 2
  • 13

1 Answers1

2

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.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • Wow ! That was great :). Thanks. BTW, can you please suggest me from where I can get in-depth knowledge of Cypher ? I am following their online tuts but not able to figure out everything.... – joy d Jun 05 '15 at 10:03
  • Hi Stefan, your query worked well. However if I need to put an OR between these two conditions then what should be the query ? I mean, I want to find list of persons who either acted in a movie or parent of some other person... – joy d Jun 10 '15 at 12:22
  • 1
    use the pipe for matching multiple reltype: `match (a)-[:TYPE1|:TYPE2]->(b)` – Stefan Armbruster Jun 10 '15 at 12:32