Skip to content

Commit

Permalink
Add tests related to derived fields for QueryShardContext changes
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed Apr 4, 2024
1 parent df39a42 commit f48357e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static DerivedFieldMapper toType(FieldMapper in) {
*/
public static class Builder extends ParametrizedFieldMapper.Builder {
// TODO: The type of parameter may change here if the actual underlying FieldType object is needed
private final Parameter<String> type = Parameter.stringParam("type", false, m -> toType(m).type, "text");
private final Parameter<String> type = Parameter.stringParam("type", false, m -> toType(m).type, "keyword");

private final Parameter<Script> script = new Parameter<>(
"script",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

package org.opensearch.index.query;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
Expand Down Expand Up @@ -63,9 +64,14 @@
import org.opensearch.index.fielddata.LeafFieldData;
import org.opensearch.index.fielddata.ScriptDocValues;
import org.opensearch.index.fielddata.plain.AbstractLeafOrdinalsFieldData;
import org.opensearch.index.mapper.ContentPath;
import org.opensearch.index.mapper.DerivedFieldMapper;
import org.opensearch.index.mapper.DocumentMapper;
import org.opensearch.index.mapper.IndexFieldMapper;
import org.opensearch.index.mapper.MappedFieldType;
import org.opensearch.index.mapper.Mapper;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.mapper.MappingLookup;
import org.opensearch.index.mapper.TextFieldMapper;
import org.opensearch.search.lookup.LeafDocLookup;
import org.opensearch.search.lookup.LeafSearchLookup;
Expand Down Expand Up @@ -118,6 +124,25 @@ public void testFailIfFieldMappingNotFound() {
assertThat(result.name(), equalTo("name"));
}

public void testDerivedFieldMapping() {
QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
assertNull(context.failIfFieldMappingNotFound("test_derived", null));
DocumentMapper documentMapper = mock(DocumentMapper.class);
Mapper.BuilderContext builderContext = new Mapper.BuilderContext(Settings.EMPTY, new ContentPath(0));
DerivedFieldMapper derivedFieldMapper = new DerivedFieldMapper.Builder("test_derived").build(builderContext);
MappingLookup mappingLookup = new MappingLookup(
Collections.singletonList(derivedFieldMapper),
Collections.emptyList(),
Collections.emptyList(),
0,
new StandardAnalyzer()
);
when(documentMapper.mappers()).thenReturn(mappingLookup);
context.setDerivedFieldMappers(documentMapper);
context.setAllowUnmappedFields(false);
assertEquals(derivedFieldMapper.fieldType(), context.failIfFieldMappingNotFound("test_derived", null));
}

public void testToQueryFails() {
QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null);
Exception exc = expectThrows(Exception.class, () -> context.toQuery(new AbstractQueryBuilder() {
Expand Down

0 comments on commit f48357e

Please sign in to comment.