# Elasticsearch Aggregations Return Unexpected Counts When Filtering Website Search Results

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregations-return-unexpected-counts-when-filtering-website-search-results/391697>\
**Category:** Elasticsearch\
**Created:** [November 18, 2026, 3:52pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-return-unexpected-counts-when-filtering-website-search-results/391697 "2026-11-18T15:52:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeroot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joeroot/32/132925_2.png) [@joeroot](https://discuss.elastic.co/u/joeroot)\
**Post date:** [November 18, 2026, 3:52pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregations-return-unexpected-counts-when-filtering-website-search-results/391697/1 "2026-11-18T15:52:02Z")

</div>

I am developing a website that uses Elasticsearch to provide visitors with searchable content and category-based filters. The search functionality itself is working reasonably well, and users can enter keywords to find relevant records, but I am facing a problem with the aggregation counts displayed alongside the search results. My website uses Elasticsearch aggregations to show how many matching records belong to each category, content type, or other available filter option. These counts are important because visitors use them to understand how much content is available under each option before narrowing their search. However, I have noticed that the numbers returned by Elasticsearch do not always correspond to the actual records displayed in the results. Sometimes a category shows a count that is higher than the number of matching records, while other times an option displays zero even though I can find relevant documents when searching for that category directly.

The issue becomes more noticeable when visitors combine multiple filters with a keyword search. For example, a user might search for a particular term and then select a category, after which the website updates the results and the available filter counts. The returned documents generally appear to follow the selected filters, but the aggregation section sometimes continues showing counts that seem to represent a broader collection of documents rather than the currently filtered results. In other situations, selecting one filter changes the counts of unrelated categories in ways I cannot explain. I initially thought this might be caused by the frontend displaying an outdated response, but after inspecting the responses returned by Elasticsearch, I found that the unexpected counts can already be present in the aggregation data before it reaches the frontend. This makes me think the problem is more likely related to the query structure or the way aggregations interact with filters.

I have been reviewing the Elasticsearch queries generated by my application and noticed that they contain a combination of keyword matching, Boolean conditions, and aggregation definitions. The main search conditions are placed inside the query section, while the category filters are used to narrow down the results. I am now trying to understand whether I have structured the aggregation queries correctly or whether some of the filters are not being applied to aggregations in the way I expect. I have read about filter aggregations, global aggregations, and the `post_filter` option, but I am still uncertain about the differences between them when building a search interface with multiple selectable filters. In particular, I want the main results to respect all selected filters while the aggregation counts follow the intended filtering behavior, rather than accidentally counting documents from a wider set.

Another area I am investigating is the structure and mapping of the fields used for aggregations. Some of the fields contain category names or content types, and I understand that Elasticsearch generally requires keyword-type fields for terms aggregations rather than analyzed text fields. I have checked parts of my mapping, but I am not completely confident that every field used by the aggregation has the correct data type. I am also unsure whether nested documents or multi-valued fields could affect the counts in my particular query structure. I do not want to change the mapping or rebuild the index without first understanding whether the problem is caused by field configuration or by the way the aggregation is written. It would be helpful to know which parts of the mapping and query response I should inspect to distinguish between these possibilities.

For troubleshooting, I have tried simplifying the search request by removing some of the filters and running individual aggregations against the same index. The results are sometimes more consistent with simpler queries, but the unexpected counts return when I restore the complete query used by the website. I have also compared the aggregation buckets with the documents returned by direct searches, although I am not sure whether I am comparing equivalent result sets in every case. I would like to create a reliable test query that demonstrates how Elasticsearch calculates aggregation counts when several filters are active at the same time. If there are recommended approaches for debugging complex Boolean queries, inspecting aggregation scope, or validating bucket counts against matching documents, I would appreciate some practical examples that I can apply to my existing implementation.

Has anyone encountered a similar problem where Elasticsearch returns search results that look correct but produces unexpected aggregation counts for a website's filter interface? I would appreciate guidance on how to structure terms aggregations when multiple filters are involved, when to use filter aggregations instead of `post_filter`, and how to ensure that each aggregation counts the intended documents. I am particularly interested in understanding whether this behavior is usually caused by query scope, field mappings, nested data, or an incorrect combination of filtering conditions. My goal is to make the category and filter counts accurately represent the relevant search results so visitors can narrow down content without seeing misleading numbers. Any practical advice or example query structures would be greatly appreciated. Sorry for long post!
