Skip to content

Profiling aggregation with sort order throws 'invalid path' error #22123

@polyfractal

Description

@polyfractal

A query from Kibana configures some aggregations, then sorts a terms agg with a child cardinality agg. This works fine as a regular query, but when profiled throws an exception about bad sorting.

Reproduction:

DELETE test
PUT test

GET test/_search
{
  "profile": true,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "NOT _exists_: valueNum",
            "analyze_wildcard": true
          }
        },
        {
          "range": {
            "isoDate": {
              "gte": 1481522634609,
              "lte": 1481551200000,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "must_not": []
    }
  },
  "_source": {
    "excludes": []
  },
  "aggs": {
    "2": {
      "date_histogram": {
        "field": "isoDate",
        "interval": "10m",
        "time_zone": "America/New_York",
        "min_doc_count": 1
      },
      "aggs": {
        "3": {
          "terms": {
            "field": "displayName.keyword",
            "size": 5,
            "order": {
              "1": "desc"
            }
          },
          "aggs": {
            "1": {
              "cardinality": {
                "field": "displayName.keyword"
              }
            }
          }
        }
      }
    }
  }
}

Which throws:

{
   "error": {
      "root_cause": [
         {
            "type": "aggregation_execution_exception",
            "reason": "Invalid terms aggregation order path [1]. Terms 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": "YF_q6BE9SHuqJe0GauuVeA",
            "reason": {
               "type": "aggregation_execution_exception",
               "reason": "Invalid terms aggregation order path [1]. Terms 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."
            }
         }
      ],
      "caused_by": {
         "type": "aggregation_execution_exception",
         "reason": "Invalid terms aggregation order path [1]. Terms 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
}

The non-profiled version returns an empty set of results as you'd expect:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "2": {
         "buckets": []
      }
   }
}

@colings86 any thoughts? I haven't had a chance to look into this at all yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Search/SearchSearch-related issues that do not fall into other categories>bughelp wantedadoptme

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions