Elasticsearch script score impact on search performance

Viewed 2319

I'm in progress of optimizing search queries performance and I'm following recommendations from https://www.elastic.co/guide/en/elasticsearch/reference/7.7/tune-for-search-speed.html

Query does the following:

  • Filters by multiple dates fields
  • Optionally filters by category_ids
  • Is wrapped in a function score, where one of the functions is a script score

One of the cheapest optimizations suggested is rounding dates to improve query caching. I've rounded time down to minutes at application level.

Another cheap optimization was mapping identifiers as keywords

I've tried both and none of them made a significant difference. I've observed application performance metrics, query slow logs, the difference was negligible.

Mapping identifiers as keywords turned out to be even slower, however I've also ran a test where I eliminated all the functions, reran all the queries and keyword identifiers were outperforming numeric identifiers.

The very same article suggests avoiding scripts, which I'll be doing next.

Given the case when keyword identifiers were doing better than numeric identifiers without functions and doing worse with functions is suspicious and I cannot explain that.

So in what way script score (function_score) impacts other queries performance?

This is a trimmed query version:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "created_at": {
                  "gte": "2020-06-26T17:22:00"
                }
              }
            },
            {
              "terms": {
                "catalog_ids": [4, 178, 222, 532, 1078, 1131]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "1 / ln(now - doc['created_at'].value + 1)",
              "lang": "expression",
              "params": { "now": 1593184920000 }
            }
          }
        },
        {
          "filter": {
            "range": {
              "boost_until": {
                "gte": "2020-06-26T17:22:00"
              }
            }
          },
          "weight": 15.15
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "sum"
    }
  }
}

Query duration differences with/without function score: With function score Without function score

These are all tests from a single-node cluster with 5M documents. Queries are taken from slow query log.

0 Answers
Related