Skip to content

Commit 5ec5253

Browse files
chrisparrinelloKubik42
authored andcommitted
Fix SearchExecutionContextTests.testFielddataLookupSomtimesLoop (elastic#137595)
Closes elastic#136766. Make the testFielddataLookupSomtimesLoop test not depend on docID to make decision about whether or not the break the cycle in runtime fields for test purposes.
1 parent f98fe98 commit 5ec5253

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import org.apache.lucene.document.Field;
1212
import org.apache.lucene.document.KeywordField;
13-
import org.apache.lucene.document.StringField;
1413
import org.apache.lucene.index.DirectoryReader;
1514
import org.apache.lucene.index.LeafReaderContext;
1615
import org.apache.lucene.index.Term;
@@ -223,18 +222,27 @@ public void testFielddataLookupTerminatesInLoop() {
223222
}
224223

225224
public void testFielddataLookupSometimesLoop() throws IOException {
226-
SearchExecutionContext searchExecutionContext = createSearchExecutionContext(
227-
// simulate a runtime field cycle in the second doc: 1: doc['2'] 2: doc['3'] 3: doc['4'] 4: doc['4']
225+
// create this field so we can use it to make sure we're escaping the loop on only the "first" document
226+
var concreteField = new KeywordFieldMapper.KeywordFieldType("indexed_field", true, true, Collections.emptyMap());
227+
228+
// simulate a runtime field cycle in the second doc: 1: doc['2'] 2: doc['3'] 3: doc['4'] 4: doc['4']
229+
var runtimeFields = List.of(
228230
runtimeField("1", leafLookup -> leafLookup.doc().get("2").get(0).toString()),
229231
runtimeField("2", leafLookup -> leafLookup.doc().get("3").get(0).toString()),
230232
runtimeField("3", leafLookup -> leafLookup.doc().get("4").get(0).toString()),
231-
runtimeField("4", (leafLookup, docId) -> {
232-
if (docId == 0) {
233+
runtimeField("4", leafLookup -> {
234+
if (leafLookup.doc().get("indexed_field").getFirst().equals("first")) {
233235
return "escape!";
234236
}
235-
return leafLookup.doc().get("4").get(0).toString();
237+
return leafLookup.doc().get("4").getFirst().toString();
236238
})
237239
);
240+
SearchExecutionContext searchExecutionContext = createSearchExecutionContext(
241+
"uuid",
242+
null,
243+
createMappingLookup(List.of(concreteField), runtimeFields),
244+
Collections.emptyMap()
245+
);
238246
List<String> values = collect("1", searchExecutionContext, new TermQuery(new Term("indexed_field", "first")));
239247
assertEquals(List.of("escape!"), values);
240248
IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> collect("1", searchExecutionContext));
@@ -776,8 +784,8 @@ private static List<String> collect(String field, SearchExecutionContext searchE
776784
private static List<String> collect(String field, SearchExecutionContext searchExecutionContext, Query query) throws IOException {
777785
List<String> result = new ArrayList<>();
778786
try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
779-
indexWriter.addDocument(List.of(new StringField("indexed_field", "first", Field.Store.NO)));
780-
indexWriter.addDocument(List.of(new StringField("indexed_field", "second", Field.Store.NO)));
787+
indexWriter.addDocument(List.of(new KeywordField("indexed_field", "first", Field.Store.YES)));
788+
indexWriter.addDocument(List.of(new KeywordField("indexed_field", "second", Field.Store.YES)));
781789
try (DirectoryReader reader = indexWriter.getReader()) {
782790
IndexSearcher searcher = newSearcher(reader);
783791
MappedFieldType fieldType = searchExecutionContext.getFieldType(field);

0 commit comments

Comments
 (0)