Search in sources :

Example 1 with DocValueFetcher

use of org.opensearch.index.mapper.DocValueFetcher 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)

Aggregations

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