# Filter by \_score in aggregation

**URL:** <https://discuss.elastic.co/t/filter-by-score-in-aggregation/112881>\
**Category:** Elasticsearch\
**Created:** [December 21, 2017, 7:03pm UTC](https://discuss.elastic.co/t/filter-by-score-in-aggregation/112881 "2017-12-21T19:03:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_Gilson](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@Luke\_Gilson](https://discuss.elastic.co/u/Luke_Gilson)\
**Post date:** [December 21, 2017, 7:03pm UTC](https://discuss.elastic.co/t/filter-by-score-in-aggregation/112881/1 "2017-12-21T19:03:37Z")

</div>

**ES Version: 2.3**

**elasticsearch-py: 2.3.0**

Currently, I am using the [min\_score](https://www.elastic.co/guide/en/elasticsearch/reference/2.1/search-request-min-score.html) attribute to remove the lower cruft of search results. e.g.:

```
{
    "query": {"filtered": {
        "query": {"bool": {
            "must": {"term" : {"color": "green"}}
        }}
    }},
    "min_score": 0.01
}

```

I run an aggregation on these results, grouping by another attribute.

```
{
    "query": {"filtered": {
        "query": {"bool": {
            "must": {"term" : {"color": "green"}}
        }}
    }},
    "aggs": {
             "brand": {"terms": {"field": "car_brand"},
             "aggs": {"num": {"cardinality": {"field": "upc"}}}
            }
    "min_score": 0.01
}

```

The problem is that the `min_score` is run on the final query results, and is not taken account in the `aggs` query. The aggregated results include all entries from `query`. This leads to the number of, say, green cars being returned in "hits" being smaller than the count returned in "aggs"

**Is it possible to filter the result-set passed to `aggs` by \_score?**

I've tried using the filtered aggregation, but receive an  
exception:

```
{
    "query": {"filtered": {
        "query": {"bool": {
            "must": {"term" : {"color": "green"}}
        }}
    }},
    "aggs": {
             "filtered_brands": {
                 "filter": {"script": {"script": {
                     "inline": "_score > 0.01", "lang": "expression"}}}
                 "aggs": {
                     "brand": {"terms": {"field": "car_brand"},
                     "aggs": {"num": {"cardinality": {"field": "upc"}}}
                 }
              }},
    "min_score": 0.01
}

```

Response:

```
{
"error": {
    "root_cause": [
        {
            "type": "script_exception",
            "reason": "Error evaluating inline script [_score > 0.01] using lang [expression]"
        }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
        {
            "shard": 0,
            "index": "foobar",
            "node": "node-1",
            "reason": {
                "type": "script_exception",
                "reason": "Error evaluating inline script [_score > 0.01] using lang [expression]",
                "caused_by": {
                    "type": "illegal_state_exception",
                    "reason": "Scores are not available in the current context"
                }
            }
        }
    ]
},
"status": 500

```

}

---

<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:** [January 18, 2018, 7:03pm UTC](https://discuss.elastic.co/t/filter-by-score-in-aggregation/112881/2 "2018-01-18T19:03:43Z")

</div>

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