-
Notifications
You must be signed in to change notification settings - Fork 25.3k
EQL: Use the implicit tiebreaker sort value in the search queries #72089
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5961f6
Use the implicit tiebreaker sort value in the next request's search_a…
astefan 3f2f42f
Merge branch 'master' into eql_pit_tiebreaker
elasticmachine dbedee4
Address reviews
astefan 58c255e
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
astefan 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 hidden or 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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
...rg/elasticsearch/xpack/eql/execution/search/extractor/ImplicitTiebreakerHitExtractor.java
This file contains hidden or 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,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
package org.elasticsearch.xpack.eql.execution.search.extractor; | ||
|
||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.xpack.eql.EqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.ql.execution.search.extractor.HitExtractor; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Returns the implicit Elasticsearch tiebreaker value associated with a PIT request for every search hit against which it is run. | ||
*/ | ||
public class ImplicitTiebreakerHitExtractor implements HitExtractor { | ||
|
||
public static final HitExtractor INSTANCE = new ImplicitTiebreakerHitExtractor(); | ||
static final String NAME = "tb"; | ||
|
||
private ImplicitTiebreakerHitExtractor() {} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { } | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Object extract(SearchHit hit) { | ||
Object[] sortValues = hit.getRawSortValues(); | ||
if (sortValues.length == 0) { | ||
throw new EqlIllegalArgumentException("Expected at least one sorting value in the search hit, but got none"); | ||
} | ||
return sortValues[sortValues.length - 1]; | ||
} | ||
|
||
@Override | ||
public String hitName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null || obj.getClass() != getClass()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return ImplicitTiebreakerHitExtractor.class.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "_shard_doc"; | ||
} | ||
} |
This file contains hidden or 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
123 changes: 123 additions & 0 deletions
123
...rc/test/java/org/elasticsearch/xpack/eql/execution/assembler/ImplicitTiebreakerTests.java
This file contains hidden or 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,123 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.execution.assembler; | ||
|
||
import org.apache.lucene.search.TotalHits; | ||
import org.apache.lucene.search.TotalHits.Relation; | ||
import org.elasticsearch.ExceptionsHelper; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.action.search.SearchResponse.Clusters; | ||
import org.elasticsearch.action.search.SearchResponseSections; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.elasticsearch.search.SearchHits; | ||
import org.elasticsearch.search.SearchSortValues; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.eql.execution.assembler.SequenceSpecTests.TimestampExtractor; | ||
import org.elasticsearch.xpack.eql.execution.search.HitReference; | ||
import org.elasticsearch.xpack.eql.execution.search.QueryClient; | ||
import org.elasticsearch.xpack.eql.execution.search.QueryRequest; | ||
import org.elasticsearch.xpack.eql.execution.search.extractor.ImplicitTiebreakerHitExtractor; | ||
import org.elasticsearch.xpack.eql.execution.sequence.SequenceMatcher; | ||
import org.elasticsearch.xpack.eql.execution.sequence.TumblingWindow; | ||
import org.elasticsearch.xpack.ql.execution.search.extractor.HitExtractor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static org.elasticsearch.action.ActionListener.wrap; | ||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; | ||
|
||
public class ImplicitTiebreakerTests extends ESTestCase { | ||
|
||
private final List<HitExtractor> keyExtractors = emptyList(); | ||
private final HitExtractor tsExtractor = TimestampExtractor.INSTANCE; | ||
private final HitExtractor tbExtractor = null; | ||
private final HitExtractor implicitTbExtractor = ImplicitTiebreakerHitExtractor.INSTANCE; | ||
private final List<Long> implicitTiebreakerValues; | ||
private final int stages = randomIntBetween(3, 10); | ||
|
||
public ImplicitTiebreakerTests() { | ||
this.implicitTiebreakerValues = new ArrayList<>(stages); | ||
for (int i = 0; i < stages; i++) { | ||
implicitTiebreakerValues.add(randomLong()); | ||
} | ||
} | ||
|
||
class TestQueryClient implements QueryClient { | ||
|
||
@Override | ||
public void query(QueryRequest r, ActionListener<SearchResponse> l) { | ||
int ordinal = r.searchSource().terminateAfter(); | ||
if (ordinal > 0) { | ||
int previous = ordinal - 1; | ||
// except the first request, the rest should have the previous response's search_after _shard_doc value | ||
assertArrayEquals("Elements at stage " + ordinal + " do not match", | ||
r.searchSource().searchAfter(), new Object[] { (long) previous, implicitTiebreakerValues.get(previous) }); | ||
} | ||
|
||
long sortValue = implicitTiebreakerValues.get(ordinal); | ||
SearchHit searchHit = new SearchHit(ordinal, String.valueOf(ordinal), null, null); | ||
searchHit.sortValues(new SearchSortValues( | ||
new Long[] { (long) ordinal, sortValue }, | ||
new DocValueFormat[] { DocValueFormat.RAW, DocValueFormat.RAW })); | ||
SearchHits searchHits = new SearchHits(new SearchHit[] { searchHit }, new TotalHits(1, Relation.EQUAL_TO), 0.0f); | ||
SearchResponseSections internal = new SearchResponseSections(searchHits, null, null, false, false, null, 0); | ||
SearchResponse s = new SearchResponse(internal, null, 0, 1, 0, 0, null, Clusters.EMPTY); | ||
l.onResponse(s); | ||
} | ||
|
||
@Override | ||
public void fetchHits(Iterable<List<HitReference>> refs, ActionListener<List<List<SearchHit>>> listener) { | ||
List<List<SearchHit>> searchHits = new ArrayList<>(); | ||
for (List<HitReference> ref : refs) { | ||
List<SearchHit> hits = new ArrayList<>(ref.size()); | ||
for (HitReference hitRef : ref) { | ||
hits.add(new SearchHit(-1, hitRef.id(), null, null)); | ||
} | ||
searchHits.add(hits); | ||
} | ||
listener.onResponse(searchHits); | ||
} | ||
} | ||
|
||
public void testImplicitTiebreakerBeingSet() { | ||
QueryClient client = new TestQueryClient(); | ||
List<Criterion<BoxedQueryRequest>> criteria = new ArrayList<>(stages); | ||
boolean descending = randomBoolean(); | ||
boolean criteriaDescending = descending; | ||
|
||
for (int i = 0; i < stages; i++) { | ||
final int j = i; | ||
criteria.add(new Criterion<BoxedQueryRequest>(i, | ||
new BoxedQueryRequest(() -> SearchSourceBuilder.searchSource() | ||
.size(10) | ||
.query(matchAllQuery()) | ||
.terminateAfter(j), "@timestamp", emptyList()), | ||
keyExtractors, | ||
tsExtractor, | ||
tbExtractor, | ||
implicitTbExtractor, | ||
criteriaDescending)); | ||
// for DESC (TAIL) sequences only the first criterion is descending the rest are ASC, so flip it after the first query | ||
if (criteriaDescending && i == 0) { | ||
criteriaDescending = false; | ||
} | ||
} | ||
|
||
SequenceMatcher matcher = new SequenceMatcher(stages, descending, TimeValue.MINUS_ONE, null); | ||
TumblingWindow window = new TumblingWindow(client, criteria, null, matcher); | ||
window.execute(wrap(p -> {}, ex -> { | ||
throw ExceptionsHelper.convertToRuntime(ex); | ||
})); | ||
} | ||
} |
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.
Regarding the error message, could the value be a non-long integer?
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.
@bpintea no. Always
long
.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.
Would then the instance check agains a
Long
be also appropriate? (just FMI, its prolly slightly safer the way it is now)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'd leave it as is, for the reason you mentioned but also to be consistent with the approach throughout that method.