Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 815
momo Avatar asked Feb 27 '13 11:02

momo


1 Answers

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
                }
            }]
        }
    }
}
like image 110
imotov Avatar answered Sep 17 '22 18:09

imotov