Query boosting vs function score query


(William Kinaan) #1

I have the following query:

get all the documents that its field field1 contains a specific value, but I want to give more priority to the documents that its another field field2 contains a specific value.

I came up with the following two solutions:
First one (Function score query):

GET /myIndexName/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "field1": {
                  "value": "ValueHere"
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "_score * (doc['field2'].value == 'AnotherValue' ? 2 : 1)"
          }
        }
      ]
    }
  }
}

Second query (query boosting):

GET myIndexName/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "field1": {
            "query": "ValueHere", "operator": "and"
          }
        }}
      ],
      "should": [
        {"match": {
         "building": {
           "query": "AnotherValue",
           "boost": 2
         }
        }
          
        }
      
      ]
    }
  }
}

Do you have any idea which one is better please ?


(Mark Walkom) #2

How do you define better here? Have you tested them?

I'd expect the first one to be slower as you are using a script, but only you can say what is best for your use case.


(William Kinaan) #3

Thanks for answering,

I define the better by the query that will bring documents that contains the first word in the first field and gives better ranking for the documents that contains a word in the second field.

Do both of them perform well here please ?


(system) #4