# Bucket sort on nested field

**URL:** <https://discuss.elastic.co/t/bucket-sort-on-nested-field/201774>\
**Category:** Elasticsearch\
**Created:** [October 1, 2019, 10:28am UTC](https://discuss.elastic.co/t/bucket-sort-on-nested-field/201774 "2019-10-01T10:28:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaus82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klaus82/32/45900_2.png) [@klaus82](https://discuss.elastic.co/u/klaus82)\
**Post date:** [October 1, 2019, 10:28am UTC](https://discuss.elastic.co/t/bucket-sort-on-nested-field/201774/1 "2019-10-01T10:28:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 29, 2019, 10:28am UTC](https://discuss.elastic.co/t/bucket-sort-on-nested-field/201774/2 "2019-10-29T10:28:34Z")

</div>

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