Lupidon
Lupidon

Reputation: 619

Is there a way to sort percentile aggregation by 'order' field is Elasticsearch?

I'm trying to order sub aggregation of percentile by order field (when I run it on avg/sum aggregation it works)

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

But when I run it I get the next error:

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

The question is how I do it in multi-value aggregation (If it does not work as it works in single aggregation)?

Upvotes: 1

Views: 888

Answers (1)

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

The documentation contains guides on how to sort by multi-value aggregation https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order

Ordering the buckets by multi value metrics sub-aggregation (identified by the aggregation name):

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

So maybe this would work instead?

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

Upvotes: 1

Related Questions