How to filter movies by title and multiple genres, while aggregating genre counts in Elasticsearch?

Hi everyone!

I'm working with a movie index in Elasticsearch and need help building a query. Here’s the requirement:

  1. 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."

  2. 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!

Have a look at Global aggregation | Elasticsearch Guide [8.15] | Elastic

This might help you

I'm facing serious trouble building objects using Elastic.Clients.Elasticsearch, but I need to do it without using Fluent API.

Seems related to a specific language issue and not directly related to the initial question.

So may be open a new discussion for this?

I don't have the answer myself as I'm a Java developer.

dadoonet in java hahaah??

About the global setting, I believe it should work; now I want to see the performance with an index containing a lot of data.

As for the builder with objects, I found it very complex to implement with C#. I believe there was a strong effort to make it simple, but it didn’t turn out that way.