5
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 0,
  "aggs": {
    "itineraryId": {
      "terms": {
        "field": "iid",
        "size": 2147483647,
        "order": [
          {
            "price>price>price.max": "desc"
          }
        ]
      },
      "aggs": {
        "duration": {
          "stats": {
            "field": "drn"
          }
        },
        "price": {
          "nested": {
            "path": "prl"
          },
          "aggs": {
            "price": {
              "filter": {
                "terms": {
                  "prl.cc.keyword": [
                    "USD"
                  ]
                }
              },
              "aggs": {
                "price": {
                  "stats": {
                    "field": "prl.spl.vl"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Here, I am getting the error:

"Invalid terms aggregation order path [price>price>price.max]. 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. Sub-path [price] points to non single-bucket aggregation"

query works fine if I order by duration aggregation like

"order": [
      {
        "duration.max": "desc"
      }

So is there any way to Order aggregation by nested aggregation on nested field i.e something like below ?

"order": [
      {
        "price>price>price.max": "desc"
      }
18
  • 2
    This is not supported yet, but a community pull request has just been submitted to address this issue: github.com/elastic/elasticsearch/pull/24121 Original issue: github.com/elastic/elasticsearch/issues/16838 Commented Apr 17, 2017 at 13:08
  • @Val Do you know any other workaround for this ? Commented Apr 17, 2017 at 14:45
  • Yes, add the maximum price as a new field at the root level. Commented Apr 17, 2017 at 14:47
  • 1
    Then you can have a max_price object with one price per currency "max_price": {"USD": 1000, "EUR": 1210, ...} Commented Apr 17, 2017 at 15:12
  • 1
    No, because there will only be a single value per currency (always the max price for any nested currency). You can then add another max sub-aggregation on max_price.USD and sort the iid terms with it. Commented Apr 17, 2017 at 15:34

1 Answer 1

1

As Val has pointed out in the comments ES does not support it yet.

Till then you can first aggregate the nested aggregation and then use the reverse nested aggregation to aggregate the duration, that is present in the root of the document.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html

Sign up to request clarification or add additional context in comments.

1 Comment

@J Aamish - I think thats not possible for nested field price. And on duration field, ordering already works as stated in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.