|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.fetch.subphase; |
| 21 | + |
| 22 | +import org.apache.lucene.index.LeafReaderContext; |
| 23 | +import org.apache.lucene.index.ReaderUtil; |
| 24 | +import org.elasticsearch.common.document.DocumentField; |
| 25 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 26 | +import org.elasticsearch.index.mapper.DocumentMapper; |
| 27 | +import org.elasticsearch.search.SearchHit; |
| 28 | +import org.elasticsearch.search.fetch.FetchSubPhase; |
| 29 | +import org.elasticsearch.search.internal.SearchContext; |
| 30 | +import org.elasticsearch.search.lookup.SourceLookup; |
| 31 | + |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Set; |
| 38 | +import java.util.function.Function; |
| 39 | + |
| 40 | +/** |
| 41 | + * A fetch sub-phase for high-level field retrieval. Given a list of fields, it |
| 42 | + * retrieves the field values from _source and returns them as document fields. |
| 43 | + */ |
| 44 | +public final class FetchFieldsPhase implements FetchSubPhase { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void hitsExecute(SearchContext context, SearchHit[] hits) { |
| 48 | + hitsExecute(context, hit -> getSourceLookup(context, hit), hits); |
| 49 | + } |
| 50 | + |
| 51 | + // Visible for testing. |
| 52 | + @SuppressWarnings("unchecked") |
| 53 | + void hitsExecute(SearchContext context, |
| 54 | + Function<SearchHit, SourceLookup> sourceProvider, |
| 55 | + SearchHit[] hits) { |
| 56 | + FetchFieldsContext fetchFieldsContext = context.fetchFieldsContext(); |
| 57 | + if (fetchFieldsContext == null || fetchFieldsContext.fields().isEmpty()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + DocumentMapper documentMapper = context.mapperService().documentMapper(); |
| 62 | + if (documentMapper.sourceMapper().enabled() == false) { |
| 63 | + throw new IllegalArgumentException("Unable to retrieve the requested [fields] since _source is " + |
| 64 | + "disabled in the mappings for index [" + context.indexShard().shardId().getIndexName() + "]"); |
| 65 | + } |
| 66 | + |
| 67 | + Set<String> fields = new HashSet<>(); |
| 68 | + for (String fieldPattern : context.fetchFieldsContext().fields()) { |
| 69 | + if (documentMapper.objectMappers().containsKey(fieldPattern)) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + Collection<String> concreteFields = context.mapperService().simpleMatchToFullName(fieldPattern); |
| 73 | + fields.addAll(concreteFields); |
| 74 | + } |
| 75 | + |
| 76 | + for (SearchHit hit : hits) { |
| 77 | + SourceLookup sourceLookup = sourceProvider.apply(hit); |
| 78 | + Map<String, Object> valuesByField = extractValues(sourceLookup, fields); |
| 79 | + |
| 80 | + for (Map.Entry<String, Object> entry : valuesByField.entrySet()) { |
| 81 | + String field = entry.getKey(); |
| 82 | + Object value = entry.getValue(); |
| 83 | + List<Object> values = value instanceof List |
| 84 | + ? (List<Object>) value |
| 85 | + : List.of(value); |
| 86 | + |
| 87 | + DocumentField documentField = new DocumentField(field, values); |
| 88 | + hit.setField(field, documentField); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private SourceLookup getSourceLookup(SearchContext context, SearchHit hit) { |
| 94 | + SourceLookup sourceLookup = context.lookup().source(); |
| 95 | + int readerIndex = ReaderUtil.subIndex(hit.docId(), context.searcher().getIndexReader().leaves()); |
| 96 | + LeafReaderContext readerContext = context.searcher().getIndexReader().leaves().get(readerIndex); |
| 97 | + sourceLookup.setSegmentAndDocument(readerContext, hit.docId()); |
| 98 | + return sourceLookup; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * For each of the provided paths, return its value in the source. Note that in contrast with |
| 103 | + * {@link SourceLookup#extractRawValues}, array and object values can be returned. |
| 104 | + */ |
| 105 | + private Map<String, Object> extractValues(SourceLookup sourceLookup, Collection<String> paths) { |
| 106 | + Map<String, Object> result = new HashMap<>(paths.size()); |
| 107 | + for (String path : paths) { |
| 108 | + Object value = XContentMapValues.extractValue(path, sourceLookup); |
| 109 | + if (value != null) { |
| 110 | + result.put(path, value); |
| 111 | + } |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | +} |
0 commit comments