【发布时间】:2021-03-08 09:14:00
【问题描述】:
我想执行搜索请求,并且只接收文档中的唯一字段,按特定字段排序。 在我的情况下,我希望结果按时间戳排序,并且我只需要字段 refId。为了只获取 id,我使用聚合。
来自 Elastic Dev Tools 的请求示例
POST /MY-INDEX/_search
{
"size": 0
"query": {
"bool": {
//my query
}
},
"sort" : [
{ "timestamp": {"order" : "asc"}}
],
"aggs": {
"agg_id": {
"terms": {
"field": "refId"
}
}
}
}
但是,聚合的顺序与搜索结果的顺序不匹配。是否有可能有相同的聚合顺序?
---- 更新 ---- 数据示例
{
"timestamp": 1604582511657,
"id": 4
"refID": "ref3"
}
{
"timestamp": 1604582511655,
"id": 3
"refID": "ref1"
}
{
"timestamp": 1604582511654,
"id": 2
"refID": "ref1"
}
{
"timestamp": 1604582511653,
"id": 1
"refID": "ref2"
}
搜索结果
"aggregations": {
"unique_id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ref1",
"doc_count": 2,
},
{
"key": "ref2",
"doc_count": 1,
},
{
"key": "ref3",
"doc_count": 1,
}
]
}
}
预期结果:
"buckets": [
{
"key": "ref2",
"doc_count": 2,
},
{
"key": "ref1",
"doc_count": 1,
},
{
"key": "ref3",
"doc_count": 1,
}
]
聚合结果按 doc_count 排序,但不像搜索查询按时间戳排序
【问题讨论】:
-
能否分享一些示例索引数据和预期的搜索结果?
-
@Bhavya 为数据和结果添加了一个示例
-
根据您的示例数据,
"refID": "ref1"的doc_count为 2,您为什么将"refID": "ref2"的doc_count显示为 2? -
能否也分享一下您的预期搜索结果?
-
抱歉,这是一个复制和粘贴错误。感谢您的解决方案,这正是我想要的。
标签: elasticsearch