|
16 | 16 | import org.elasticsearch.xcontent.XContentParserConfiguration; |
17 | 17 |
|
18 | 18 | import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
19 | 24 | import java.util.Optional; |
20 | 25 | import java.util.Set; |
21 | 26 |
|
@@ -66,60 +71,132 @@ public SortedSetDocValues ordinals(LeafReaderContext context) throws IOException |
66 | 71 | throw new UnsupportedOperationException(); |
67 | 72 | } |
68 | 73 |
|
69 | | - private static class IgnoredSourceRowStrideReader implements RowStrideReader { |
70 | | - private final String fieldName; |
71 | | - private final Reader reader; |
72 | | - |
73 | | - private IgnoredSourceRowStrideReader(String fieldName, Reader reader) { |
74 | | - this.fieldName = fieldName; |
75 | | - this.reader = reader; |
76 | | - } |
77 | | - |
| 74 | + private record IgnoredSourceRowStrideReader(String fieldName, Reader reader) implements RowStrideReader { |
78 | 75 | @Override |
79 | 76 | public void read(int docId, StoredFields storedFields, Builder builder) throws IOException { |
80 | 77 | var ignoredSource = storedFields.storedFields().get(IgnoredSourceFieldMapper.NAME); |
81 | 78 | if (ignoredSource == null) { |
82 | 79 | return; |
83 | 80 | } |
84 | 81 |
|
85 | | - boolean written = false; |
| 82 | + Map<String, List<IgnoredSourceFieldMapper.NameValue>> valuesForFieldAndParents = new HashMap<>(); |
| 83 | + |
| 84 | + // Contains name of the field and all its parents |
| 85 | + Set<String> fieldNames = new HashSet<>() { |
| 86 | + { |
| 87 | + add("_doc"); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + var current = new StringBuilder(); |
| 92 | + for (String part : fieldName.split("\\.")) { |
| 93 | + current.append(part); |
| 94 | + fieldNames.add(current.toString()); |
| 95 | + } |
| 96 | + |
86 | 97 | for (Object value : ignoredSource) { |
87 | 98 | IgnoredSourceFieldMapper.NameValue nameValue = IgnoredSourceFieldMapper.decode(value); |
88 | | - if (nameValue.name().equals(fieldName)) { |
89 | | - // Leaf field is stored directly (not as a part of a parent object), let's try to decode it. |
90 | | - Optional<Object> singleValue = XContentDataHelper.decode(nameValue.value()); |
91 | | - if (singleValue.isPresent()) { |
92 | | - reader.readValue(singleValue.get(), builder); |
93 | | - written = true; |
94 | | - continue; |
95 | | - } |
96 | | - |
97 | | - // We have a value for this field but it's an array or an object |
98 | | - var type = XContentDataHelper.decodeType(nameValue.value()); |
99 | | - assert type.isPresent(); |
100 | | - |
101 | | - var filterParserConfig = XContentParserConfiguration.EMPTY.withFiltering("", Set.of(fieldName), Set.of(), true); |
102 | | - try ( |
103 | | - XContentParser parser = type.get() |
104 | | - .xContent() |
105 | | - .createParser( |
106 | | - filterParserConfig, |
107 | | - nameValue.value().bytes, |
108 | | - nameValue.value().offset + 1, |
109 | | - nameValue.value().length - 1 |
110 | | - ) |
111 | | - ) { |
112 | | - parser.nextToken(); |
113 | | - reader.parse(parser, builder); |
114 | | - } |
115 | | - written = true; |
| 99 | + if (fieldNames.contains(nameValue.name())) { |
| 100 | + valuesForFieldAndParents.computeIfAbsent(nameValue.name(), k -> new ArrayList<>()).add(nameValue); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // TODO figure out how to handle XContentDataHelper#voidValue() |
| 105 | + |
| 106 | + if (readFromFieldValue(valuesForFieldAndParents.get(fieldName), builder)) { |
| 107 | + return; |
| 108 | + } |
| 109 | + if (readFromParentValue(valuesForFieldAndParents, builder)) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + builder.appendNull(); |
| 114 | + } |
| 115 | + |
| 116 | + private boolean readFromFieldValue(List<IgnoredSourceFieldMapper.NameValue> nameValues, Builder builder) throws IOException { |
| 117 | + if (nameValues == null || nameValues.isEmpty()) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + // TODO this is not working properly |
| 122 | + if (nameValues.size() > 1) { |
| 123 | + builder.beginPositionEntry(); |
| 124 | + } |
| 125 | + |
| 126 | + for (var nameValue : nameValues) { |
| 127 | + // Leaf field is stored directly (not as a part of a parent object), let's try to decode it. |
| 128 | + Optional<Object> singleValue = XContentDataHelper.decode(nameValue.value()); |
| 129 | + if (singleValue.isPresent()) { |
| 130 | + reader.readValue(singleValue.get(), builder); |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + // We have a value for this field but it's an array or an object |
| 135 | + var type = XContentDataHelper.decodeType(nameValue.value()); |
| 136 | + assert type.isPresent(); |
| 137 | + |
| 138 | + try ( |
| 139 | + XContentParser parser = type.get() |
| 140 | + .xContent() |
| 141 | + .createParser(XContentParserConfiguration.EMPTY, nameValue.value().bytes, nameValue.value().offset + 1, nameValue.value().length - 1) |
| 142 | + ) { |
| 143 | + parser.nextToken(); |
| 144 | + reader.parse(parser, builder); |
116 | 145 | } |
117 | | - // It is possible that the field is stored as part of the parent object, we'll need to look at those too and use something |
118 | | - // similar to reader.parse() |
119 | 146 | } |
120 | 147 |
|
121 | | - if (written == false) { |
122 | | - builder.appendNull(); |
| 148 | + if (nameValues.size() > 1) { |
| 149 | + builder.endPositionEntry(); |
| 150 | + } |
| 151 | + |
| 152 | + return true; |
| 153 | + } |
| 154 | + |
| 155 | + private boolean readFromParentValue(Map<String, List<IgnoredSourceFieldMapper.NameValue>> valuesForFieldAndParents, Builder builder) throws IOException { |
| 156 | + if (valuesForFieldAndParents.isEmpty()) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + // If a parent object is stored at a particular level its children won't be stored. |
| 161 | + // So we should only ever have one parent here. |
| 162 | + assert valuesForFieldAndParents.size() == 1 : "_ignored_source field contains multiple levels of the same object"; |
| 163 | + var parentValues = valuesForFieldAndParents.values().iterator().next(); |
| 164 | + if (parentValues.size() > 1) { |
| 165 | + builder.beginPositionEntry(); |
| 166 | + } |
| 167 | + |
| 168 | + for (var nameValue : parentValues) { |
| 169 | + parseFieldFromParent(nameValue, builder); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + if (parentValues.size() > 1) { |
| 174 | + builder.endPositionEntry(); |
| 175 | + } |
| 176 | + |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + private void parseFieldFromParent(IgnoredSourceFieldMapper.NameValue nameValue, Builder builder) throws IOException { |
| 181 | + var type = XContentDataHelper.decodeType(nameValue.value()); |
| 182 | + assert type.isPresent(); |
| 183 | + |
| 184 | + var filterParserConfig = XContentParserConfiguration.EMPTY.withFiltering(null, Set.of(fieldName), Set.of(), true); |
| 185 | + try ( |
| 186 | + XContentParser parser = type.get() |
| 187 | + .xContent() |
| 188 | + .createParser(filterParserConfig, nameValue.value().bytes, nameValue.value().offset + 1, nameValue.value().length - 1) |
| 189 | + ) { |
| 190 | + parser.nextToken(); |
| 191 | +// boolean found = false; |
| 192 | +// do { |
| 193 | +// var token = parser.nextToken(); |
| 194 | +// if (token == XContentParser.Token.FIELD_NAME && parser.currentName().equals(fieldName)) { |
| 195 | +// found = true; |
| 196 | +// } |
| 197 | +// |
| 198 | +// } while (found == false); |
| 199 | + reader.parse(parser, builder); |
123 | 200 | } |
124 | 201 | } |
125 | 202 |
|
|
0 commit comments