Bucket sort on nested field

I have a problem with a bucket sort on nested field on Elastic 7.1.0:
My index has the following mapping:
{
"mapping": {
"dynamic": "strict",
"properties": {
"created_at_timestamp": {
"type": "date"
},
"url": {
"type": "keyword",
},
"title": {
"type": "keyword",
},
"entities": {
"type": "nested",
"properties": {
"counter": {
"type": "long"
},
"metric": {
"type": "long"
},
"id": {
"type": "long"
},
"relevance": {
"type": "float"
},
"weighted_metric": {
"type": "float"
}
}
}
}
}
}

and I need to order this documents by "weighted_metric", filtered for a specific entity id. I wrote this query:

GET my_index/_search?size=0
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "entities",
            "query": {
              "term": {
                "entities.id": "27374"
              }
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "title": {
              "value": ""
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "by_url_and_title": {
      "composite": {
        "sources": [
          {
            "final_url": {
              "terms": {
                "field": "final_url"
              }
            }
          },
          {
            "title": {
              "terms": {
                "field": "title"
              }
            }
          }
        ]
      },
      "aggs": {
        "sum_metric": {
          "nested": {
            "path": "entities"
          },
          "aggs": {
            "weightedmetric": {
              "filters": {
                "filters": {
                  "new": {
                    "bool": {
                      "should": [
                        {
                          "term": {
                            "entities.id": "27374"
                          }
                        }
                      ]
                    }
                  }
                }
              },
              "aggs": {
                "wmetric": {
                  "sum": {
                    "field": "entities.weighted_metric"
                  }
                }
              }
            },
            "w_sort": {
              "bucket_sort": {
                "sort": [
                  {
                    "weightedmetric.wmetric": {
                      "order": "desc"
                    }
                  }
                ],
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

And I have this error:

{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "class_cast_exception",
      "reason": "org.elasticsearch.search.aggregations.bucket.nested.InternalNested cannot be cast to org.elasticsearch.search.aggregations.InternalMultiBucketAggregation"
    }
  },
  "status": 503
}

If I don't try to order the buckets everything works fine.
Can someone help me with this query? I need to order the buckets by weighted_metric. thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.