# Simple Function Score Query Shows Very Poor Performance

**URL:** <https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274>\
**Category:** Elasticsearch\
**Created:** [May 6, 2015, 10:32am UTC](https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274 "2015-05-06T10:32:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yosi\_Haran](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yosi_haran/32/44914_2.png) [@Yosi\_Haran](https://discuss.elastic.co/u/Yosi_Haran)\
**Post date:** [May 6, 2015, 10:32am UTC](https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274/1 "2015-05-06T10:32:50Z")

</div>

Hi all,

I'm running a query that looks like the following, only with many Filters:

```
{
  "size": 30,
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "match_all": {}
          }
        }
      },
      "script_score": {
        "script": "( doc['num'].value <= 3 || Math.random() < 1/doc['num'].value ) ? Math.random() : 0"
      },
      "boost_mode": "replace"
    }
  }
}

```

and am seeing run times about 10 times slower than without a Function Score Script, even when using all possible filters.

We are running Elastic version 1.0.0 and using Java API (package org.elasticsearch.client)

Is there any way to achieve a similar result with better performance? The links for "Native Java Scripts" in the tutorial seems broken ☹

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [May 6, 2015, 11:39am UTC](https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274/2 "2015-05-06T11:39:34Z")

</div>

How much slower in absolute millis? How many documents are being scored?  
Have you tried upgrading to a newer version? 1.0 uses mvel as the default  
scripting language and its ancient. Is it faster enough the second time  
the same script is sent? When the query is running do you see groovy or  
mvel in the stack traces when you jstack Elasticsearch? Have you tried the  
expression script engine? Its available in newer versions.

I'd file the native script docs link being broken as a bug.

---

<div class="post-metadata">

**Author:** ![smithallen](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@smithallen](https://discuss.elastic.co/u/smithallen)\
**Post date:** [June 7, 2016, 6:30am UTC](https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274/3 "2016-06-07T06:30:44Z")

</div>

I am using ES 2.3.1 and use Function Score Script(java plugin form) and found the speed is more slower than query without function score.

e.g. query directly took: 46 but query with function\_score took:180.  
how to speed up with function\_score?

---

<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:45pm UTC](https://discuss.elastic.co/t/simple-function-score-query-shows-very-poor-performance/274/4 "2017-07-05T22:45:46Z")

</div>


