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

Runtime field doesn't work correctly with range query when emitted value is 0.0 #71786

Closed
josefschiefer27 opened this issue Apr 17, 2021 · 2 comments · Fixed by #71915
Closed
Assignees
Labels
>bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v7.12.0 v7.13.0 v8.0.0-alpha1

Comments

@josefschiefer27
Copy link

Elasticsearch version (bin/elasticsearch --version): 7.12.0

Plugins installed: []

JVM version (java -version): java version "16" 2021-03-16

OS version (uname -a if on a Unix-like system): MacOS Catalina, Version 10.15.7

Description of the problem including expected versus actual behavior:
When emitting the value 0.0 for a runtime time field of type double, a range query with lt (less equal) returns the wrong result.

Steps to reproduce:

Please include a minimal but complete recreation of the problem,
including (e.g.) index creation, mappings, settings, query etc. The easier
you make for us to reproduce it, the more likely that somebody will take the
time to look at it.

  1. Create an index with a runtime field of type double. The runtime field emits always the value 0.0.
 PUT my-index
 {
   "mappings": {
     "runtime": {
       "field1": {
         "type": "double",
         "script": {
           "source": "emit(0.0)"
         }
       }
     }
   }
}
  1. Insert a document to the index
PUT my-index/_doc/1
{}
  1. Search for the document using this range query
GET my-index/_search
{
  "query": {
    "range": {
      "field1": {
        "lt": 0
      }
    }
  }, 
  "docvalue_fields": ["field1"]
}

This range query returns the inserted document which is a wrong result. Please note that the range query condition is <0 - hence this query shouldn't return any documents. I noticed that this problem only occurs when emitting the value 0. Other values (e.g. 1.0, 2.0, 3.0...) work as expected.

@josefschiefer27 josefschiefer27 added >bug needs:triage Requires assignment of a team area label labels Apr 17, 2021
@cbuescher cbuescher self-assigned this Apr 20, 2021
@cbuescher
Copy link
Member

Because of the excluding the upper bound 0.0 the query is translated to an "field1:[-Infinity TO -0.0]" query. The "-0.0" upper value is included because its the next smallest value below 0.0 for the double type. While this is respected on the regular "double" field, the impelentation in DoubleScriptFieldRangeQuery uses a simple Java double comparison (i.e. a <=b) where apparently -0.0 == 0.0. Fortunately we can switch to Double.compare that seems to handle this case correctly. I'll open a PR shortly.

cbuescher pushed a commit to cbuescher/elasticsearch that referenced this issue Apr 20, 2021
DoubleScriptFieldRangeQuery which is used on runtime fields of type "double"
currently uses simple double type comparison for checking its upper and lower
bounds. Unfortunately it seems that -0.0 == 0.0, but when we want to exclude a
0.0 bound via "lt" the generated range query uses -0.0 as its upper bound which
erroneously includes the 0.0 value. We can use `Double.compare` instead which
seems to handle this edge case well.

Closes elastic#71786
@cbuescher cbuescher added :Search/Search Search-related issues that do not fall into other categories and removed needs:triage Requires assignment of a team area label labels Apr 20, 2021
@elasticmachine elasticmachine added the Team:Search Meta label for search team label Apr 20, 2021
@elasticmachine
Copy link
Collaborator

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

cbuescher pushed a commit that referenced this issue Apr 20, 2021
DoubleScriptFieldRangeQuery which is used on runtime fields of type "double"
currently uses simple double type comparison for checking its upper and lower
bounds. Unfortunately it seems that -0.0 == 0.0, but when we want to exclude a
0.0 bound via "lt" the generated range query uses -0.0 as its upper bound which
erroneously includes the 0.0 value. We can use `Double.compare` instead which
seems to handle this edge case well.

Closes #71786
cbuescher pushed a commit that referenced this issue Apr 20, 2021
DoubleScriptFieldRangeQuery which is used on runtime fields of type "double"
currently uses simple double type comparison for checking its upper and lower
bounds. Unfortunately it seems that -0.0 == 0.0, but when we want to exclude a
0.0 bound via "lt" the generated range query uses -0.0 as its upper bound which
erroneously includes the 0.0 value. We can use `Double.compare` instead which
seems to handle this edge case well.

Closes #71786
cbuescher pushed a commit that referenced this issue Apr 20, 2021
DoubleScriptFieldRangeQuery which is used on runtime fields of type "double"
currently uses simple double type comparison for checking its upper and lower
bounds. Unfortunately it seems that -0.0 == 0.0, but when we want to exclude a
0.0 bound via "lt" the generated range query uses -0.0 as its upper bound which
erroneously includes the 0.0 value. We can use `Double.compare` instead which
seems to handle this edge case well.

Closes #71786
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v7.12.0 v7.13.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants