CodexBloom - Programming Q&A Platform

Elasticsearch 8.5 Missing Documents in Aggregation Results with Filtered Queries

👀 Views: 28 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-26
elasticsearch aggregation query json

I've been researching this but I'm getting frustrated with I'm having a hard time understanding I'm experiencing an scenario with Elasticsearch 8.5 where certain documents seem to be missing from the aggregation results when I use a filtered query. I'm executing a query to get the count of documents that match specific criteria, but the total count from the aggregation doesn't reflect the expected results. Here's the query I'm using: ```json { "query": { "bool": { "must": [ { "match": { "status": "active" } }, { "range": { "created_date": { "gte": "2023-01-01", "lte": "2023-12-31" } } } ] } }, "aggs": { "status_count": { "terms": { "field": "status.keyword" } } } } ``` This query is supposed to return the count of documents where the `status` is `active`, within the specified date range, and then aggregate by `status`. However, when I check the results, I'm getting an aggregation count of only 50 documents, despite knowing there are around 200 matching documents in the index. I've double-checked the index mapping, and the `status` field is set up as a keyword, so that should be fine. I also confirmed that documents meet the criteria with a simple match query, which returns the expected results: ```json { "query": { "bool": { "must": [ { "match": { "status": "active" } }, { "range": { "created_date": { "gte": "2023-01-01", "lte": "2023-12-31" } } } ] } } } ``` This confirms that those documents exist, yet they do not appear in the aggregation result. I also tried increasing the `size` parameter in the aggregation, but that didn't change the count. Is there something I'm overlooking in the aggregation setup, or could it be a potential bug in this version of Elasticsearch? Any help would be greatly appreciated! I appreciate any insights! For context: I'm using Json on Windows 11.