Elastic search Query terms and scoring
Asked Answered
B

1

8

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 !

Brokaw answered 27/2, 2013 at 11:23 Comment(1)
this may help someone, i found it much later after having this same question elasticsearch.org/blog/…Vulcanism
E
10

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
                }
            }]
        }
    }
}
Encrimson answered 27/2, 2013 at 12:31 Comment(3)
Yes, I thought about it, and tried it. But then I can't set up two differents boost right ? I edit the example to try to be more explicit about it.Brokaw
You can wrap each terms filter into its own constant_score query. I added an example.Encrimson
Hey, thanks a lot, it was a great help ! I tried to play with custom_score by doing similar stuff but never got that idea. Maybe could you point me to documentation where I could find some example like that ? I searched pretty much and did not find something near.Brokaw

© 2022 - 2024 — McMap. All rights reserved.