# Accessing \_score in the filter of aggregations

**URL:** <https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877>\
**Category:** Elasticsearch\
**Created:** [February 3, 2019, 5:59pm UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877 "2019-02-03T17:59:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddig](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@eddig](https://discuss.elastic.co/u/eddig)\
**Post date:** [February 3, 2019, 5:59pm UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877/1 "2019-02-03T17:59:41Z")

</div>

There is an unexpected situation in how the `filter` aggregation works with the `_score` of a document. It seems that the `_score` is not accessible in the `script` inside of `filter` aggregation but in the same time, ES doesn't produce any errors in the response as it was in for the version 2.3 ([Filter by \_score in aggregation](https://discuss.elastic.co/t/filter-by-score-in-aggregation/112881)).  
Below an example to reproduce this behavior:  
Create index `test`

```
PUT test
{
  "settings" : {
    "number_of_shards" : 1
  },
  "mappings" : {
    "_doc" : {
      "properties" : {
        "name" : { "type" : "text" },
        "color" : { "type" : "keyword" }
      }
    }
  }
}

```

Create two documents, `red fox` and `arctic fox`

```
POST test/_doc
{
  "name": "arctic fox",
  "color": "white"
}

POST test/_doc
{
  "name": "red fox",
  "color": "red"
}

```

Do search for `red fox` and aggregate colors only of the most relevant documents

```
POST test/_search
{
  "query": {
    "match": {
      "name": "red fox"
    }
  },
  "aggs": {
    "mostRelevantColors": {
      "filter": {
        "script": {
          "script": {
            "source": "_score > 0.5",
            "lang": "expression"
          }
        }
      },
      "aggs": {
        "colors": {
          "terms": {
            "field": "color"
          }
        }
      }
    }
  }
}

```

Response

```
{
  "hits": {
    "hits": [
      {
        "_score": 0.87546873,
        "_type": "_doc",
        "_id": "l7petGgBlKKsTTjoJ0OO",
        "_source": {
          "color": "red",
          "name": "red fox"
        },
        "_index": "test"
      },
      {
        "_score": 0.18232156,
        "_type": "_doc",
        "_id": "f7pdtGgBlKKsTTjo-0O4",
        "_source": {
          "color": "white",
          "name": "arctic fox"
        },
        "_index": "test"
      }
    ],
    "total": 2,
    "max_score": 0.87546873
  },
  "_shards": {
    "successful": 1,
    "failed": 0,
    "skipped": 0,
    "total": 1
  },
  "took": 1,
  "aggregations": {
    "mostRelevantColors": {
      "colors": {
        "buckets": [],
        "sum_other_doc_count": 0,
        "doc_count_error_upper_bound": 0
      },
      "doc_count": 0
    }
  },
  "timed_out": false
}

```

I expect one color, `red` at the aggregations section. But there are no colors at all.

**Is ES doesn't support `_score` in `filter` aggregation and silently ignore it?**

UPDATE 1:  
By the way, the `_score` is available for the `stats` aggregation

```
POST test/_search
{
  "query": {
    "match": {
      "name": "red fox"
    }
  },
  "aggs": {
      "meta": {
          "stats": {
              "script": "_score"
          }
      }
  }
}

{
  "hits": {
    "hits": [
      {
        "_score": 0.87546873,
        "_type": "_doc",
        "_id": "l7petGgBlKKsTTjoJ0OO",
        "_source": {
          "color": "red",
          "name": "red fox"
        },
        "_index": "test"
      },
      {
        "_score": 0.18232156,
        "_type": "_doc",
        "_id": "f7pdtGgBlKKsTTjo-0O4",
        "_source": {
          "color": "white",
          "name": "arctic fox"
        },
        "_index": "test"
      }
    ],
    "total": 2,
    "max_score": 0.87546873
  },
  "_shards": {
    "successful": 1,
    "failed": 0,
    "skipped": 0,
    "total": 1
  },
  "took": 8,
  "aggregations": {
    "meta": {
      "count": 2,
      "max": 0.8754687309265137,
      "sum": 1.057790294289589,
      "avg": 0.5288951471447945,
      "min": 0.18232156336307526
    }
  },
  "timed_out": false
}

```

We also have a [doc article](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html#scripting-score) that said that the `_score` available for the aggregations.

---

<div class="post-metadata">

**Author:** ![eddig](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@eddig](https://discuss.elastic.co/u/eddig)\
**Post date:** [February 7, 2019, 7:47am UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877/2 "2019-02-07T07:47:31Z")

</div>

After additional research (source code of ES) I've found out that the Range aggregator also has access to the `_score`. Below an example that solved my problem:

```
POST /test/_search
{
  "query": {
    "match": {
      "name": "red fox"
    }
  },
  "aggs": {
    "mostRelevantColors": {
      "range": {
          "script": {
            "source": "_score"
          },
          "ranges": [
              {
                  "from": 0.5
              }
            ]
      },
      "aggs": {
        "colors": {
          "terms": {
            "field": "color"
          }
        }
      }
    }
  }
}

```

Response

```
{
  "hits": {
    "hits": [
      {
        "_score": 0.87546873,
        "_type": "_doc",
        "_id": "l7petGgBlKKsTTjoJ0OO",
        "_source": {
          "color": "red",
          "name": "red fox"
        },
        "_index": "test"
      },
      {
        "_score": 0.18232156,
        "_type": "_doc",
        "_id": "f7pdtGgBlKKsTTjo-0O4",
        "_source": {
          "color": "white",
          "name": "arctic fox"
        },
        "_index": "test"
      }
    ],
    "total": 2,
    "max_score": 0.87546873
  },
  "_shards": {
    "successful": 1,
    "failed": 0,
    "skipped": 0,
    "total": 1
  },
  "took": 1,
  "aggregations": {
    "mostRelevantColors": {
      "buckets": [
        {
          "colors": {
            "buckets": [
              {
                "key": "red",
                "doc_count": 1
              }
            ],
            "sum_other_doc_count": 0,
            "doc_count_error_upper_bound": 0
          },
          "from": 0.5,
          "key": "0.5-*",
          "doc_count": 1
        }
      ]
    }
  },
  "timed_out": false
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 7, 2019, 8:38am UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877/3 "2019-02-07T08:38:59Z")

</div>

For this sort of “category snapping” you may be interested in looking at the significant terms and sampler aggregation: [https://youtu.be/azP15yvbOBA](https://youtu.be/azP15yvbOBA)

While your example identifies that colour:red is popular in high-scoring docs it doesn’t determine that red is significantly associated (all docs in your index might be red). Popularity is not the same as significance as the above video shows.

---

<div class="post-metadata">

**Author:** ![eddig](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@eddig](https://discuss.elastic.co/u/eddig)\
**Post date:** [February 22, 2019, 7:11am UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877/4 "2019-02-22T07:11:34Z")

</div>

Thanks for the video! In our app we use custom scores with the ability to choose which term is more significant for the user. Despite it doesn't fit our requirements, I agree that significant terms are a good way to have a more relevance result in general.

---

<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:** [March 22, 2019, 7:11am UTC](https://discuss.elastic.co/t/accessing-score-in-the-filter-of-aggregations/166877/5 "2019-03-22T07:11:38Z")

</div>

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