|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.fieldvisitor; |
| 11 | + |
| 12 | +import org.apache.lucene.index.FieldInfo; |
| 13 | +import org.apache.lucene.index.LeafReaderContext; |
| 14 | +import org.apache.lucene.index.StoredFieldVisitor; |
| 15 | +import org.apache.lucene.util.BytesRef; |
| 16 | +import org.elasticsearch.common.bytes.BytesReference; |
| 17 | +import org.elasticsearch.index.mapper.BlockLoader; |
| 18 | +import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +class IgnoredSourceFieldLoader extends StoredFieldLoader { |
| 29 | + private final boolean forceSequentialReader; |
| 30 | + private final Map<String, Set<String>> potentialFieldsInIgnoreSource; |
| 31 | + private final Set<String> fieldNames; |
| 32 | + |
| 33 | + IgnoredSourceFieldLoader(BlockLoader.FieldsSpec spec, boolean forceSequentialReader) { |
| 34 | + fieldNames = new HashSet<>(spec.ignoredFieldsSpec().requiredIgnoredFields()); |
| 35 | + this.forceSequentialReader = forceSequentialReader; |
| 36 | + |
| 37 | + HashMap<String, Set<String>> potentialFieldsInIgnoreSource = new HashMap<>(); |
| 38 | + for (String requiredIgnoredField : spec.ignoredFieldsSpec().requiredIgnoredFields()) { |
| 39 | + for (String potentialStoredField : spec.ignoredFieldsSpec().format().requiredStoredFields(requiredIgnoredField)) { |
| 40 | + potentialFieldsInIgnoreSource.computeIfAbsent(potentialStoredField, k -> new HashSet<>()).add(requiredIgnoredField); |
| 41 | + } |
| 42 | + } |
| 43 | + this.potentialFieldsInIgnoreSource = potentialFieldsInIgnoreSource; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public LeafStoredFieldLoader getLoader(LeafReaderContext ctx, int[] docs) throws IOException { |
| 48 | + var reader = forceSequentialReader ? sequentialReader(ctx) : reader(ctx, docs); |
| 49 | + var visitor = new SFV(fieldNames, potentialFieldsInIgnoreSource); |
| 50 | + return new LeafStoredFieldLoader() { |
| 51 | + |
| 52 | + private int doc = -1; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void advanceTo(int doc) throws IOException { |
| 56 | + if (doc != this.doc) { |
| 57 | + visitor.reset(); |
| 58 | + reader.accept(doc, visitor); |
| 59 | + this.doc = doc; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public BytesReference source() { |
| 65 | + assert false : "source() is not supported by IgnoredSourceFieldLoader"; |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String id() { |
| 71 | + assert false : "id() is not supported by IgnoredSourceFieldLoader"; |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String routing() { |
| 77 | + assert false : "routing() is not supported by IgnoredSourceFieldLoader"; |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Map<String, List<Object>> storedFields() { |
| 83 | + return visitor.values; |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public List<String> fieldsToLoad() { |
| 90 | + return potentialFieldsInIgnoreSource.keySet().stream().toList(); |
| 91 | + } |
| 92 | + |
| 93 | + static class SFV extends StoredFieldVisitor { |
| 94 | + final Map<String, List<Object>> values = new HashMap<>(); |
| 95 | + final Set<String> fieldNames; |
| 96 | + private final Set<String> unvisitedFields; |
| 97 | + final Map<String, Set<String>> potentialFieldsInIgnoreSource; |
| 98 | + |
| 99 | + SFV(Set<String> fieldNames, Map<String, Set<String>> potentialFieldsInIgnoreSource) { |
| 100 | + this.fieldNames = fieldNames; |
| 101 | + this.unvisitedFields = new HashSet<>(fieldNames); |
| 102 | + this.potentialFieldsInIgnoreSource = potentialFieldsInIgnoreSource; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Status needsField(FieldInfo fieldInfo) throws IOException { |
| 107 | + if (unvisitedFields.isEmpty()) { |
| 108 | + return Status.STOP; |
| 109 | + } |
| 110 | + |
| 111 | + Set<String> foundFields = potentialFieldsInIgnoreSource.get(fieldInfo.name); |
| 112 | + if (foundFields == null) { |
| 113 | + return Status.NO; |
| 114 | + } |
| 115 | + |
| 116 | + unvisitedFields.removeAll(foundFields); |
| 117 | + return Status.YES; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void binaryField(FieldInfo fieldInfo, byte[] value) { |
| 122 | + values.computeIfAbsent(fieldInfo.name, k -> new ArrayList<>()).add(new BytesRef(value)); |
| 123 | + } |
| 124 | + |
| 125 | + void reset() { |
| 126 | + values.clear(); |
| 127 | + unvisitedFields.addAll(fieldNames); |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + static boolean supports(BlockLoader.FieldsSpec spec) { |
| 133 | + return spec.storedFieldsSpec().noRequirements() |
| 134 | + && spec.ignoredFieldsSpec().format() == IgnoredSourceFieldMapper.IgnoredSourceFormat.PER_FIELD_IGNORED_SOURCE; |
| 135 | + } |
| 136 | +} |
0 commit comments