Hi everyone!
I'm working with a movie index in Elasticsearch and need help building a query. Here’s the requirement:
-
I want to retrieve movies where the title contains a specific term, such as "car," and the genre matches one or more specific values, like "horror" and "thriller."
-
At the same time, I’d like to get an aggregation that groups and counts all genres that contain this term "car" in the title, without restricting the aggregation to only "horror" and "thriller."
Ideally, I’d like a single query that filters movies by both title and genre(s) while providing an aggregation that shows counts across all relevant genres, regardless of the specific genres I filtered in the search.
Just a reminder that I search by name and an aggregation. The standard aggregation should return with the filter; for example: if I search for "car" and "suspense," other aggregations not should be excluded.
The count must match. When filtering for "horror," where there are 5 movies, the total document count should be 5. When I also add the filter for "suspense," where there are 3 movies, the total count should be 8.
Is there an efficient way to do this in a single query, or would I need to use msearch with two separate queries?
Any guidance or suggestions would be greatly appreciated!
Thank you!