【问题标题】:Elasticsearch aggregation order same as search resultElasticsearch 聚合顺序与搜索结果相同
【发布时间】: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


【解决方案1】:

您可以使用terms aggregationmax aggregation 来实现您所需的用例

添加一个包含索引数据、搜索查询和搜索结果的工作示例

索引数据:

{
  "timestamp": 1604582511657,
  "id": 4
  "refID": "ref3"
}
{
  "timestamp": 1604582511656,
  "id": 3
  "refID": "ref1"
}
{
  "timestamp": 1604582511654,
  "id": 2
  "refID": "ref1"
}
{
  "timestamp": 1604582511655,
  "id": 1
  "refID": "ref2"
}

搜索查询:

  {
  "size": 0,
  "aggs": {
    "unique_id": {
      "terms": {
        "field": "refID.keyword",
        "order": {
          "latestOrder": "desc"
        }
      },
      "aggs": {
        "latestOrder": {
          "max": {
            "field": "timestamp"
          }
        }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "unique_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "ref3",
          "doc_count": 1,
          "latestOrder": {
            "value": 1.604582511657E12
          }
        },
        {
          "key": "ref1",
          "doc_count": 2,
          "latestOrder": {
            "value": 1.604582511656E12
          }
        },
        {
          "key": "ref2",
          "doc_count": 1,
          "latestOrder": {
            "value": 1.604582511655E12
          }
        }
      ]
    }

【讨论】:

  • @Clemens 请仔细阅读我的回答,如果这能解决您的问题,请告诉我?
猜你喜欢
  • 2020-02-06
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多