Is there a way to change the Addition action between 2 occurrence? (In Boolean query)

40 views Asked by At

I have the next Query:
curl -X POST "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool" : {

      "must" : 
        { "term" : 
          { "message" : 
            {
              "value": "message",
              "boost": 2.0 
            }
          } 
        },
        "should" : 
        { "term" : 
          { "message" : 
            {
              "value": "message",
              "boost": 2.0 
            }
          } 
        }
    
    }
  }
}
'

The _score that I get from this response is multiple by 2 of _score (which I get without one of the scopes (must / should) )
My question is if there is a way to change the action to multiple instead of addition

for example - instead _score + _score I'll get _score * _score

1

There are 1 answers

6
Assael Azran On

You can make use of function score query

A working example (using script score):

_score will be multiplied by _score

Mappings

PUT index
{
  "mappings": {
    "properties": {
      "message": {
        "type": "keyword"
      }
    }
  }
}

Insert documents

POST index/_doc/1
{
  "message":"message"
}

POST index/_doc/2
{
  "message":"message"
}

Search query without script_score

GET index/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ],"should": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ]
        }
      }
    }
  }
}

Results:

"hits" : [
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.3862944,
        "_source" : {
          "message" : "message"
        }
      },
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.3862944,
        "_source" : {
          "message" : "message"
        }
      }
    ]

Search query using script_score

GET index/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ],"should": [
            {
              "term": {
                "message": {
                  "value": "message"
                }
              }
            }
          ]
        }
      },
      "script_score": {
        "script": {
          "source": "_score"
        }
      }
    }
  }
}

Results

"hits" : [
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.921812,
        "_source" : {
          "message" : "message"
        }
      },
      {
        "_index" : "index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.921812,
        "_source" : {
          "message" : "message"
        }
      }
    ]