Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher query to find nodes that have 3 relationships

Tags:

neo4j

cypher

I figured out how to write this query when I am looking for 2 relationships, but not sure how to add more relationships to the query.

Assume you have a book club database with 'reader' and 'book' as nodes. The 'book' nodes have a 'genre' attribute (to define that the book is a Fiction, Non-Fiction, Biography, Reference, etc.) There is a Relationship "HasRead" between 'reader' nodes and 'book' nodes where someone has read a particular book.

If I want to find readers that have read both Fiction AND Non-Fiction books, I could execute this Cypher query:

Start b1=node:MyBookIndex('Genre:Fiction'), 
      b2=node:MyBookIndex('Genre:Non-Fiction')
Match b1-[:HadRead]-r-[:HasRead]-b2
Return r.ReaderName

The key to the above query is the Match clause that has the two book aliases feeding into the r alias for the 'reader' nodes.

Question: How would I write the query to find users that have read Fiction AND Non-Fiction AND Reference books? I'm stuck with how you would write the Match clause when you have more than 2 things you are looking for.

like image 837
Dave Michener Avatar asked Dec 27 '22 04:12

Dave Michener


1 Answers

You can have multiple line specified in a single MATCH clause, separated by commas. For example, the following two MATCH clauses are semantically equivalent (and will be evaluated identically by the engine):

//these mean the same thing!
match a--b--c
match a--b, b--c

You can have any number of these matches. So, plugging that into your query, you get this:

start b1=node:MyBookIndex('Genre:Fiction'), 
      b2=node:MyBookIndex('Genre:Non-Fiction'),
      b3=node:MyBookIndex('Genre:Reference')
match b1-[:HasRead]-r,
      b2-[:HasRead]-r,
      b3-[:HasRead]-r
return r.ReaderName
like image 53
ean5533 Avatar answered Feb 08 '23 13:02

ean5533