Shcheklein
Shcheklein

Reputation: 6284

Constant weight for each part in a bool query in Elasticsearch

Is it possible to get number of "shoulds" matched or return constant weight per each should matched. For example, if I have a bool query like this:

"query": {
            "bool": {
                "should": [
                    { "match": { "last_name": "Shcheklein" }}, 
                    { "match": { "first_name": "Bart" }}
                ]
         }
   }

I would like to get:

score == 1 if one of the fields (last_name, first_name) matches (even in a fuzzy sense) score == 2 if both fields match

And is it possible to get a list of fields matched?

Upvotes: 4

Views: 1798

Answers (1)

keety
keety

Reputation: 17441

you could probably use constant score to achieve this and use highlighting to figure out the fields that matched.

Example :

{
   "query": {
      "bool": {
         "disable_coord": true,
         "should": [
            {
               "constant_score": {
                  "query": {
                     "match": {
                        "last_name": "Shcheklein"
                     }
                  },
                  "boost": 1
               }
            },
            {
               "constant_score": {
                  "query": {
                     "match": {
                        "first_name": "bart"
                     }
                  },
                  "boost": 1
               }
            }
         ]
      }
   }
}

Upvotes: 3

Related Questions