elasticsearch categorize_text聚合使用子top_hits排序聚合

a11xaf1n  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(213)

我有下面的聚合。但是我希望topClassificationGroup中的bucket也按照与topHits相同的排序进行排序。因此,包含“最佳”top_hit的bucket应该是分类聚合中的第一个。

"aggs": {
    "classification": {
        "categorize_text": {
            "field": "classification"
        },
        "aggs": {
            "max_score_inner": {
                "max": {
                    "script": {
                        "lang": "expression",
                        "source": "_score"
                    }
                }
            },
            "topClassificationGroup": {
                "categorize_text": {
                    "field": "subClassification",
                    "size": 3
                },
                "aggs": {
                    "topSubClassificationGroup": {
                        "top_hits": {
                            "size": 3,
                            "track_scores": true,
                            "sort": [
                                {
                                    "orderCount2": "desc"
                                },
                                {
                                    "bestOverall.raw": "desc"
                                },
                                {
                                    "bestOverallC.raw": "desc"
                                },
                                {
                                    "orderCount": "desc"
                                },
                                "_score"
                            ]
                        }
                    }
                }
            }
        }
    }
}
crcmnpdw

crcmnpdw1#

我可以使用聚合与bucket_sort一起对一些字段。但我想知道是否有一个更简单的解决方案。也为关键字字段可能不工作,我不知道如何。

"aggs": {
    "classification": {
        "categorize_text": {
            "field": "classification"
        },
        "aggs": {
            "max_score_inner": {
                "max": {
                    "script": {
                        "lang": "expression",
                        "source": "_score"
                    }
                }
            },
            "topClassificationGroup": {
                "categorize_text": {
                    "field": "subClassification",
                    "size": 3
                },
                "aggs": {
                    "max_order_count_2": {
                        "max": {
                            "field": "orderCount2"
                        }
                    },
                    "max_order_count": {
                        "max": {
                            "field": "orderCount"
                        }
                    },
                    "max_score": {
                        "max": {
                            "script": {
                                "lang": "expression",
                                "source": "_score"
                            }
                        }
                    },
                    "topSubClassificationGroup": {
                        "top_hits": {
                            "size": 3,
                            "track_scores": true,
                            "sort": [
                                {
                                    "orderCount2": "desc"
                                },
                                {
                                    "bestOverall.raw": "desc"
                                },
                                {
                                    "bestOverallC.raw": "desc"
                                },
                                {
                                    "orderCount": "desc"
                                },
                                "_score"
                            ]
                        }
                    },
                    "sort_bucket": {
                        "bucket_sort": {
                            "sort": [
                                { "max_order_count_2": { "order": "desc" } },
                                { "max_order_count": { "order": "desc" } },
                                { "max_score": { "order": "desc" } }
                            ],
                            "gap_policy": "insert_zeros"
                        }
                    }
                }
            }
        }
    }
}

相关问题