Search code examples
elasticsearchtire

Elastic search Query terms and scoring


I have a request using terms looking like :

{
"query": {
  "bool": {
    "should": [
      {
        "terms": {
          "subjects.id": [
            1,
            2,
            3
          ], boost: 3
        }
      },
      {
        "terms": {
          "qualification_type.id": [
            3,
            5
          ], boost: 2
        }
      }
    ]
  }
}

I works pretty well but attributes a better score to documents that match three subject than to the document matching only one subject.

My question is : is there a way to force the score to be the same if there is one or many match on the subjects ?

Thanks in advance !


Solution

  • You can convert the terms queries into filters and wrap them into constant score query. For example:

    {
        "query": {
            "bool": {
                "should": [{
                    "constant_score": {
                        "filter": {
                            "terms": {
                                "subjects.id": [1, 2, 3]
                            }
                        },
                        boost: 3
                    }
                }, {
                    "constant_score": {
                        "filter": {
                            "terms": {
                                "qualification_type.id": [3, 5]
                            }
                        },
                        boost: 2
                    }
                }]
            }
        }
    }