Skip to content

Parent/Child Aggregation ordering path not working #53534

@dizzzyroma

Description

@dizzzyroma

Elasticsearch version (bin/elasticsearch --version): 7.6.1

Plugins installed: [none]

JVM version (java -version): OpenJDK 11.0.6

OS version (uname -a if on a Unix-like system): Ubuntu 18.04

Aggregation ordering path not working when we try to aggregate parent/child documents. Elastic search return status 500
Expected result that elastic search return ordered aggregations

Steps to reproduce:

  1. Create index
PUT my_index
{
  "mappings": {
    "properties": {
        "employer_id": {
          "type": "integer"
        },
         "price": {
          "type": "integer"
        },
       "join_field": { 
        "type": "join",
        "relations": {
          "order": "item" 
        }
      }
    }
  }
}
  1. Problem query:
GET my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "relation": "order"
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "employer": {
      "terms": {
        "field": "employer_id",
        "size": "50",
        "order": {
          "child_join>price_sum": "desc"
        }
      },
      "aggs": {
        "child_join": {
          "children": {
            "type": "item"
          },
          "aggs": {
            "price_sum": {
              "sum": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}
  1. Response
{
  "error": {
    "root_cause": [
      {
        "type": "aggregation_execution_exception",
        "reason": "Invalid aggregation order path [child_join>price_sum]. 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. Sub-path [child_join] points to non single-bucket aggregation"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my_index",
        "node": "MMgye6_eSdqPGUc6buwxLQ",
        "reason": {
          "type": "aggregation_execution_exception",
          "reason": "Invalid aggregation order path [child_join>price_sum]. 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. Sub-path [child_join] points to non single-bucket aggregation"
        }
      }
    ]
  },
  "status": 500
}

The same functionality worked fine on Elasticsearch 5.3
Example:

PUT my_index
{
  "mappings": {
    "order": {
      "properties": {
        "employer_id": {
          "type": "integer"
        }
      }
    },
    "item": {
      "properties": {
        "price": {
          "type": "integer"
        }
      },
      "_parent": {
        "type": "order"
      }
    }
  }
}

GET my_index/order/_search
{
  "aggs": {
    "employer": {
      "terms": {
        "field": "employer_id",
        "size": "50",
        "order": {
          "child_join>price_sum": "desc"
        }
      },
      "aggs": {
        "child_join": {
          "children": {
            "type": "item"
          },
          "aggs": {
            "price_sum": {
              "sum": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions