# Query boosting vs function score query

**URL:** <https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597>\
**Category:** Elasticsearch\
**Created:** [March 6, 2016, 2:40pm UTC](https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597 "2016-03-06T14:40:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilliamRoma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/williamroma/32/8322_2.png) [@WilliamRoma](https://discuss.elastic.co/u/WilliamRoma)\
**Post date:** [March 6, 2016, 2:40pm UTC](https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597/1 "2016-03-06T14:40:19Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 6, 2016, 8:22pm UTC](https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597/2 "2016-03-06T20:22:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WilliamRoma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/williamroma/32/8322_2.png) [@WilliamRoma](https://discuss.elastic.co/u/WilliamRoma)\
**Post date:** [March 7, 2016, 1:10am UTC](https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597/3 "2016-03-07T01:10:20Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:10pm UTC](https://discuss.elastic.co/t/query-boosting-vs-function-score-query/43597/4 "2017-07-05T23:10:46Z")

</div>


