Search in sources :

Example 1 with ValueFetcher

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);
}
Also used : ArrayList(java.util.ArrayList) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ValueFetcher(org.opensearch.index.mapper.ValueFetcher)

Example 2 with ValueFetcher

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()));
            }
        }
    };
}
Also used : DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) FetchSubPhase(org.opensearch.search.fetch.FetchSubPhase) List(java.util.List) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) ValueFetcher(org.opensearch.index.mapper.ValueFetcher) FetchContext(org.opensearch.search.fetch.FetchContext) DocValueFetcher(org.opensearch.index.mapper.DocValueFetcher) IOException(java.io.IOException) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocumentField(org.opensearch.common.document.DocumentField) FetchSubPhaseProcessor(org.opensearch.search.fetch.FetchSubPhaseProcessor) ArrayList(java.util.ArrayList) DocumentField(org.opensearch.common.document.DocumentField) DocValueFetcher(org.opensearch.index.mapper.DocValueFetcher) ArrayList(java.util.ArrayList) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ValueFetcher(org.opensearch.index.mapper.ValueFetcher) DocValueFetcher(org.opensearch.index.mapper.DocValueFetcher) FetchSubPhaseProcessor(org.opensearch.search.fetch.FetchSubPhaseProcessor)

Example 3 with ValueFetcher

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;
}
Also used : DocumentField(org.opensearch.common.document.DocumentField) HashMap(java.util.HashMap) ValueFetcher(org.opensearch.index.mapper.ValueFetcher)

Example 4 with ValueFetcher

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());
}
Also used : CustomFieldsVisitor(org.opensearch.index.fieldvisitor.CustomFieldsVisitor) ValueFetcher(org.opensearch.index.mapper.ValueFetcher)

Aggregations

ValueFetcher (org.opensearch.index.mapper.ValueFetcher)4 ArrayList (java.util.ArrayList)2 DocumentField (org.opensearch.common.document.DocumentField)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 DeprecationLogger (org.opensearch.common.logging.DeprecationLogger)1 CustomFieldsVisitor (org.opensearch.index.fieldvisitor.CustomFieldsVisitor)1 DocValueFetcher (org.opensearch.index.mapper.DocValueFetcher)1 FetchContext (org.opensearch.search.fetch.FetchContext)1 FetchSubPhase (org.opensearch.search.fetch.FetchSubPhase)1 FetchSubPhaseProcessor (org.opensearch.search.fetch.FetchSubPhaseProcessor)1