Skip to content

Sorting is incorrect when using script sorting with nested filter and Descending order #98156

Open
@pmakari

Description

@pmakari

Elasticsearch Version

7.17.9

Installed Plugins

No response

Java Version

bundled

OS Version

Mac

Problem Description

When utilizing the script sort builder for nested documents alongside a filter, an issue arises with the sort functionality when the order is set to descending. Seems like that the problem is due to the usage of Double.MAX_VALUE for documents that do not match the filter criteria. To rectify this, a simple solution would be to adjust the default value to Double.MIN_VALUE when the descending order is enabled. By making this adjustment, the sort functionality should work accurately for the nested documents.

Steps to Reproduce

Sample script:

      "_script": {
        "script": {
          "source": "params.weight.getOrDefault(doc['search.simples.sizeFilter.eu'].value, 0)",
          "lang": "painless",
          "params": {
            "weight": {
              "dresses|XL": 5.0,
              "tops|M": 5.0,
              "dresses|L": 5.0
            }
          }
        },
        "type": "number",
        "order": "desc",
        "mode": "sum",
        "nested": {
          "path": "search.simples",
          "filter": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "search.simples.stockStatus": {
                      "value": "AVAILABLE"
                    }
                  }
                }
              ]
            }
          }
        }
      }

Documents which don't match the filter will appear on top because 1.7976931348623157E308 is assigned to them.

Logs (if relevant)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions