Home > database >  How to filter ElasticSearch results without having it affect the document score?
How to filter ElasticSearch results without having it affect the document score?

Time:02-04

I am trying to filter my results on "publication_year" field but I don't want it to affect the score of the document, but if I add the "range" to the query or to "filter", it seems to affect the score and score the documents higher whose "publication_year" is closer to "lte" or "less than equal to" the upper limit in the "range".

My query:

query = {
                
                'bool': {
                    'should': [
                        {
                            'match_phrase': {
                                "title": keywords
                            }
                        },
                        {
                            'match_phrase': {
                                "abstract": keywords
                            }
                        },
                    ]
                }
                        
            }
            if publication_year_constraint:
                range_query = {"range":{"publication_year":{"gte":publication_year_constraint, "lte": datetime.datetime.today().year}}}
                query["bool"]["filter"] = [range_query]

tried putting the "range" inside the "should" block as well, similar results.

CodePudding user response:

Try use Filter Context.

In a filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated.

Example:

{ 

 "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
      "filter": [ 
        { "term":  { "status": "published" }},
        { "range": { "publish_date": { "gte": "2015-01-01" }}}
      ]
    }
  }
}
  • Related