How to search in properties of a relationship which has an array of values?

Hi,
How do you search in the relationship properties if the relationship properties has an array of values?
For e.g.

  1. In the Movie database, the Person has Acted_IN Movies.
  2. A person plays multiple roles for e.g. Keanu Reeves ACTED_IN 'The Matrix'. The 'ACTED_IN' has properties called roles, and the values are for e.g. ['Neo', 'Reo', 'Leo'].

In this case, If I want to check in the database who has ACTED_IN role ='Reo', how do we express this?

In this case, the following query will not return any result:
match (p:Person)-[a:ACTED_IN]->(m:Movie{title:'The Matrix'}) where a.roles = 'Reo' return p.name, m.title, a.roles

But if we use the
match (p:Person)-[a:ACTED_IN]->(m:Movie{title:'The Matrix'}) where a.roles = ['Neo', 'Reo', 'Leo'] return p.name, m.title, a.roles

Then we get the result back.

I donot want to query using all roles, but only one of the many roles.

Thanks
Bijay

You can use the IN keyword to test whether an element is contained in a list:

match (p:Person)-[a:ACTED_IN]->(m:Movie{title:'The Matrix'}) 
where 'Reo' IN a.roles
return p.name, m.title, a.roles

Alternately you can use the any() list predicate to see if any of the elements in the list have that value:

match (p:Person)-[a:ACTED_IN]->(m:Movie{title:'The Matrix'}) 
where any(role in a.roles WHERE role = 'Reo')
return p.name, m.title, a.roles

And if you have a list (let's say it's a list parameter) of roles and you want to make sure all of those roles were played by the same person in a movie, you can use the all() list predicate instead:

match (p:Person)-[a:ACTED_IN]->(m:Movie{title:'The Matrix'}) 
where all(role in $roleList WHERE role IN a.roles)
return p.name, m.title, a.roles

Oh, and keep in mind none of these will leverage indexes (even if these are list properties on a node).

1 Like

Thank you! It worked.
I was trying to use UNWIND first and then search in the returned row, but that was not necessary as per your suggestion. Just for curiosity, could UNWIND be used for the relation properties and searched in the rowset returned? How would you do that?
Thank you once again!!

What about single?

where single(role in a.roles WHERE role = 'Reo')