Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort on date fields with missing values (#73763) #116099

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

hanjongho
Copy link

@hanjongho hanjongho commented Nov 1, 2024

hi @cbuescher, @jimczi
I'm trying to find and share the error in the logic of sort with missing, order method together in the date field.

while using .missing("_last").sort(SortOrder.DESC) in date field (doc does not have a date field),
sort value return -9223372036854775808L by the following logic
image

can simulate this case with this order

1. Put Index Settings

curl -X PUT "localhost:9200/my-index-000001" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "date_nanos_test": {
        "type": "date_nanos"
      },
      "date_test": {
        "type": "date"
      }
    }
  }
}
'

2. Bulk 2 documents that has date_nanos, date type each

curl -X PUT "localhost:9200/my-index-000001/_bulk?refresh" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"date_test":"2024-11-01"}
{"index":{"_id":"2"}}
{"date_nanos_test":"2024-11-01T23:50:00.000000001"}
'

3. Query with .missing("_last").sort(SortOrder.DESC) in date_nanos field

curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 5,
  "sort": [
    {
      "date_nanos_test": {
        "order": "desc",
        "missing": "_last"
      }
    }
  ]
}
'

3.1 Actual Result

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "date_nanos_test" : "2024-11-01T23:50:00.000000001"
        },
        "sort" : [
          1730505000000000001
        ]
      },
      {
        "_index" : "my-index-000001",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "date_test" : "2024-11-01"
        },
        "sort" : [
          0
        ]
      }
    ]
  }
}

this works well, fixed in #74760

4. Query with .missing("_last").sort(SortOrder.DESC) in date field

curl -X GET "localhost:9200/my-index-000001/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 5,
  "sort": [
    {
      "date_test": {
        "order": "desc",
        "missing": "_last"
      }
    }
  ]
}
'

4.1 Actual Result

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "date_test" : "2024-11-01"
        },
        "sort" : [
          1730419200000
        ]
      },
      {
        "_index" : "my-index-000001",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "date_nanos_test" : "2024-11-01T23:50:00.000000001"
        },
        "sort" : [
          -9223372036854775808
        ]
      }
    ]
  }
}

Suggest

when I searched for the issues related to this error, I found #73763, which is the same issue about date_nanos.
I checked the PR #74760 (commited by @cbuescher) for the issue. when I added the date type to the logic (PR) and tested it in my local, 0L responded well normally. and when I tested it with order asc, needed Long.MAX_VALUE, elastic search's specification.
image

please understand if there is something wrong in my explanation. review it when you have time.
thank you 👍

@elasticsearchmachine elasticsearchmachine added v9.0.0 external-contributor Pull request authored by a developer outside the Elasticsearch team needs:triage Requires assignment of a team area label labels Nov 1, 2024
@hanjongho hanjongho changed the title Fix sort on date fields with missing values Fix sort on date fields with missing values #73763 Nov 1, 2024
@hanjongho hanjongho changed the title Fix sort on date fields with missing values #73763 Fix sort on date fields with missing values (#73763) Nov 1, 2024
@gbanasiak gbanasiak added the :Search/Search Search-related issues that do not fall into other categories label Nov 22, 2024
@elasticsearchmachine elasticsearchmachine added Team:Search Meta label for search team and removed needs:triage Requires assignment of a team area label labels Nov 22, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@benwtrent benwtrent added :Search Foundations/Search Catch all for Search Foundations and removed :Search/Search Search-related issues that do not fall into other categories labels Nov 22, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Nov 22, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

@elasticsearchmachine elasticsearchmachine removed the Team:Search Meta label for search team label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external-contributor Pull request authored by a developer outside the Elasticsearch team :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants