lukasm
lukasm

Reputation: 11

Is it possible to achieve this type of filtering with Cypher

I have a DSL to query RedisGraph which looks like this:

App::Graph.query(movie: { title: 'Matrix' }, country: { value: 'Italy' , value: 'Spain', value: 'Greece' })

Which produces this cypher query:

MATCH (m:Movie)<-[:ACTED_IN]-(a:Actor)
WHERE m.title = 'Matrix'
WITH m, collect(a) as actors
WHERE 1 = 1
AND (movie)<-[:AVAILABLE_IN]-(:Country {value: 'Italy'})
AND (movie)<-[:AVAILABLE_IN]-(:Country {value: 'Spain'})
AND (movie)<-[:AVAILABLE_IN]-(:Country {value: 'Greece'})
RETURN actors, m

Question: in some cases a Country node, for example Spain, will have no edges, and as such currently the above cypher query would rightly return no results. But is it possible to rewrite the query above so that it would ignore the Country node if it has no edges, matching and returning nodes that are connected to the remaining initially defined list of countries i.e. Italy, Greece?

I've tried many option, below being the last one, but have not found a way to achieve this

MATCH (m:Movie)<-[:ACTED_IN]-(a:Actor)
WHERE m.title = 'Matrix'
WITH m, collect(a) as actors
MATCH (m)<-[:AVAILABLE_IN]-(connected_countries)
WHERE connected_countries.value IN ['Italy', 'Spain', 'Greece']
WITH m, actors, collect(connected_countries.value) as relevant_countries
WHERE 1 = 1
AND ((movie)<-[:AVAILABLE_IN]-(:Country {value: 'Italy'}) AND IN relevant_countries)
AND ((movie)<-[:AVAILABLE_IN]-(:Country {value: 'Spain'}) AND IN relevant_countries)
AND ((movie)<-[:AVAILABLE_IN]-(:Country {value: 'Greece'}) AND IN relevant_countries)
RETURN actors, m

Upvotes: 1

Views: 262

Answers (1)

SWilly22
SWilly22

Reputation: 879

Please try:

MATCH (m:Movie {title:'Matrix'})
WITH m AS m
UNWIND ['Italy', 'Spain', 'Greece'] AS country_name
MATCH (:Country {value: country_name})-[:AVAILABLE_IN]->(m)<-[:ACTED_IN]-(a:Actor)
WITH m, collect(a) as actors
RETURN actors, m

Upvotes: 1

Related Questions