Simple Function Score Query Shows Very Poor Performance

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 :frowning:

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.

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?

1 Like