-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Adding a null pointer check to fix index_prefix query #2879
Conversation
For index
For index
For index
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are faster than me :)
Thanks for taking care of this.
I think the right fix is to take care of the acceptance check.
Also could you add tests to make sure we dont break it in the future?
@@ -583,7 +583,9 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, bool | |||
} | |||
Automaton automaton = Operations.concatenate(automata); | |||
AutomatonQuery query = new AutomatonQuery(new Term(name(), value + "*"), automaton); | |||
query.setRewriteMethod(method); | |||
if (method != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach solves the problem but the real root cause is the check for min, max chars.
The right fix here is to not accept() the query if the len is less than min_chars
.
This makes sure the accept fails at: Line-762 and doesnt really need to call: Line-765
if (method != null) { | |
boolean accept(int length) { | |
return length >= minChars && length <= maxChars; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I didn't know we should not be accepting the value less than minChars. I will make that change and add tests as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also take a look at: dd540ef.
There might be other cases which might fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the support for minChars - 1 was specifically added. Would the fix in this PR then be the solution since it changes the behavior if we don't accept the minChars - 1 length?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the fix the original commit put in lead to this problem. My hunch says this should be fixed.
Could you read through the commit and understand what other problems we will see?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the commit, the operation for the index_prefix query for length = minChars-1 is remapped to a? from a* to make it less expensive. Also, looking at the current code base, I see that the setRewriteMethod is wrapped by a null check at other places: for example https://github.com/opensearch-project/OpenSearch/blame/3c5d997a765e24ffa32d35219fd5026cfb143a9d/server/src/main/java/org/opensearch/index/mapper/StringFieldType.java#L116.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Vacha for this.
I take back my suggestion, as its probably not supporting the optimization in performance.
I agree with you that, verifying the method being null
solves the problem while being efficient.
Could you add some tests and we should be good to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is query.setRewriteMethod(null)
a valid thing by itself? Can the rewrite method be null
? If not throw in an assert
or something like that to prevent hiding other errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Gradle Check success b6b9d3561c9869ff26326526b217d1fdab4e3a00 |
Signed-off-by: Vacha Shah <vachshah@amazon.com>
Signed-off-by: Vacha Shah <vachshah@amazon.com>
Looks good to me. Thanks @VachaShah ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine approving and merging this PR to fix the immediate bug but we should open a follow-up PR for #2896. Let us know if you'd like to take care of that as well @VachaShah
@@ -583,7 +583,9 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, bool | |||
} | |||
Automaton automaton = Operations.concatenate(automata); | |||
AutomatonQuery query = new AutomatonQuery(new Term(name(), value + "*"), automaton); | |||
query.setRewriteMethod(method); | |||
if (method != null) { | |||
query.setRewriteMethod(method); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query.setRewriteMethod
is removed in Lucene 9.2. I've opened an issue to remove all references to the method and use CONSTANT_SCORE_REWRITE
in places where null
is passed in as the method
parameter.
Thanks @nknize! Sure I can take care of that. |
* Adding a null pointer check to fix index_prefix query Signed-off-by: Vacha Shah <vachshah@amazon.com> * Adding test Signed-off-by: Vacha Shah <vachshah@amazon.com> (cherry picked from commit 452e368)
* Adding a null pointer check to fix index_prefix query Signed-off-by: Vacha Shah <vachshah@amazon.com> * Adding test Signed-off-by: Vacha Shah <vachshah@amazon.com> (cherry picked from commit 452e368)
* Adding a null pointer check to fix index_prefix query Signed-off-by: Vacha Shah <vachshah@amazon.com> * Adding test Signed-off-by: Vacha Shah <vachshah@amazon.com> (cherry picked from commit 452e368) Co-authored-by: Vacha Shah <vachshah@amazon.com>
* Adding a null pointer check to fix index_prefix query Signed-off-by: Vacha Shah <vachshah@amazon.com> * Adding test Signed-off-by: Vacha Shah <vachshah@amazon.com> (cherry picked from commit 452e368) Co-authored-by: Vacha Shah <vachshah@amazon.com>
Signed-off-by: Vacha Shah vachshah@amazon.com
Description
When the length of the query prefix is = minChars-1, the code follows the path in TextFieldMapper and throws an NPE here. Adding a null check for the method, the query returns a result without an NPE now:
Tested with various scenarios mentioned in the issue. Adding the details in the comments of this PR.
Issues Resolved
#2826
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.