use of org.opensearch.index.mapper.ValueFetcher in project OpenSearch by opensearch-project.
the class FieldFetcher method create.
public static FieldFetcher create(QueryShardContext context, SearchLookup searchLookup, Collection<FieldAndFormat> fieldAndFormats) {
List<FieldContext> fieldContexts = new ArrayList<>();
for (FieldAndFormat fieldAndFormat : fieldAndFormats) {
String fieldPattern = fieldAndFormat.field;
String format = fieldAndFormat.format;
Collection<String> concreteFields = context.simpleMatchToIndexNames(fieldPattern);
for (String field : concreteFields) {
MappedFieldType ft = context.getFieldType(field);
if (ft == null || context.isMetadataField(field)) {
continue;
}
ValueFetcher valueFetcher = ft.valueFetcher(context, searchLookup, format);
fieldContexts.add(new FieldContext(field, valueFetcher));
}
}
return new FieldFetcher(fieldContexts);
}
use of org.opensearch.index.mapper.ValueFetcher in project OpenSearch by opensearch-project.
the class FetchDocValuesPhase method getProcessor.
@Override
public FetchSubPhaseProcessor getProcessor(FetchContext context) {
FetchDocValuesContext dvContext = context.docValuesContext();
if (dvContext == null) {
return null;
}
if (context.docValuesContext().fields().stream().map(f -> f.format).anyMatch(USE_DEFAULT_FORMAT::equals)) {
DEPRECATION_LOGGER.deprecate("explicit_default_format", "[" + USE_DEFAULT_FORMAT + "] is a special format that was only used to " + "ease the transition to 7.x. It has become the default and shouldn't be set explicitly anymore.");
}
/*
* Its tempting to swap this to a `Map` but that'd break backwards
* compatibility because we support fetching the same field multiple
* times with different configuration. That isn't possible with a `Map`.
*/
List<DocValueField> fields = new ArrayList<>();
for (FieldAndFormat fieldAndFormat : context.docValuesContext().fields()) {
MappedFieldType ft = context.mapperService().fieldType(fieldAndFormat.field);
if (ft == null) {
continue;
}
String format = USE_DEFAULT_FORMAT.equals(fieldAndFormat.format) ? null : fieldAndFormat.format;
ValueFetcher fetcher = new DocValueFetcher(ft.docValueFormat(format, null), context.searchLookup().doc().getForField(ft));
fields.add(new DocValueField(fieldAndFormat.field, fetcher));
}
return new FetchSubPhaseProcessor() {
@Override
public void setNextReader(LeafReaderContext readerContext) {
for (DocValueField f : fields) {
f.fetcher.setNextReader(readerContext);
}
}
@Override
public void process(HitContext hit) throws IOException {
for (DocValueField f : fields) {
DocumentField hitField = hit.hit().field(f.field);
if (hitField == null) {
hitField = new DocumentField(f.field, new ArrayList<>(2));
// even if we request a doc values of a meta-field (e.g. _routing),
// docValues fields will still be document fields, and put under "fields" section of a hit.
hit.hit().setDocumentField(f.field, hitField);
}
hitField.getValues().addAll(f.fetcher.fetchValues(hit.sourceLookup()));
}
}
};
}
use of org.opensearch.index.mapper.ValueFetcher in project OpenSearch by opensearch-project.
the class FieldFetcher method fetch.
public Map<String, DocumentField> fetch(SourceLookup sourceLookup, Set<String> ignoredFields) throws IOException {
Map<String, DocumentField> documentFields = new HashMap<>();
for (FieldContext context : fieldContexts) {
String field = context.fieldName;
if (ignoredFields.contains(field)) {
continue;
}
ValueFetcher valueFetcher = context.valueFetcher;
List<Object> parsedValues = valueFetcher.fetchValues(sourceLookup);
if (parsedValues.isEmpty() == false) {
documentFields.put(field, new DocumentField(field, parsedValues));
}
}
return documentFields;
}
use of org.opensearch.index.mapper.ValueFetcher in project OpenSearch by opensearch-project.
the class HighlightUtils method loadFieldValues.
/**
* Load field values for highlighting.
*/
public static List<Object> loadFieldValues(MappedFieldType fieldType, QueryShardContext context, FetchSubPhase.HitContext hitContext, boolean forceSource) throws IOException {
if (forceSource == false && fieldType.isStored()) {
CustomFieldsVisitor fieldVisitor = new CustomFieldsVisitor(singleton(fieldType.name()), false);
hitContext.reader().document(hitContext.docId(), fieldVisitor);
List<Object> textsToHighlight = fieldVisitor.fields().get(fieldType.name());
return textsToHighlight != null ? textsToHighlight : Collections.emptyList();
}
ValueFetcher fetcher = fieldType.valueFetcher(context, null, null);
return fetcher.fetchValues(hitContext.sourceLookup());
}
Aggregations