我们可以在字符串字段而不是术语字段上对术语聚合进行排序吗?

4ngedf3f  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(0)|浏览(159)

我们有一个字段为field1的术语聚合。我们必须使用另一个字段field2(字符串字段)对所有桶进行排序。此聚合中已存在子聚合。
是否可以创建另一个子聚合来帮助按字段2的asc或desc顺序对存储桶进行排序?
在这种情况下,使用sort是没有帮助的。
是否可以使用field2对聚合进行排序?
我尝试创建一个新列,在field1前面添加field2,并使用该列进行排序。但这有两个缺点
降低搜索能力
使用field1排序所获得的唯一性将丢失。含义-对于相同的field1值,field2可以有多个唯一值。因此,在使用新列时,获得的不同计数或桶数是错误的。
那么,有没有什么方法可以对某个索引的文档进行排序,将其分组为基于字段而不是术语字段的聚合?

"sources": {
            "terms": {
                "size": 150,
                "include": {
                    "partition": 0,
                    "num_partitions": 1
                },
                "field": "Field1.keyword",
                "order": {
                    "_key": "desc"
                }
            },
            "aggs": {
                "latest": {
                    "top_hits": {
                        "size": 1,
                        "_source": [
                            "Field3",
                            "Field2" 
                        ]               
                    }
                }
            }
        },
        "distinct_count": {
            "cardinality": {
                "field": "Field1.keyword"
            }
        }
    }

input:-

"total": 868228,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "deptindex",
                "_type": "metadata",
                "_id": "ID1",
                "_score": 1.0,
                "_source": {
                    "Name": "Ava",
                    "Dept": "D1",
                    "DateOfJoin": "2020-02-26T10:57:17"}
              },
              {
                "_index": "deptindex",
                "_type": "metadata",
                "_id": "ID2",
                "_score": 1.0,
                "_source": {
                    "Name": "Steph",
                    "Dept": "D2",
                    "DateOfJoin": "2020-02-24T10:57:17"}
              },
              {
                "_index": "deptindex",
                "_type": "metadata",
                "_id": "ID3",
                "_score": 1.0,
                "_source": {
                    "Name": "Carl",
                    "Dept": "D2",
                    "DateOfJoin": "2020-02-01T10:57:17"}
              },
{
                "_index": "deptindex",
                "_type": "metadata",
                "_id": "ID3",
                "_score": 1.0,
                "_source": {
                    "Name": "May",
                    "Dept": "D3",
                    "DateOfJoin": "2020-02-11T10:57:17"}
              },
...] }

输出:-所有最热门的点击应该按asc或desc顺序排列。

"total": 25352,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "sources": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "D2",
                    "doc_count": 2,
                    "latest": {
                        "hits": {
                            "total": 2,
                            "max_score": 1.0,
                            "hits": [
                                {
                                    "_index": "deptindex",
                                    "_type": "metadata",
                                    "_id": "ID2",
                                    "_score": 1.0,
                                    "_source": {
                                        "Name": "Steph"
                                    }
                                }
                            ]
                        }
                    }
                },
                 {
                    "key": "D3",
                    "doc_count": 2,
                    "latest": {
                        "hits": {
                            "total": 3,
                            "max_score": 1.0,
                            "hits": [
                                {
                                    "_index": "deptindex",
                                    "_type": "metadata",
                                    "_id": "ID3",
                                    "_score": 1.0,
                                    "_source": {
                                        "Name": "May"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "key": "D1",
                    "doc_count": 5,
                    "latest": {
                        "hits": {
                            "total": 2,
                            "max_score": 1.0,
                            "hits": [
                                {
                                    "_index": "deptindex",
                                    "_type": "metadata",
                                    "_id": "ID1",
                                    "_score": 1.0,
                                    "_source": {
                                        "Name": "Ava"
                                    }
                                },...
                            ]
                        }
                    }
                }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题