我开始学习Elasticsearch并遇到了对分组结果进行排序的问题。
我试图在文档中找到相关内容,但我没有找到任何内容。
这就是我所拥有的:
映射:
{
"mappings": {
"post": {
"properties": {
"tags": {
"type": "nested"
}
}
}
}
}
文章:
{
"title": "some title",
"message": "some message",
"tags": [
{
"id": 1,
"title": "some tag title"
},
{
"id": 2,
"title": "some tag title 2"
},
{
"id": 3,
"title": "some tag title 3"
}
]
}
{
"title": "some title 2",
"message": "some message 2",
"tags": [
{
"id": 2,
"title": "some tag title 2"
}
]
}
{
"title": "some title 3",
"message": "some message 3",
"tags": [
{
"id": 1,
"title": "some tag title"
},
{
"id": 2,
"title": "some tag title 2"
}
]
}
我的要求:
{
"size": 0,
"aggs": {
"group_by_tags": {
"nested": {
"path": "tags"
},
"aggs": {
"tags": {
"terms": {
"field": "tags.id"
},
"aggs": {
"titles": {
"top_hits": {
"size": 1,
"_source": {
"include": [
"tags.title"
]
}
}
}
}
}
}
}
}
}
响应:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": [
]
},
"aggregations": {
"group_by_tags": {
"doc_count": 6,
"tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2,
"doc_count": 3,
"titles": {
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 0
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title 2"
}
}
}
]
}
}
},
{
"key": 1,
"doc_count": 2,
"titles": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 0
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title"
}
}
}
]
}
}
},
{
"key": 3,
"doc_count": 1,
"titles": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 2
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title 3"
}
}
}
]
}
}
}
]
}
}
}
}
如何查看按doc_count排序的结果聚合。
如何按tag.title排序结果聚合?
我想要的是什么:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": [
]
},
"aggregations": {
"group_by_tags": {
"doc_count": 6,
"tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 2,
"titles": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 0
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title"
}
}
}
]
}
}
},
{
"key": 2,
"doc_count": 3,
"titles": {
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 0
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title 2"
}
}
}
]
}
}
},
{
"key": 3,
"doc_count": 1,
"titles": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_nested": {
"field": "tags",
"offset": 2
},
"_score": 1,
"_source": {
"tags": {
"title": "some tag title 3"
}
}
}
]
}
}
}
]
}
}
}
}
对不起我的英文。
更新
我更新了请求:
{
"size": 0,
"aggs": {
"group_by_tags": {
"nested": {
"path": "tags"
},
"aggs": {
"tags": {
"terms": {
"field": "tags.id"
},
"aggs": {
"titles": {
"terms": {
"field": "tags.title.keyword"
}
}
}
}
}
}
}
}
更新2:
请求:
{
"size": 0,
"aggs": {
"group_by_tags": {
"nested": {
"path": "tags"
},
"aggs": {
"tags": {
"terms": {
"field": "tags.id",
"order": {
"titles": "asc"
}
},
"aggs": {
"titles": {
"terms": {
"field": "tags.title.keyword"
}
}
}
}
}
}
}
}
有了这个请求,我收到错误:
"reason": {
"type": "aggregation_execution_exception",
"reason": "Invalid terms aggregation order path [titles]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
}