aman verma
aman verma

Reputation: 762

Query only for increase scoring in ElasticSearch?

I want to write a query in elasticsearch only for scoring can anybody please tell me how can i do that my query is given below also "minimum_should_match": 0 is not working with should, if any query matches both area2 and area1 then i want to boost the score of the result now how can i achieve that can anybody tel me the final query which i should write ??

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

Upvotes: 0

Views: 257

Answers (1)

eemp
eemp

Reputation: 1166

Do you want to get all records that match the shop search and boost the results where the the address.area1 and/or address.area2 field values match the area searches?

The syntax you are using for the bool is off. The match clause needs to be fixed up to fit Elasticsearch's expectations and the extra nested bool should not be necessary. See Elasticsearch Bool Query.

{
    "query" : {
        "bool" : {
            "must" : {
                "query_string" : {
                    "query" : <SHOP_SEARCH>,
                    "fields" : ['shop_name']
                }
            },
            "should" : [
                {
                    "match" : {
                        "address.area1" : {
                            "query" : <SEARCH_AREA1>,
                            "operator" : "and",
                            "boost" : <CUSTOM_BOOST1>
                        }
                    }
                },
                {
                    "match" : {
                        "address.area2" : {
                            "query" : <SEARCH_AREA2>,
                            "operator" : "and",
                            "boost" : <CUSTOM_BOOST2>
                        }
                    }
                }
            ]
        }
    }
}

I think with that query, you can expect the the following:

  • only results that match the shop search
  • results that do not match on the area fields but do match the shop search - this is due to the presence of the match clause
  • order of the results will generally be results that match both areas first, followed by results that match one of the areas, followed by results that match neither of the areas - but no guarantees. Learn more about Elasticsearch scoring

You might want to review everything you are getting with query_string.

Upvotes: 1

Related Questions