Skip to content

Commit 00b6e5e

Browse files
committed
Add test for fetch and highlight phase for derived fields
Signed-off-by: Rishabh Maurya <rishabhmaurya05@gmail.com>
1 parent 641fc5b commit 00b6e5e

File tree

6 files changed

+382
-83
lines changed

6 files changed

+382
-83
lines changed

server/src/main/java/org/opensearch/index/mapper/DerivedFieldSupportedTypes.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* it is used to create an IndexableField for the provided type and object. It is useful when indexing into
3737
* lucene MemoryIndex in {@link org.opensearch.index.query.DerivedFieldQuery}.
3838
*/
39-
enum DerivedFieldSupportedTypes {
39+
public enum DerivedFieldSupportedTypes {
4040

4141
BOOLEAN("boolean", (name, context) -> {
4242
BooleanFieldMapper.Builder builder = new BooleanFieldMapper.Builder(name);

server/src/main/java/org/opensearch/index/mapper/DerivedFieldType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public DerivedFieldValueFetcher valueFetcher(QueryShardContext context, SearchLo
8383
if (format != null) {
8484
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
8585
}
86-
return new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context, searchLookup));
86+
return new DerivedFieldValueFetcher(getDerivedFieldLeafFactory(context, searchLookup == null ? context.lookup() : searchLookup));
8787
}
8888

8989
@Override

server/src/main/java/org/opensearch/index/query/DerivedFieldQuery.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,12 @@ public boolean equals(Object o) {
127127
return false;
128128
}
129129
DerivedFieldQuery other = (DerivedFieldQuery) o;
130-
return Objects.equals(this.query, other.query)
131-
&& Objects.equals(this.valueFetcher, other.valueFetcher)
132-
&& Objects.equals(this.searchLookup, other.searchLookup)
133-
&& Objects.equals(this.indexableFieldGenerator, other.indexableFieldGenerator)
134-
&& Objects.equals(this.indexAnalyzer, other.indexAnalyzer);
130+
return Objects.equals(this.query, other.query) && Objects.equals(this.indexAnalyzer, other.indexAnalyzer);
135131
}
136132

137133
@Override
138134
public int hashCode() {
139-
return Objects.hash(classHash(), query, valueFetcher, searchLookup, indexableFieldGenerator, indexableFieldGenerator);
135+
return Objects.hash(classHash(), query, indexAnalyzer);
140136
}
141137

142138
@Override

server/src/main/java/org/opensearch/index/query/QueryShardContext.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.opensearch.common.TriFunction;
4646
import org.opensearch.common.annotation.PublicApi;
4747
import org.opensearch.common.lucene.search.Queries;
48+
import org.opensearch.common.regex.Regex;
4849
import org.opensearch.common.util.BigArrays;
4950
import org.opensearch.core.action.ActionListener;
5051
import org.opensearch.core.common.ParsingException;
@@ -332,7 +333,15 @@ public Map<String, Query> copyNamedQueries() {
332333
* type then the fields will be returned with a type prefix.
333334
*/
334335
public Set<String> simpleMatchToIndexNames(String pattern) {
335-
return mapperService.simpleMatchToFullName(pattern);
336+
Set<String> matchingFields = mapperService.simpleMatchToFullName(pattern);
337+
if (derivedFieldTypeMap != null && !derivedFieldTypeMap.isEmpty()) {
338+
for (String fieldName : derivedFieldTypeMap.keySet()) {
339+
if (Regex.simpleMatch(pattern, fieldName)) {
340+
matchingFields.add(fieldName);
341+
}
342+
}
343+
}
344+
return matchingFields;
336345
}
337346

338347
/**

server/src/test/java/org/opensearch/search/fetch/subphase/FieldFetcherTests.java

+2-74
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,20 @@
4343
import org.opensearch.index.IndexSettings;
4444
import org.opensearch.index.mapper.MapperService;
4545
import org.opensearch.index.query.QueryShardContext;
46-
import org.opensearch.script.MockScriptEngine;
47-
import org.opensearch.script.ScriptEngine;
48-
import org.opensearch.script.ScriptModule;
49-
import org.opensearch.script.ScriptService;
50-
import org.opensearch.search.lookup.LeafSearchLookup;
51-
import org.opensearch.search.lookup.SearchLookup;
5246
import org.opensearch.search.lookup.SourceLookup;
5347
import org.opensearch.test.OpenSearchSingleNodeTestCase;
5448

5549
import java.io.IOException;
56-
import java.util.Collections;
5750
import java.util.List;
5851
import java.util.Map;
5952
import java.util.Set;
6053

61-
import static java.util.Collections.singletonMap;
6254
import static org.hamcrest.Matchers.containsInAnyOrder;
6355
import static org.hamcrest.Matchers.equalTo;
6456
import static org.hamcrest.Matchers.hasItems;
65-
import static org.mockito.ArgumentMatchers.any;
66-
import static org.mockito.Mockito.mock;
67-
import static org.mockito.Mockito.when;
6857

6958
public class FieldFetcherTests extends OpenSearchSingleNodeTestCase {
7059

71-
private static String DERIVED_FIELD_SCRIPT_1 = "derived_field_script_1";
72-
private static String DERIVED_FIELD_SCRIPT_2 = "derived_field_script_2";
73-
7460
public void testLeafValues() throws IOException {
7561
MapperService mapperService = createMapperService();
7662
XContentBuilder source = XContentFactory.jsonBuilder()
@@ -449,45 +435,6 @@ public void testTextSubFields() throws IOException {
449435
}
450436
}
451437

452-
public void testDerivedFields() throws IOException {
453-
XContentBuilder mapping = XContentFactory.jsonBuilder()
454-
.startObject()
455-
.startObject("derived")
456-
.startObject("derived_1")
457-
.field("type", "keyword")
458-
.startObject("script")
459-
.field("source", DERIVED_FIELD_SCRIPT_1)
460-
.field("lang", "mockscript")
461-
.endObject()
462-
.endObject()
463-
.startObject("derived_2")
464-
.field("type", "keyword")
465-
.startObject("script")
466-
.field("source", DERIVED_FIELD_SCRIPT_2)
467-
.field("lang", "mockscript")
468-
.endObject()
469-
.endObject()
470-
.endObject()
471-
.endObject();
472-
473-
IndexService indexService = createIndex("index", Settings.EMPTY, MapperService.SINGLE_MAPPING_NAME, mapping);
474-
MapperService mapperService = indexService.mapperService();
475-
476-
XContentBuilder source = XContentFactory.jsonBuilder()
477-
.startObject()
478-
.field("field1", "some text 1")
479-
.field("field2", "some text 2")
480-
.endObject();
481-
482-
Map<String, DocumentField> fields = fetchFields(mapperService, source, "*");
483-
assertThat(fields.size(), equalTo(2));
484-
assertThat(fields.keySet(), containsInAnyOrder("derived_1", "derived_2"));
485-
assertThat(fields.get("derived_1").getValues().size(), equalTo(1));
486-
assertThat(fields.get("derived_2").getValues().size(), equalTo(1));
487-
assertThat(fields.get("derived_1").getValue(), equalTo("some text 1"));
488-
assertThat(fields.get("derived_2").getValue(), equalTo("some text 2"));
489-
}
490-
491438
private static Map<String, DocumentField> fetchFields(MapperService mapperService, XContentBuilder source, String fieldPattern)
492439
throws IOException {
493440

@@ -501,13 +448,7 @@ private static Map<String, DocumentField> fetchFields(MapperService mapperServic
501448
SourceLookup sourceLookup = new SourceLookup();
502449
sourceLookup.setSource(BytesReference.bytes(source));
503450

504-
SearchLookup searchLookup = mock(SearchLookup.class);
505-
LeafSearchLookup leafSearchLookup = mock(LeafSearchLookup.class);
506-
when(searchLookup.source()).thenReturn(sourceLookup);
507-
when(searchLookup.getLeafSearchLookup(any())).thenReturn(leafSearchLookup);
508-
when(leafSearchLookup.source()).thenReturn(sourceLookup);
509-
FieldFetcher fieldFetcher = FieldFetcher.create(createQueryShardContext(mapperService), searchLookup, fields);
510-
fieldFetcher.setNextReader(null);
451+
FieldFetcher fieldFetcher = FieldFetcher.create(createQueryShardContext(mapperService), null, fields);
511452
return fieldFetcher.fetch(sourceLookup, Set.of());
512453
}
513454

@@ -556,19 +497,6 @@ private static QueryShardContext createQueryShardContext(MapperService mapperSer
556497
.build();
557498
IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build();
558499
IndexSettings indexSettings = new IndexSettings(indexMetadata, settings);
559-
560-
final MockScriptEngine engine = new MockScriptEngine(
561-
MockScriptEngine.NAME,
562-
Map.of(
563-
DERIVED_FIELD_SCRIPT_1,
564-
(script) -> ((Map<String, Object>) script.get("_source")).get("field1"),
565-
DERIVED_FIELD_SCRIPT_2,
566-
(script) -> ((Map<String, Object>) script.get("_source")).get("field2")
567-
),
568-
Collections.emptyMap()
569-
);
570-
final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
571-
ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
572500
return new QueryShardContext(
573501
0,
574502
indexSettings,
@@ -577,7 +505,7 @@ private static QueryShardContext createQueryShardContext(MapperService mapperSer
577505
null,
578506
mapperService,
579507
null,
580-
scriptService,
508+
null,
581509
null,
582510
null,
583511
null,

0 commit comments

Comments
 (0)