Cypher query secondary relationship where all nodes are linked

Hi Graphistas,
A "Mood" is a group of "Genre". I can get easily all "Movies" associated to a "Mood" but I strugle to do the opposite: how can I get the "Mood" of a "Movie"?

Note that the complexity lies in the fact that a Movie is associated to a Mood if all the Genre of the Mood are link to the Movie.
In the example image my Movie belongs to the "Fiction & Romance" Mood but not to the "Action & Thriller" Mood.

Thanks for your help!

If I understand your requirement, you would like to identify a movie's moods. By definition, a movie has a specific mood if the movie is connected to all of the genres that connect to the mood. If this is correct statement, then the following query will identify which potential moods a movie can have is a mood or not.

Basically, the query does the following:

  1. Finds the movie of interest
  2. Finds all moods connected to the movie
  3. Finds all genres connected to the movie
  4. Finds all genres connected to each mood
  5. Checks if all the genres of the mood are within the set of the movie genres. if true, the movie has that mood. if not, the movie does not.

match(m:Movie{title:'Batman'})
match(m)-->(g:Genre)-->(n:Mood)
with distinct n as moods, m
call {
with m, moods
match(m)-[:IN_GENRE]->(gm)
with m, collect(gm) as genreOfMovie, moods
match(gn)-[:IN_MOOD]->(moods)
return genreOfMovie, collect(gn) as genreOfMood
}
return m.title as movie, moods.name as mood, all(i in genreOfMood where i in genreOfMovie) as inMood

Here is my test data and results. You will need to alter the relationship directions if my test data has different directions than your data model.

Screen Shot 2022-01-11 at 7.41.02 PM

1 Like

Thanks @glilienfield for the query and clear explanations, it worked like a charm!

Note for other people information.
*I ask on discord to @hakan.lofqvist1 whether it can be relevant to create a relation between Mood and Movie : *
"Yes and no. Yes = if you run gds algo or some other heavy query to figure out the best mood. No = if it can be a fast local query."
=> In our use case it's preferable to use a cypher query for now.

Also he mentionned as an interesting solution to organize your Moods into a hierarchy (for instance by running similarity overlap on a (:Movie)-->(:Genre) graph). Because then you can get Movie -> Fiction & Romance <-[:CHILD_TO]- Romance.

Thank you both