Неверный путь заказа агрегации - PullRequest
0 голосов
/ 17 апреля 2020

Я использую 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"}

Как я могу отсортировать по значению, возвращенному просадка агрегация?

ошибка, которую я получаю:

{
  "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
}

1 Ответ

0 голосов
/ 17 апреля 2020

Невозможно - связано с этим вопросом и отслеживается на github .


Мы можем заказать термины aggs by субагрегации метрик с одним или несколькими значениями , то есть:

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 Я также не могу понять, почему метри с одним значением c не считается допустимым сортировщиком агрегации -.-

...