|
20 | 20 | package org.elasticsearch.search.fetch.subphase; |
21 | 21 |
|
22 | 22 | import org.elasticsearch.ElasticsearchException; |
23 | | -import org.elasticsearch.common.bytes.BytesReference; |
24 | 23 | import org.elasticsearch.common.io.stream.BytesStreamOutput; |
25 | 24 | import org.elasticsearch.common.xcontent.XContentBuilder; |
26 | | -import org.elasticsearch.common.xcontent.XContentType; |
27 | 25 | import org.elasticsearch.search.SearchHit; |
28 | 26 | import org.elasticsearch.search.fetch.FetchSubPhase; |
29 | 27 | import org.elasticsearch.search.internal.SearchContext; |
30 | 28 | import org.elasticsearch.search.lookup.SourceLookup; |
31 | 29 |
|
32 | 30 | import java.io.IOException; |
33 | | -import java.io.UncheckedIOException; |
34 | 31 | import java.util.Map; |
35 | 32 |
|
36 | | -import static org.elasticsearch.common.xcontent.XContentFactory.contentBuilder; |
37 | | - |
38 | 33 | public final class FetchSourceSubPhase implements FetchSubPhase { |
39 | 34 |
|
40 | 35 | @Override |
@@ -65,7 +60,17 @@ public void hitExecute(SearchContext context, HitContext hitContext) { |
65 | 60 | final int initialCapacity = nestedHit ? 1024 : Math.min(1024, source.internalSourceRef().length()); |
66 | 61 | BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); |
67 | 62 | XContentBuilder builder = new XContentBuilder(source.sourceContentType().xContent(), streamOutput); |
68 | | - builder.value(value); |
| 63 | + if (value != null) { |
| 64 | + builder.value(value); |
| 65 | + } else { |
| 66 | + // This happens if the source filtering could not find the specified in the _source. |
| 67 | + // Just doing `builder.value(null)` is valid, but the xcontent validation can't detect what format |
| 68 | + // it is. In certain cases, for example response serialization we fail if no xcontent type can't be |
| 69 | + // detected. So instead we just return an empty top level object. Also this is in inline with what was |
| 70 | + // being return in this situation in 5.x and earlier. |
| 71 | + builder.startObject(); |
| 72 | + builder.endObject(); |
| 73 | + } |
69 | 74 | hitContext.hit().sourceRef(builder.bytes()); |
70 | 75 | } catch (IOException e) { |
71 | 76 | throw new ElasticsearchException("Error filtering source", e); |
|
0 commit comments