Skip to content

Commit

Permalink
Give value fetcher to metadata fields where missing
Browse files Browse the repository at this point in the history
  • Loading branch information
javanna committed Apr 3, 2024
1 parent 646ac4f commit 4d6d8f0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import org.elasticsearch.index.mapper.DocumentParsingException;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.index.mapper.MetadataMapperTestCase;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -200,4 +202,10 @@ public void testValidateDefaultIgnoreMalformed() throws Exception {
assertThat(((DateFieldMapper) mapperService.documentMapper().mappers().getMapper("@timestamp")).ignoreMalformed(), is(false));
}
}

public void testFetchStoredValue() throws IOException {
MetadataFieldMapper metadataFieldMapper = new DataStreamTimestampFieldMapper.Builder().build();
List<?> values = fetchStoredValue(metadataFieldMapper.fieldType(), Collections.singletonList("2021-01-01"), null);
assertEquals("2021-01-01", values.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Query existsQuery(SearchExecutionContext context) {

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
throw new UnsupportedOperationException();
return new StoredValueFetcher(context.lookup(), NAME);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NestedPathFieldMapper extends MetadataFieldMapper {

public static final String NAME = "_nested_path";

private static final NestedPathFieldMapper INSTANCE = new NestedPathFieldMapper(NAME);
static final NestedPathFieldMapper INSTANCE = new NestedPathFieldMapper(NAME);
private static final NestedPathFieldMapper INSTANCE_PRE_V8 = new NestedPathFieldMapper(NAME_PRE_V8);

public static String name(IndexVersion version) {
Expand Down Expand Up @@ -66,7 +66,7 @@ public Query existsQuery(SearchExecutionContext context) {

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
return new StoredValueFetcher(context.lookup(), NAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public boolean mayExistInIndex(SearchExecutionContext context) {

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
return new StoredValueFetcher(context.lookup(), NAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) {
SearchExecutionContext sec = fetchContext.getSearchExecutionContext();
for (String field : storedFieldsContext.fieldNames()) {
if (SourceFieldMapper.NAME.equals(field) == false) {
Collection<String> fieldNames = sec.getMatchingFieldNames(field);
/*Collection<String> fieldNames = sec.getMatchingFieldNames(field);
for (String fieldName : fieldNames) {
MappedFieldType ft = sec.getFieldType(fieldName);
if (ft.isStored() == false) {
continue;
}
storedFields.add(new StoredField(fieldName, ft, sec.isMetadataField(ft.name())));
fieldsToLoad.add(ft.name());
}*/
//TODO temporary removal of wildcard resolution: see what tests fail
MappedFieldType ft = sec.getFieldType(field);
if (ft.isStored() == false) {
continue;
}
storedFields.add(new StoredField(field, ft, sec.isMetadataField(ft.name())));
fieldsToLoad.add(ft.name());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.empty;

Expand Down Expand Up @@ -42,4 +44,9 @@ public void testDefaults() throws IOException {
ParsedDocument document = mapper.parse(new SourceToParse("id", new BytesArray("{}"), XContentType.JSON));
assertThat(document.rootDoc().getFields(NestedPathFieldMapper.NAME), empty());
}

public void testFetchStoredValue() throws IOException {
List<?> values = fetchStoredValue(NestedPathFieldMapper.INSTANCE.fieldType(), Collections.singletonList("path"), null);
assertEquals("path", values.get(0));
}
}

0 comments on commit 4d6d8f0

Please sign in to comment.