Does order of aggregation in ElasticSearch query affect final search result - Stack Overflow

I have an ElasticSearch query like below, where I try to take the aggregations, filter with bucket_filt

I have an ElasticSearch query like below, where I try to take the aggregations, filter with bucket_filter then do pagination with bucket_paging. If I execute this exact query, I get the correct output as expected. But if I switch the order of bucket_filter and bucket_paging in query, it returns less documents than expected. As guess that, with the later case, ElasticSearch executes the bucket_paging paging first (that return max 50 items), then applies the bucket_filter filter, that in turn filters out a few more items from previous 50 items. Is my assumption correct? And if yes, is there any way to customize the query to get the expected result without being affected by mentioned aggregations' order in query.

My problem is, I'm using elasticsearch-java client library to build the query, which put aggregations into a map instead of a list, as a result the order of aggregations are random in the final built query.

Note: I'm on ElasticSearch v8.7

{
"aggregations": {
    "by_planning_sum_id": {
        "aggregations": {
            "bad_count": {
                "filter": {
                    "bool": {
                        "must": [{ "term": { "review_score_class": { "value": "bad" } } }]
                    }
                }
            },
            "country_data": {
                "aggregations": {
                    "avg_score": { "avg": { "field": "review_score" } }
                },
                "filter": {
                    "bool": {
                        "must": [{ "term": { "region_code": { "value": "JP" } } }]
                    }
                }
            },
            "bad_ratio": {
                "bucket_script": {
                    "buckets_path": { "all": "_count", "bad": "bad_count>_count" },
                    "script": { "source": "params.bad/params.all" }
                }
            },
            "zero_flag": {
                "bucket_script": {
                    "buckets_path": { "count": "country_data>_count" },
                    "gap_policy": "insert_zeros",
                    "script": { "source": "return ((params.count == 0) ? 0 : 1)" }
                }
            },
            "quality_negative_count": {
                "filter": {
                    "bool": {
                        "must": [
                            { "term": { "quality_label_class": { "value": "negative" } } }
                        ]
                    }
                }
            },
            "avg_score": { "avg": { "field": "review_score" } },
            "bucket_filter": {
                "bucket_selector": {
                    "buckets_path": { "count": "_count" },
                    "script": { "source": "params.count>=30" }
                }
            },
            "bucket_paging": {
                "bucket_sort": {
                    "from": 0,
                    "size": 50,
                    "sort": [
                        { "zero_flag": { "order": "desc" } },
                        { "country_data>avg_score": { "order": "desc" } },
                        { "avg_score": { "order": "desc" } },
                        { "_key": { "order": "desc" } }
                    ]
                }
            }
        },
        "terms": { "field": "root_planning_sum_id", "size": 10000 }
    }
},
"query": "..."
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745274084a4619927.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信