-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Remaining queries for long script field #59816
Merged
nik9000
merged 4 commits into
elastic:feature/runtime_fields
from
nik9000:feature/runtime_fields
Jul 20, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../src/main/java/org/elasticsearch/xpack/runtimefields/query/LongScriptFieldRangeQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.query; | ||
|
||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.xpack.runtimefields.LongScriptFieldScript; | ||
|
||
import java.util.Objects; | ||
|
||
public class LongScriptFieldRangeQuery extends AbstractLongScriptFieldQuery { | ||
private final long lowerValue; | ||
private final long upperValue; | ||
|
||
public LongScriptFieldRangeQuery( | ||
Script script, | ||
LongScriptFieldScript.LeafFactory leafFactory, | ||
String fieldName, | ||
long lowerValue, | ||
long upperValue | ||
) { | ||
super(script, leafFactory, fieldName); | ||
this.lowerValue = lowerValue; | ||
this.upperValue = upperValue; | ||
assert lowerValue <= upperValue; | ||
} | ||
|
||
@Override | ||
protected boolean matches(long[] values, int count) { | ||
for (int i = 0; i < count; i++) { | ||
if (lowerValue <= values[i] && values[i] <= upperValue) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public final String toString(String field) { | ||
StringBuilder b = new StringBuilder(); | ||
if (false == fieldName().contentEquals(field)) { | ||
b.append(fieldName()).append(':'); | ||
} | ||
b.append('[').append(lowerValue).append(" TO ").append(upperValue).append(']'); | ||
return b.toString(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), lowerValue, upperValue); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (false == super.equals(obj)) { | ||
return false; | ||
} | ||
LongScriptFieldRangeQuery other = (LongScriptFieldRangeQuery) obj; | ||
return lowerValue == other.lowerValue && upperValue == other.upperValue; | ||
} | ||
|
||
long lowerValue() { | ||
return lowerValue; | ||
} | ||
|
||
long upperValue() { | ||
return upperValue; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
.../src/main/java/org/elasticsearch/xpack/runtimefields/query/LongScriptFieldTermsQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.query; | ||
|
||
import com.carrotsearch.hppc.LongSet; | ||
|
||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.xpack.runtimefields.LongScriptFieldScript; | ||
|
||
import java.util.Objects; | ||
|
||
public class LongScriptFieldTermsQuery extends AbstractLongScriptFieldQuery { | ||
private final LongSet terms; | ||
|
||
public LongScriptFieldTermsQuery(Script script, LongScriptFieldScript.LeafFactory leafFactory, String fieldName, LongSet terms) { | ||
super(script, leafFactory, fieldName); | ||
this.terms = terms; | ||
} | ||
|
||
@Override | ||
protected boolean matches(long[] values, int count) { | ||
for (int i = 0; i < count; i++) { | ||
if (terms.contains(values[i])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public final String toString(String field) { | ||
if (fieldName().contentEquals(field)) { | ||
return terms.toString(); | ||
} | ||
return fieldName() + ":" + terms; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), terms); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (false == super.equals(obj)) { | ||
return false; | ||
} | ||
LongScriptFieldTermsQuery other = (LongScriptFieldTermsQuery) obj; | ||
return terms.equals(other.terms); | ||
} | ||
|
||
LongSet terms() { | ||
return terms; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
functions are cool, but couldn't we have a shared method that does the boiler-plate range conversion, which is called when creating the lucene query?
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.
potentially, that could even be pushed upstream? I would love to not have changes to this mapper as part of the feature branch, if possible.
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 Elasticsearch is mostly responsible for the
Object
tolong
stuff. I don't think that is something Lucene wants but I could certainly be wrong.I could copy and paste the code but I think that'd be dangerous as it'd get out of sync if someone made a chance to the mapper.
I think that is what this is. I could probably write it in a way that doesn't use the functional interface if you'd prefer, but it'll be a little more jumbled. It might still be more clear just because we avoid the indirection.
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.
maybe I was not too careful looking, but it looked like you were calling the function last in the method, which made me think that you could rather have one method to convert the ranges, though what is the return type of that method going to be? Maybe I start to see how a function simplifies it. I don't have a strong opinion. Maybe make it a BiFunction ?
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've pushed a
BiFunction
. Do you think that is better?