2

I want to write a query inside should such that when both matches the document then i will boost the score by 2 but when any one of them or none of them matches i do not want any boost my code is given below now can anyone please tell me how should i write my "Should" query so that i can do my work.

query: {
                bool: {
                  must: {
                    query_string: {
                      query: shop_search,
                      fields: ['shop_name'],
                      boost: 3.0
                    }
                  }, should: [
                    {
                      term : { 'address.area2' : search_area2 }
                    },
                    {
                      term : { "address.area1" : search_area1 }
                    }
                  ], "boost": 2.0
                } 
              }

1 Answer 1

5

Nest another boolean query inside your should clause:

"query": {
    "bool": {
        "must": {
            "query_string": {
                "query": shop_search,
                "fields": ["shop_name"],
                "boost": 3.0
            }
        },
        "should": {
            "bool": {
                "must": [{
                    "term": {
                        "address.area2": search_area2
                    }
                }, {
                    "term": {
                        "address.area1": search_area1
                    }
                }],
                "boost": 2.0
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

can you please write where i can specify boost also ?? i mean just type the query part ??
Sorry, I had it in the wrong place (now fixed). I think your goal is that when both area1 AND area2 match to boost the outer query score by 2. So you want the inner-bool to have a boost of 2.0 when matched.
like this ?? should: { bool: { must: [ { term : { 'address.area2' : "Area" } }, { term : { "address.area1" : "hitefield" } } ] }, "boost": 2.0 }
sorry but it's not working this is boosting even if only one area match but i want to boost only when both area's matches
Run it with "explain":true. You should see the match and score contribution coming only from shop_name.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.