Skip to content

Commit b2de161

Browse files
committed
Refactor tests
1 parent 6755ab2 commit b2de161

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryEvaluatorTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.lucene.util.BytesRef;
2626
import org.elasticsearch.compute.OperatorTests;
2727
import org.elasticsearch.compute.data.BlockFactory;
28+
import org.elasticsearch.compute.data.BytesRefBlock;
29+
import org.elasticsearch.compute.data.BytesRefVector;
2830
import org.elasticsearch.compute.data.DocBlock;
2931
import org.elasticsearch.compute.data.DoubleBlock;
3032
import org.elasticsearch.compute.data.ElementType;
@@ -50,6 +52,7 @@
5052
import java.util.TreeSet;
5153

5254
import static org.elasticsearch.compute.test.OperatorTestCase.randomPageSize;
55+
import static org.hamcrest.Matchers.equalTo;
5356

5457
/**
5558
* Base class for testing Lucene query evaluators.
@@ -149,7 +152,26 @@ private void testTermsQuery(boolean shuffleDocs) throws IOException {
149152
assertTermsQuery(results, matching, expectedMatchCount);
150153
}
151154

152-
protected abstract void assertTermsQuery(List<Page> results, Set<String> matching, int expectedMatchCount);
155+
protected void assertTermsQuery(List<Page> results, Set<String> matching, int expectedMatchCount) {
156+
int matchCount = 0;
157+
for (Page page : results) {
158+
int initialBlockIndex = termsBlockIndex(page);
159+
BytesRefVector terms = page.<BytesRefBlock>getBlock(initialBlockIndex).asVector();
160+
@SuppressWarnings("unchecked")
161+
T resultVector = (T) page.getBlock(resultsBlockIndex(page)).asVector();
162+
for (int i = 0; i < page.getPositionCount(); i++) {
163+
BytesRef termAtPosition = terms.getBytesRef(i, new BytesRef());
164+
boolean isMatch = matching.contains(termAtPosition.utf8ToString());
165+
assertResultMatch(resultVector, i, isMatch);
166+
if (isMatch) {
167+
matchCount++;
168+
}
169+
}
170+
}
171+
assertThat(matchCount, equalTo(expectedMatchCount));
172+
}
173+
174+
protected abstract void assertResultMatch(T resultVector, int position, boolean isMatch);
153175

154176
private List<Page> runQuery(Set<String> values, Query query, boolean shuffleDocs) throws IOException {
155177
DriverContext driverContext = driverContext();

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryExpressionEvaluatorTests.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,7 @@ protected int resultsBlockIndex(Page page) {
7878
}
7979

8080
@Override
81-
protected void assertTermsQuery(List<Page> results, Set<String> matching, int expectedMatchCount) {
82-
int matchCount = 0;
83-
for (Page page : results) {
84-
int initialBlockIndex = termsBlockIndex(page);
85-
BytesRefVector terms = page.<BytesRefBlock>getBlock(initialBlockIndex).asVector();
86-
BooleanVector matches = page.<BooleanBlock>getBlock(initialBlockIndex + 1).asVector();
87-
for (int i = 0; i < page.getPositionCount(); i++) {
88-
BytesRef termAtPosition = terms.getBytesRef(i, new BytesRef());
89-
assertThat(matches.getBoolean(i), equalTo(matching.contains(termAtPosition.utf8ToString())));
90-
if (matches.getBoolean(i)) {
91-
matchCount++;
92-
}
93-
}
94-
}
95-
assertThat(matchCount, equalTo(expectedMatchCount));
81+
protected void assertResultMatch(BooleanVector resultVector, int position, boolean isMatch) {
82+
assertThat(resultVector.getBoolean(position), equalTo(isMatch));
9683
}
9784
}

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/LuceneQueryScoreEvaluatorTests.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88
package org.elasticsearch.compute.lucene;
99

1010
import org.apache.lucene.search.Scorable;
11-
import org.apache.lucene.util.BytesRef;
1211
import org.elasticsearch.compute.data.BlockFactory;
13-
import org.elasticsearch.compute.data.BytesRefBlock;
14-
import org.elasticsearch.compute.data.BytesRefVector;
1512
import org.elasticsearch.compute.data.DoubleVector;
1613
import org.elasticsearch.compute.data.Page;
1714
import org.elasticsearch.compute.operator.Operator;
1815
import org.elasticsearch.compute.operator.ScoreOperator;
1916

2017
import java.io.IOException;
21-
import java.util.List;
22-
import java.util.Set;
2318

2419
import static org.elasticsearch.compute.lucene.LuceneQueryScoreEvaluator.NO_MATCH_SCORE;
2520
import static org.hamcrest.Matchers.equalTo;
@@ -82,23 +77,12 @@ protected int resultsBlockIndex(Page page) {
8277
}
8378

8479
@Override
85-
protected void assertTermsQuery(List<Page> results, Set<String> matching, int expectedMatchCount) {
86-
int matchCount = 0;
87-
for (Page page : results) {
88-
int initialBlockIndex = termsBlockIndex(page);
89-
BytesRefVector terms = page.<BytesRefBlock>getBlock(initialBlockIndex).asVector();
90-
DoubleVector matches = (DoubleVector) page.getBlock(resultsBlockIndex(page)).asVector();
91-
for (int i = 0; i < page.getPositionCount(); i++) {
92-
BytesRef termAtPosition = terms.getBytesRef(i, new BytesRef());
93-
if (matching.contains(termAtPosition.utf8ToString())) {
94-
assertThat(matches.getDouble(i), greaterThan((double) TEST_SCORE));
95-
matchCount++;
96-
} else {
97-
// Default score, as Lucene docs gets retrieved with a implicit score
98-
assertThat(matches.getDouble(i), equalTo(DEFAULT_SCORE));
99-
}
100-
}
80+
protected void assertResultMatch(DoubleVector resultVector, int position, boolean isMatch) {
81+
if (isMatch) {
82+
assertThat(resultVector.getDouble(position), greaterThan(DEFAULT_SCORE));
83+
} else {
84+
// All docs have a default score coming from Lucene
85+
assertThat(resultVector.getDouble(position), equalTo(DEFAULT_SCORE));
10186
}
102-
assertThat(matchCount, equalTo(expectedMatchCount));
10387
}
10488
}

0 commit comments

Comments
 (0)