【问题标题】:Invalid aggregation order path聚合顺序路径无效
【发布时间】:2020-07-30 09:59:33
【问题描述】:

我正在使用 ElasticSearch 7.6 和 test 索引,该索引具有以下映射:

{
  "test" : {
    "mappings" : {
      "properties" : {
        "amount" : {
          "type" : "long"
        },
        "group_id" : {
          "type" : "long"
        },
        "id" : {
          "type" : "long"
        }
      }
    }
  }
}

现在,我正在使用以下查询:

{
  "size": 0,
  "track_total_hits": false,
  "aggs": {
    "strategies": {
      "terms": {
        "field": "group_id",
        "order": {
          "drawdown": "asc"
        },
        "size": 100,
        "include": {
          "partition": 1,
          "num_partitions": 100
        }
      },
      "aggs": {
        "drawdown": {
          "scripted_metric": {
            "init_script": "state.id = 0; state.cumsum = 0; state.max = 0; state.min = 0; state.diff = 0",
            "map_script": "state.cumsum += doc.amount.value; if (state.cumsum>state.max) {state.max=state.cumsum;state.id=doc.id.value} if (state.max - state.cumsum >= state.diff) {state.min=state.cumsum;state.diff=state.max - state.cumsum;}",
            "combine_script": "return state;",
            "reduce_script": "states.sort((x, y) -> x.id - y.id); int min = states[0].min; int max = states[0].max; for(int i = 1; i < states.length; i++) { int nextMin = states[i].min; int nextMax = states[i].max; if (max > nextMax && min > nextMin) { min = nextMin; } else if (max < nextMax && max-min < nextMax-nextMin) { max = nextMax; min = nextMin;} } return max-min;"
          }
        }
      }
    }
  }
}

如果我删除订单部分查询效果很好,我指的是以下部分:

"order": {"drawdown": "asc"}

如何按drawdown聚合返回的值排序?

我得到的错误是:

{
  "error": {
    "root_cause": [
      {
        "type": "aggregation_execution_exception",
        "reason": "Invalid aggregation order path [drawdown]. 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."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test",
        "node": "jHJ3_eOLQ3iZZeHZPI_dFA",
        "reason": {
          "type": "aggregation_execution_exception",
          "reason": "Invalid aggregation order path [drawdown]. 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."
        }
      }
    ]
  },
  "status": 500
}

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-query


    【解决方案1】:

    不可能 - 与此 question 相关并在 github 上跟踪。


    我们可以通过 single or multi value metrics sub-aggregations 订购术语 aggs,即:

    GET test/_search
    {
      "size": 0,
      "track_total_hits": false,
      "aggs": {
        "strategies": {
          "terms": {
            "field": "group_id",
            "size": 100,
            "order": {
              "stats_agg.max": "desc"
            }
          },
          "aggs": {
            "stats_agg": {
              "stats": {
                "field": "amount"
              }
            }
          }
        }
      }
    }
    

    TBH 我也无法理解为什么单值脚本指标不被视为有效的聚合排序器-.-

    【讨论】:

    • 任何解决方法?我们不能按聚合器排序吗?真的吗?
    • 我们可以,但有一些限制。让我更新我的答案。
    • 等等,你添加了.max ok,但是我也可以用 scripted_metric 做类似的事情吗?
    • 我不知道,例如可能以某种方式复制回撤 agg 的值。放入另一个,然后对其进行排序。乌托邦?
    • 不,你不能。通过 github cmets 访问——其中的某个地方是有原因的。不完全记得它们是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2010-10-30
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多