|
10 | 10 |
|
11 | 11 | import org.apache.lucene.document.Field; |
12 | 12 | import org.apache.lucene.document.KeywordField; |
13 | | -import org.apache.lucene.document.StringField; |
14 | 13 | import org.apache.lucene.index.DirectoryReader; |
15 | 14 | import org.apache.lucene.index.LeafReaderContext; |
16 | 15 | import org.apache.lucene.index.Term; |
@@ -223,18 +222,27 @@ public void testFielddataLookupTerminatesInLoop() { |
223 | 222 | } |
224 | 223 |
|
225 | 224 | 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( |
228 | 230 | runtimeField("1", leafLookup -> leafLookup.doc().get("2").get(0).toString()), |
229 | 231 | runtimeField("2", leafLookup -> leafLookup.doc().get("3").get(0).toString()), |
230 | 232 | 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")) { |
233 | 235 | return "escape!"; |
234 | 236 | } |
235 | | - return leafLookup.doc().get("4").get(0).toString(); |
| 237 | + return leafLookup.doc().get("4").getFirst().toString(); |
236 | 238 | }) |
237 | 239 | ); |
| 240 | + SearchExecutionContext searchExecutionContext = createSearchExecutionContext( |
| 241 | + "uuid", |
| 242 | + null, |
| 243 | + createMappingLookup(List.of(concreteField), runtimeFields), |
| 244 | + Collections.emptyMap() |
| 245 | + ); |
238 | 246 | List<String> values = collect("1", searchExecutionContext, new TermQuery(new Term("indexed_field", "first"))); |
239 | 247 | assertEquals(List.of("escape!"), values); |
240 | 248 | IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> collect("1", searchExecutionContext)); |
@@ -776,8 +784,8 @@ private static List<String> collect(String field, SearchExecutionContext searchE |
776 | 784 | private static List<String> collect(String field, SearchExecutionContext searchExecutionContext, Query query) throws IOException { |
777 | 785 | List<String> result = new ArrayList<>(); |
778 | 786 | 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))); |
781 | 789 | try (DirectoryReader reader = indexWriter.getReader()) { |
782 | 790 | IndexSearcher searcher = newSearcher(reader); |
783 | 791 | MappedFieldType fieldType = searchExecutionContext.getFieldType(field); |
|
0 commit comments