Filter by _score in aggregation

ES Version: 2.3

elasticsearch-py: 2.3.0

Currently, I am using the min_score 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

}

1 Like

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