【问题标题】:Is there a way to sort percentile aggregation by 'order' field is Elasticsearch?有没有办法通过'order'字段对百分位聚合进行排序是Elasticsearch?
【发布时间】:2021-04-11 00:28:52
【问题描述】:

我正在尝试通过order 字段对百分位数的子聚合进行排序(当我在 avg/sum 聚合上运行它时它可以工作)

{
  "size": 0,
  "_source": false,
  "stored_fields": "_none_",
  "aggregations": {
    "aggs": {
      "terms": {
        "field": "field",
        "size": 100,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "sub_aggs": "asc"
          },
          {
            "_key": "asc"
          }
        ]
      },
      "aggregations": {
        "sub_aggs": {
          "percentiles": {
            "field": "sub_agg_field",
            "percents": [95],
            "keyed": true,
            "tdigest": {
              "compression": 100
            }
          }
        }
      }
    }
  }
}

但是当我运行它时,我得到了下一个错误:

{
  "error": {
    "root_cause": [
      {
        "type": "aggregation_execution_exception",
        "reason": "Invalid aggregation order path [sub_aggs]. When ordering on a multi-value metrics aggregation a metric name must be specified"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "some_index",
        "node": "xJIpW6KTSMuL41-zu9um-w",
        "reason": {
          "type": "aggregation_execution_exception",
          "reason": "Invalid aggregation order path [sub_aggs]. When ordering on a multi-value metrics aggregation a metric name must be specified"
        }
      }
    ]
  },
  "status": 500
}

问题是我如何在多值聚合中做到这一点(如果它在单聚合中不起作用)?

【问题讨论】:

    标签: elasticsearch aggregation elasticsearch-aggregation


    【解决方案1】:

    该文档包含有关如何按多值聚合https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order 进行排序的指南

    按多值指标子聚合(由聚合名称标识)对存储桶进行排序:

    GET /_search
    {
      "aggs": {
        "genres": {
          "terms": {
            "field": "genre",
            "order": { "playback_stats.max": "desc" }
          },
          "aggs": {
            "playback_stats": { "stats": { "field": "play_count" } }
          }
        }
      }
    }
    

    所以也许这会起作用?

    {
      "size": 0,
      "_source": false,
      "stored_fields": "_none_",
      "aggregations": {
        "aggs": {
          "terms": {
            "field": "field",
            "size": 100,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "sub_aggs.95": "asc"
              },
              {
                "_key": "asc"
              }
            ]
          },
          "aggregations": {
            "sub_aggs": {
              "percentiles": {
                "field": "sub_agg_field",
                "percents": [95],
                "keyed": true,
                "tdigest": {
                  "compression": 100
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我认为它有效@Lupidon?因为我自己没有检查:)
    • 是的,它有效:),我认为一般的方法是使用“[]”,因为对于带有提醒的百分位数,“。”存在问题
    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 2017-09-13
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多