Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading