Skip to content

Sort by VERSION value behaves incorrectly when sort value returned by painless script #85989

@luigidellaquila

Description

@luigidellaquila

When a query sort is defined on a script that returns a VERSION value (https://www.elastic.co/guide/en/elasticsearch/reference/current/version.html), the result is sorted using lexicographic order instead of SemVer

Reproducer

PUT /test

{
  "mappings": {
    "properties": {
      "version": {
        "type": "version" 
      }
    }
  }
}

POST /_bulk

{"index": {"_index":"test"}}
{"name" : "foo", "version":"1.11.3"}
{"index": {"_index":"test"}}
{"name" : "foo", "version":"1.1.4"}
{"index": {"_index":"test"}}
{"name" : "bar", "version":"1.2"}
POST /test/_search

{
    "size": 1000,
    "_source": false,
    "fields": [
        {
            "field": "name"
        },
        {
            "field": "version"
        }
    ],
    "sort": [
        {
            "_script": {
                "script": {
                    "lang": "painless",
                    "source": "doc['version'].value"
                },
                "type": "string"
            }
        }
    ]
}

Also the following does not seem to be an option:

"sort": [
        {
            "_script": {
                "script": {
                    "lang": "painless",
                    "source":"$('version', new Version('0'))"
                },
                "type": "string"
            }
        }
    ]

since it tries to cast Version to String (and fails of course)


Labeling as Core/Infra/Scripting, but not sure if it's for Search instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Core/Infra/ScriptingScripting abstractions, Painless, and MustacheTeam:Core/InfraMeta label for core/infra team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions