37

I want to match between entities by multiple relationship types.

Is it possible to say the following query:

match (Yoav:Person{name:"Yoav"})-[:liked & watched & ... ]->(movie:Movie) return movie

I need "and" between all the relation types; Yova liked and watched and .. a movie.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

1 Answers1

59

Yes, you can do something like:

match (gal:Person{name:"Yoav"})-[:liked|:watched|:other]->(movie:Movie) 
return movie

Take a look in the docs: Match on multiple relationship types

EDIT:

From the comments:

I need "and" between the relation types.. you gave me an "or"

In this case, you can do:

match (Yoav:Person{name:"Yoav"})-[:liked]->(movie:Movie),
(Yoav)-[:watched]->(movie),
(Yoav)-[:other]->(movie)
return movie
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • :liked|:watched|:other == liked and watched and other, :liked|watched|other == liked or watched or other ? – Stav Alfi Sep 09 '17 at 16:09
  • liked|watched|other == liked or watched or other – Bruno Peres Sep 09 '17 at 16:10
  • 1
    I need "and" between the relation types.. you gave me an "or" – Stav Alfi Sep 09 '17 at 16:11
  • I understand. In this case I believe has no way to do it using only one relationship in the pattern matching. – Bruno Peres Sep 09 '17 at 16:31
  • then How would you implement it? – Stav Alfi Sep 09 '17 at 16:32
  • 1
    You can use: `match (Yoav:Person{name:"Yoav"})-[:liked]->(movie:Movie), (Yoav)-[:watched]->(movie), (Yoav)-[:other]->(movie) return movie ` – Bruno Peres Sep 09 '17 at 16:35
  • Please update your answer so I can accept it. Thanks! – Stav Alfi Sep 09 '17 at 16:35
  • Neo4j Desktop gives a warning that this ability of using colons for multiple relationship types is slotted to be deprecated and a new method to be used soon. I'm not sure how the new syntax is going to look like. – Michael Black Jun 25 '18 at 19:46
  • 1
    @MichaelBlack according to [link](https://stackoverflow.com/questions/45790779/alternative-relationship-deprecated-warning) > i.e. not :R1|:R2 but :R1|R2. – vldbnc Sep 28 '18 at 07:49
  • In the newer versions of neo4j, for the OR expression, you have to lose the colons: ```match (gal:Person{name:"Yoav"})-[:liked|watched|other]->(movie:Movie) return movie``` – Chris Nov 11 '21 at 17:04