Elasticsearch – how do i add `boost` to `constant_score` in bodybuilder?

Issue

I have the following bodybuilder query :-

bodybuilder()
        .orQuery('match_all', {})
        .orQuery('constant_score', null , (qr) => {
           return qr.filter('range', 'stock_sum', {gte: 1})
         })
        .build()

Which basically generate the following (Query simulator HERE)

{
  "query": {
    "bool": {
      "should": [
        {
          "match_all": {}
        },
        {
          "constant_score": {
            "filter": {
              "range": {
                "stock_sum": {
                  "gte": 1
                }
              }
            }
          }
        }
      ]
    }
  }
}

Now what i would like to do is add a boost to constant_score as highlighted in the doc HERE. However i don’t see a way to do it, so what i’d basically like is the following:-

{
  "constant_score": {
      "filter": {
      "term": {
        "stock_sum": 0
      }
    },
    "boost": 10
  }
}

Now one thing i can do is add "boost": 10 immediatly after "stock_sum" like so, but i’am not sure it means the same thing.

{
  "constant_score": {
      "filter": {
      "term": {
        "stock_sum": 0
        "boost": 10
      }
    },
  }
}

So i guess basically my main question is how do i add boost to constant_score in bodybuilder.

P.S. if i may ask a supplementary question , i am unable to see _score in elasticview, how exactly do i see this ?

NOTE :- I don’t want to use rawOption.

Solution

This should work for you about adding the boost:

bodybuilder()
        .orQuery('match_all', {})
        .orQuery('constant_score', {boost: 10} , (qr) => {
           return qr.filter('range', 'stock_sum', {gte: 1})
         })
        .build()

Answered By – YD9

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.