CodexBloom - Programming Q&A Platform

Elasticsearch 8.5 Fails to Aggregate Data from Parent-Child Relationships with Filters

๐Ÿ‘€ Views: 2080 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-14
elasticsearch aggregation parent-child filter JSON

I'm not sure how to approach Hey everyone, I'm running into an issue that's driving me crazy... I'm following best practices but I'm working with Elasticsearch 8.5 and have set up a parent-child relationship between my `products` index (parent) and `reviews` (child) documents... I am trying to perform an aggregation to count the number of reviews per product, while also filtering to include only those reviews that have a rating of 4 or higher. However, the results seem off, as the aggregation is returning counts that do not match the expected number of filtered reviews. Hereโ€™s a simplified version of my query: ```json { "query": { "match_all": {} }, "aggs": { "products_with_high_ratings": { "terms": { "field": "product_id" }, "aggs": { "filtered_reviews": { "filter": { "range": { "rating": { "gte": 4 } } }, "aggs": { "review_count": { "value_count": { "field": "review_id" } } } } } } } } ``` I expect to see counts that reflect only the reviews meeting my filter criteria, but instead, I'm getting counts that suggest all reviews are being included. Iโ€™ve tried using a `nested` query instead of a filter, but that also didnโ€™t yield the expected results. The mappings for my documents look like this: ```json { "mappings": { "properties": { "product_id": {"type": "keyword"}, "reviews": { "type": "nested", "properties": { "review_id": {"type": "keyword"}, "rating": {"type": "integer"} } } } } } ``` I also confirmed that there are indeed reviews with a rating of 4 or more in the index. Can anyone provide insight into why the aggregation results aren't reflecting the filtered criteria correctly? Any suggestions or alternative approaches would be greatly appreciated! The project is a microservice built with Json. Any advice would be much appreciated. My development environment is Debian. For context: I'm using Json on macOS.