# Elasticsearch function\_score not working inside nested aggregations

**URL:** <https://discuss.elastic.co/t/elasticsearch-function-score-not-working-inside-nested-aggregations/327479>\
**Category:** Elasticsearch\
**Created:** [March 10, 2023, 11:28pm UTC](https://discuss.elastic.co/t/elasticsearch-function-score-not-working-inside-nested-aggregations/327479 "2023-03-10T23:28:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![frarafra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frarafra/32/118332_2.png) [@frarafra](https://discuss.elastic.co/u/frarafra)\
**Post date:** [March 10, 2023, 11:28pm UTC](https://discuss.elastic.co/t/elasticsearch-function-score-not-working-inside-nested-aggregations/327479/1 "2023-03-10T23:28:36Z")

</div>

I have an Elasticsearch query with nested aggregations. It was working as expected but when I added a `function_score` query it seems to not take it into account.

This is my query:

```auto
GET reviews/_search
{
  "size": 0,
  "aggs": {
    "media": {
      "filter": {
        "term": {
          "profileId": "uuid"
        }
      },
      "aggs": {
        "items": {
          "filter": {
            "exists": {
              "field": "gallery"
            }
          },
          "aggs": {
            "top_reviews_gallery": {
              "filter": {
                "bool": {
                  "must": {
                    "function_score": { //same behaviour as top_reviews_gallery.filter.function_score
                      "functions": [
                        {
                          "filter": {
                            "term": {
                              "version" : "5.5.9"
                            }
                          },
                          "weight": 1000
                        }
                      ]
                    }
                  }
                }
              },
              "aggs": {
                "media_items": {
                  "top_hits": {
                    "size": 100,
                    "sort": [
                      {
                        "timestamp.createdAt": {
                          "order": "desc"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

I do not get any error but reviews with version attribute higher than 5.5.9 have higher score and are returned first

---

<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:** [April 7, 2023, 11:28pm UTC](https://discuss.elastic.co/t/elasticsearch-function-score-not-working-inside-nested-aggregations/327479/2 "2023-04-07T23:28:48Z")

</div>

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