# ElasticSearch Query with function score is running more than 5 times slower

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-with-function-score-is-running-more-than-5-times-slower/53217>\
**Category:** Elasticsearch\
**Created:** [June 18, 2016, 6:59pm UTC](https://discuss.elastic.co/t/elasticsearch-query-with-function-score-is-running-more-than-5-times-slower/53217 "2016-06-18T18:59:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vijaymohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijaymohan/32/10390_2.png) [@vijaymohan](https://discuss.elastic.co/u/vijaymohan)\
**Post date:** [June 18, 2016, 6:59pm UTC](https://discuss.elastic.co/t/elasticsearch-query-with-function-score-is-running-more-than-5-times-slower/53217/1 "2016-06-18T18:59:41Z")

</div>

Here is my query without function score:

```
{
  "from": 200,
  "size": 25,
  "query": {
    "bool": {
    "filter": {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "terms": {
                      "cotypes.id": [
                        199
                      ]
                    }
                  },
                  "path": "cotypes"
                }
              },
              {
                "range": {
                  "relevance": {
                    "from": 6,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true
                  }
                }
              }
            ],
            "must_not": {
              "terms": {
                "ontologyId": [
                  1314696,
                  1314691
                ]
              }
            }
          }
        },
      "must": {
        "match": {
          "name.nameStandard": {
            "query": "john smith",
            "type": "boolean",
            "boost": 10
          }
        }
      }
    }
  }
}

```

This query return the response in ~250ms.

But I need to add some boost factor for improve the default scoring. I modified the query to use the function score, but after that query taking too long (~1300ms)

Here is the function score query:

```
{
  "from": 200,
  "size": 25,
  "query": {
  "function_score": {
    "query": {
    "bool": {
    "filter": {
          "bool": {
            "must": [
              {
                "nested": {
                  "query": {
                    "terms": {
                      "cotypes.id": [
                        199
                      ]
                    }
                  },
                  "path": "cotypes"
                }
              },
              {
                "range": {
                  "relevance": {
                    "from": 6,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true
                  }
                }
              }
            ],
            "must_not": {
              "terms": {
                "ontologyId": [
                  1314696,
                  1314691
                ]
              }
            }
          }
        },
      "must": {
        "match": {
          "name.nameStandard": {
            "query": "john smith",
            "type": "boolean",
            "boost": 10
          }
        }
      }
    }
    },
    "functions": [
      {
        "script_score": {
          "script": {
            "file": "calculate-score",
            "lang": "groovy",
            "params": {
              "relevance_boost": 0.5
            }
          }
        }
      }
    ],
    "boost_mode": "sum"
  }
}
}

```

calculate-score.groovy script given below:

```
def penalize = 1
def penalizeClassDict = [
    '226': 0.25,
    '14106': 0.25,
    '656': 0.25
   ]
for (item in doc['classIds'].values)
{
    if(penalizeClassDict.containsKey(item.toString()))
        penalize = penalize * penalizeClassDict[item.toString()]
}
_score + (pow(doc['relevance'].value, relevance_boost)) * penalize

```

Please help me to make the query perform better!

Thank you in advance!

---

<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, 10:42pm UTC](https://discuss.elastic.co/t/elasticsearch-query-with-function-score-is-running-more-than-5-times-slower/53217/2 "2017-07-05T22:42:33Z")

</div>


