# Combine two function\_score queries in dis\_max

**URL:** <https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666>\
**Category:** Elasticsearch\
**Created:** [January 5, 2017, 10:39am UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666 "2017-01-05T10:39:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bender268](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bender268](https://discuss.elastic.co/u/bender268)\
**Post date:** [January 5, 2017, 10:39am UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666/1 "2017-01-05T10:39:13Z")

</div>

Hello! I would like to make a query with two subqueries, each of them has it's own scoring based on function\_score with script. For example, this subquery

> ```
> {
> "query":{
> "function_score":{
> "query":{
> "bool":{
> "filter":[
> {
> "term":{
> "rooms_count":3
> }
> },
> {
> "term":{
> "addresses":"d76255c8-3173-4db5-a39b-badd3ebdf851"
> }
> },
> {
> "exists":{
> "field":"zhk_id"
> }
> }
> ]
> }
> },
> "script_score":{
> "script":"1 * doc['price'].value/100000"
> },
> "boost_mode":"replace"
> }
> }
> }
> 
> ```

works fine, and it's score is based on price (about 190 points). But if I try to combine two subquieries in dis\_max query, function\_score is not working and I get scores about 1 point.  
Explanation for each subquery looks like this

> "value": 100.9416, "description": "script score function, computed with script:"[script: 1 \* doc['price'].value/100000, type: inline, lang: null, params: {}]" and parameters:  
> {}",  
> for dis\_max query like  
> `"value": 1, "description": "ConstantScore(function score (#rooms_count:` #addresses:d76255c8-3173-4db5-a39b-badd3ebdf851 #ConstantScore(\_field\_names:zhk\_id),function=script[script: 1 \* doc['price'].value/100000, type: inline, lang: null, params: {}])), product of:",`

Can anybody tell me, how to combine function\_score queries properly?

My full dis\_max query [on pastebin](http://pastebin.com/CfFu6XgA)

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [January 5, 2017, 3:59pm UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666/2 "2017-01-05T15:59:25Z")

</div>

Hi @bender268,

based on what I see here everything is fine. Maybe there is some problem in your data?

Here is a complete example based on Elasticsearch 5.1.1 (just replace the `keyword` field with a `not_analyzed` string field if you use ES 2.x):

```auto
DELETE /immo

PUT /immo
{
    "mappings": {
        "flat": {
            "properties": {
                "address": {
                    "type": "keyword"
                },
                "rooms": {
                    "type": "integer"
                },
                "price": {
                    "type": "float"
                }
            }
        }
    }
}

# Create a few matching (and also non-matching flats)

POST /immo/flat
{
    "address": "Munich",
    "rooms": 3,
    "price": "500000",
    "zhk_id": 42
}

POST /immo/flat
{
    "address": "Munich",
    "rooms": 1,
    "price": "200000",
    "zhk_id": 43
}

POST /immo/flat
{
    "address": "Munich",
    "rooms": 3,
    "price": "700000"
}

POST /immo/flat
{
    "address": "Munich",
    "rooms": 3,
    "price": "800000"
}

POST /immo/flat
{
    "address": "Munich",
    "rooms": 3,
    "price": "700000",
    "zhk_id": 48
}

POST /immo/flat
{
    "address": "Munich",
    "rooms": 3,
    "price": "336000",
    "zhk_id": 88
}

```

If I run a slightly modified version of your query (adapted to the simplified example and the 5.x syntax):

```auto
GET /immo/flat/_search
{
   "query": {
      "dis_max": {
         "queries": [
            {
               "function_score": {
                  "query": {
                     "bool": {
                        "filter": [
                           {
                              "term": {
                                 "rooms": 3
                              }
                           },
                           {
                              "term": {
                                 "address": "Munich"
                              }
                           },
                           {
                              "bool": {
                                 "must_not": {
                                    "exists": {
                                       "field": "zhk_id"
                                    }
                                 }
                              }
                           }
                        ]
                     }
                  },
                  "script_score": {
                     "script": "doc['price'].value / 100000"
                  },
                  "boost_mode": "replace"
               }
            },
            {
               "function_score": {
                  "query": {
                     "bool": {
                        "filter": [
                           {
                              "term": {
                                 "rooms": 3
                              }
                           },
                           {
                              "term": {
                                 "address": "Munich"
                              }
                           },
                           {
                              "exists": {
                                 "field": "zhk_id"
                              }
                           }
                        ]
                     }
                  },
                  "script_score": {
                     "script": "doc['price'].value / 100000"
                  },
                  "boost_mode": "replace"
               }
            }
         ]
      }
   }
}

```

I get reasonable scores:

```auto
{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 5,
      "max_score": 8,
      "hits": [
         {
            "_index": "immo",
            "_type": "flat",
            "_id": "AVlvVc1FYSS4vzD5IS5l",
            "_score": 8,
            "_source": {
               "address": "Munich",
               "rooms": 3,
               "price": "800000"
            }
         },
         {
            "_index": "immo",
            "_type": "flat",
            "_id": "AVlvVdtgYSS4vzD5IS5m",
            "_score": 7,
            "_source": {
               "address": "Munich",
               "rooms": 3,
               "price": "700000",
               "zhk_id": 48
            }
         },
         {
            "_index": "immo",
            "_type": "flat",
            "_id": "AVlvVcIuYSS4vzD5IS5k",
            "_score": 7,
            "_source": {
               "address": "Munich",
               "rooms": 3,
               "price": "700000"
            }
         },
         {
            "_index": "immo",
            "_type": "flat",
            "_id": "AVlvVaXZYSS4vzD5IS5i",
            "_score": 5,
            "_source": {
               "address": "Munich",
               "rooms": 3,
               "price": "500000",
               "zhk_id": 42
            }
         },
         {
            "_index": "immo",
            "_type": "flat",
            "_id": "AVlvViw9YSS4vzD5IS5n",
            "_score": 3.36,
            "_source": {
               "address": "Munich",
               "rooms": 3,
               "price": "336000",
               "zhk_id": 88
            }
         }
      ]
   }
}

```

By the way, I don't think you need dis\_max for the query as you posted it but I guess the original one is more complex which is why you want to use it.

Daniel

---

<div class="post-metadata">

**Author:** ![bender268](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bender268](https://discuss.elastic.co/u/bender268)\
**Post date:** [January 5, 2017, 4:18pm UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666/3 "2017-01-05T16:18:56Z")

</div>

Thank you very much, @danielmitterdorfer!!!

I repeated your query and it worked!

After that, I found that in my query, there was an extra "query" keyword before "function\_score".  
I removed it and BINGO! it's all working!

I really appreciate your help, this problem cost me 3 days!

Thank you again!!!

Correct query is:

```
    {
   "query":{
      "dis_max":{
         "queries":[
        {
                        "function_score":{
                           "query":{
                              "bool":{
                                 "filter":[
                                    {
                                       "term":{
                                          "rooms_count":3
                                       }
                                    },
                                    {
                                       "term":{
                                          "addresses":"d76255c8-3173-4db5-a39b-badd3ebdf851"
                                       }
                                    },
                                    {
                                       "missing":{
                                          "field":"zhk_id"
                                       }
                                    }
                                 ]
                              }
                           },
                           "script_score":{
                              "script":"1 * doc['price'].value/100000"
                           },
                           "boost_mode":"replace"
                        }
                  },
        {
                        "function_score":{
                           "query":{
                              "bool":{
                                 "filter":[
                                    {
                                       "term":{
                                          "rooms_count":3
                                       }
                                    },
                                    {
                                       "term":{
                                          "addresses":"d76255c8-3173-4db5-a39b-badd3ebdf851"
                                       }
                                    },
                                    {
                                       "exists":{
                                          "field":"zhk_id"
                                       }
                                    }
                                 ]
                              }
                           },
                           "script_score":{
                              "script":"1 * doc['price'].value/100000"
                           },
                           "boost_mode":"replace"
                        }
                     }
         ]
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [January 5, 2017, 4:26pm UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666/4 "2017-01-05T16:26:14Z")

</div>

Hi @bender268,

glad I could help you. 🙂

Daniel

---

<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:** [February 2, 2017, 4:26pm UTC](https://discuss.elastic.co/t/combine-two-function-score-queries-in-dis-max/70666/5 "2017-02-02T16:26:35Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
