Skip to content

[BUG] Calling getMillis() in Painless script is slow #3156

@mbaranczak

Description

@mbaranczak

Describe the bug

We had a sorting script that called getMillis() on a date field. We noticed that the script was unusually slow for queries that matched a large number of results, and determined that getMillis() was the bottleneck. Removing that call resulted in a 10-fold speed-up.

I found this bug report for ElasticSearch 6.5.0, which looks very similar to what I ran into. It says that the bug was fixed; maybe the fix never made it into OpenSearch, maybe the bug came back. elastic/elasticsearch#35754

I had good results with the work-around suggested in that report (replacing getMillis() with toInstant().toEpochMilli()).

To Reproduce

On an index with a datetime field called originalDate, and a large number of documents (a million or more), run this search:

{
  "_source": ["id","originalDate"],
  "sort": {
    "_script": {
      "type": "number",
      "order": "asc",
      "script": {
        "source": "return doc[\"originalDate\"].value.getMillis();"
	  }
	}
  }
}

Host/Environment:

Amazon OpenSearch service v1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Indexing & SearchbenchmarkingIssues related to benchmarking or performance.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions