Skip to content

Commit 8892b77

Browse files
authored
Revert "Add XContentFieldFilter (#81970)" (#82897)
This reverts commit 4560a0c. It unexpectedly caused #82891.
1 parent df44d13 commit 8892b77

File tree

4 files changed

+34
-111
lines changed

4 files changed

+34
-111
lines changed

server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -522,32 +522,6 @@ public static BytesReference toXContent(ToXContent toXContent, XContentType xCon
522522
}
523523
}
524524

525-
/**
526-
* Guesses the content type based on the provided bytes which may be compressed.
527-
*
528-
* @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type.
529-
* The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed.
530-
* This method is deprecated to prevent usages of it from spreading further without specific reasons.
531-
*/
532-
@Deprecated
533-
public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
534-
Compressor compressor = CompressorFactory.compressor(bytes);
535-
if (compressor != null) {
536-
try {
537-
InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());
538-
if (compressedStreamInput.markSupported() == false) {
539-
compressedStreamInput = new BufferedInputStream(compressedStreamInput);
540-
}
541-
return XContentFactory.xContentType(compressedStreamInput);
542-
} catch (IOException e) {
543-
assert false : "Should not happen, we're just reading bytes from memory";
544-
throw new UncheckedIOException(e);
545-
}
546-
} else {
547-
return XContentHelper.xContentType(bytes);
548-
}
549-
}
550-
551525
/**
552526
* Guesses the content type based on the provided bytes.
553527
*

server/src/main/java/org/elasticsearch/index/get/ShardGetService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import org.elasticsearch.common.metrics.CounterMetric;
1717
import org.elasticsearch.common.metrics.MeanMetric;
1818
import org.elasticsearch.common.util.set.Sets;
19-
import org.elasticsearch.common.xcontent.XContentFieldFilter;
19+
import org.elasticsearch.common.xcontent.XContentHelper;
20+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
2021
import org.elasticsearch.core.Nullable;
22+
import org.elasticsearch.core.Tuple;
2123
import org.elasticsearch.index.IndexSettings;
2224
import org.elasticsearch.index.VersionType;
2325
import org.elasticsearch.index.engine.Engine;
@@ -31,6 +33,8 @@
3133
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
3234
import org.elasticsearch.index.shard.IndexShard;
3335
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
36+
import org.elasticsearch.xcontent.XContentFactory;
37+
import org.elasticsearch.xcontent.XContentType;
3438

3539
import java.io.IOException;
3640
import java.util.HashMap;
@@ -249,11 +253,15 @@ private GetResult innerGetLoadFromStoredFields(
249253
if (fetchSourceContext.fetchSource() == false) {
250254
source = null;
251255
} else if (fetchSourceContext.includes().length > 0 || fetchSourceContext.excludes().length > 0) {
256+
Map<String, Object> sourceAsMap;
252257
// TODO: The source might be parsed and available in the sourceLookup but that one uses unordered maps so different.
253258
// Do we care?
259+
Tuple<XContentType, Map<String, Object>> typeMapTuple = XContentHelper.convertToMap(source, true);
260+
XContentType sourceContentType = typeMapTuple.v1();
261+
sourceAsMap = typeMapTuple.v2();
262+
sourceAsMap = XContentMapValues.filter(sourceAsMap, fetchSourceContext.includes(), fetchSourceContext.excludes());
254263
try {
255-
source = XContentFieldFilter.newFieldFilter(fetchSourceContext.includes(), fetchSourceContext.excludes())
256-
.apply(source, null);
264+
source = BytesReference.bytes(XContentFactory.contentBuilder(sourceContentType).map(sourceAsMap));
257265
} catch (IOException e) {
258266
throw new ElasticsearchException("Failed to get id [" + id + "] with includes/excludes set", e);
259267
}

server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@
1616
import org.apache.lucene.util.BytesRef;
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.bytes.BytesReference;
19+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1920
import org.elasticsearch.common.util.CollectionUtils;
20-
import org.elasticsearch.common.xcontent.XContentFieldFilter;
21+
import org.elasticsearch.common.xcontent.XContentHelper;
22+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
2123
import org.elasticsearch.core.Nullable;
24+
import org.elasticsearch.core.Tuple;
2225
import org.elasticsearch.index.query.QueryShardException;
2326
import org.elasticsearch.index.query.SearchExecutionContext;
27+
import org.elasticsearch.xcontent.XContentBuilder;
28+
import org.elasticsearch.xcontent.XContentFactory;
2429
import org.elasticsearch.xcontent.XContentType;
2530

2631
import java.io.IOException;
2732
import java.util.Arrays;
2833
import java.util.Collections;
2934
import java.util.List;
35+
import java.util.Map;
36+
import java.util.function.Function;
3037

3138
public class SourceFieldMapper extends MetadataFieldMapper {
39+
3240
public static final String NAME = "_source";
3341
public static final String RECOVERY_SOURCE_NAME = "_recovery_source";
3442

3543
public static final String CONTENT_TYPE = "_source";
36-
private final XContentFieldFilter filter;
44+
private final Function<Map<String, ?>, Map<String, Object>> filter;
3745

3846
private static final SourceFieldMapper DEFAULT = new SourceFieldMapper(Defaults.ENABLED, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY);
3947

@@ -137,9 +145,7 @@ private SourceFieldMapper(boolean enabled, String[] includes, String[] excludes)
137145
this.includes = includes;
138146
this.excludes = excludes;
139147
final boolean filtered = CollectionUtils.isEmpty(includes) == false || CollectionUtils.isEmpty(excludes) == false;
140-
this.filter = enabled && filtered
141-
? XContentFieldFilter.newFieldFilter(includes, excludes)
142-
: (sourceBytes, contentType) -> sourceBytes;
148+
this.filter = enabled && filtered ? XContentMapValues.filter(includes, excludes) : null;
143149
this.complete = enabled && CollectionUtils.isEmpty(includes) && CollectionUtils.isEmpty(excludes);
144150
}
145151

@@ -174,7 +180,18 @@ public void preParse(DocumentParserContext context) throws IOException {
174180
public BytesReference applyFilters(@Nullable BytesReference originalSource, @Nullable XContentType contentType) throws IOException {
175181
if (enabled && originalSource != null) {
176182
// Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
177-
return filter.apply(originalSource, contentType);
183+
if (filter != null) {
184+
// we don't update the context source if we filter, we want to keep it as is...
185+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(originalSource, true, contentType);
186+
Map<String, Object> filteredSource = filter.apply(mapTuple.v2());
187+
BytesStreamOutput bStream = new BytesStreamOutput();
188+
XContentType actualContentType = mapTuple.v1();
189+
XContentBuilder builder = XContentFactory.contentBuilder(actualContentType, bStream).map(filteredSource);
190+
builder.close();
191+
return bStream.bytes();
192+
} else {
193+
return originalSource;
194+
}
178195
} else {
179196
return null;
180197
}

0 commit comments

Comments
 (0)