Skip to content

Commit

Permalink
Added test for _source disabled case
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed Nov 17, 2023
1 parent 19ce551 commit 3155151
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public SourceFieldMatchQuery(Query delegateQuery, Query filter, MappedFieldType
this.fieldType = fieldType;
this.context = context;
this.lookup = context.lookup();
if (!context.documentMapper("").sourceMapper().enabled()) {
throw new IllegalArgumentException(
"SourceFieldMatchQuery error: unable to fetch fields from _source field: _source is disabled in the mappings "
+ "for index ["
+ context.index().getName()
+ "]"
);
}
this.valueFetcher = (SourceValueFetcher) fieldType.valueFetcher(context, lookup, null);
}

Expand Down Expand Up @@ -95,7 +103,12 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
public boolean matches() {
leafSearchLookup.setDocument(approximation.docID());
List<Object> values = valueFetcher.fetchValues(leafSearchLookup.source());
// Missing fields won't count as match. Can we use a default value for missing field?
if (values.isEmpty()) {
return false;
}
MemoryIndex memoryIndex = new MemoryIndex();

for (Object value : values) {
memoryIndex.addField(fieldType.name(), (String) value, fieldType.indexAnalyzer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.opensearch.core.index.Index;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.mapper.MapperServiceTestCase;
import org.opensearch.index.mapper.ParsedDocument;
Expand All @@ -20,6 +21,7 @@
import java.util.List;
import java.util.Set;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

public class SourceFieldMatchQueryTests extends MapperServiceTestCase {
Expand All @@ -35,6 +37,8 @@ public void testAllPossibleScenarios() throws IOException {

QueryShardContext queryShardContext = createQueryShardContext(mapperService);
when(queryShardContext.sourcePath("desert")).thenReturn(Set.of("desert"));
when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid"));
when(queryShardContext.documentMapper(anyString())).thenReturn(mapperService.documentMapper());

String[] deserts = new String[] { "apple pie pie", "banana split pie", "chocolate cake" };
List<ParsedDocument> docs = new ArrayList<>();
Expand Down Expand Up @@ -103,4 +107,26 @@ public void testAllPossibleScenarios() throws IOException {
}
});
}

public void testSourceDisabled() throws IOException {
MapperService mapperService = createMapperService(topMapping(b -> b.startObject("_source").field("enabled", false).endObject()));
QueryShardContext queryShardContext = createQueryShardContext(mapperService);
when(queryShardContext.sourcePath("desert")).thenReturn(Set.of("desert"));
when(queryShardContext.index()).thenReturn(new Index("test_index", "uuid"));
when(queryShardContext.documentMapper(anyString())).thenReturn(mapperService.documentMapper());
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> new SourceFieldMatchQuery(
QueryBuilders.matchQuery("desert", "apple").doToQuery(queryShardContext), // Delegate query
QueryBuilders.matchQuery("desert", "pie").doToQuery(queryShardContext), // Filter query
queryShardContext.getFieldType("desert"),
queryShardContext
)
);
assertEquals(
"SourceFieldMatchQuery error: unable to fetch fields from _source field: "
+ "_source is disabled in the mappings for index [test_index]",
e.getMessage()
);
}
}

0 comments on commit 3155151

Please sign in to comment.