diff --git a/src/main/java/org/elasticsearch/search/fetch/source/FetchSourceSubPhase.java b/src/main/java/org/elasticsearch/search/fetch/source/FetchSourceSubPhase.java index d4d208aab4afb..76106861c80cc 100644 --- a/src/main/java/org/elasticsearch/search/fetch/source/FetchSourceSubPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/source/FetchSourceSubPhase.java @@ -22,12 +22,14 @@ import com.google.common.collect.ImmutableMap; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchParseElement; import org.elasticsearch.search.fetch.FetchSubPhase; import org.elasticsearch.search.internal.InternalSearchHit; import org.elasticsearch.search.internal.SearchContext; +import org.elasticsearch.search.lookup.SourceLookup; import java.io.IOException; import java.util.Map; @@ -71,9 +73,12 @@ public void hitExecute(SearchContext context, HitContext hitContext) throws Elas return; } - Object value = context.lookup().source().filter(fetchSourceContext.includes(), fetchSourceContext.excludes()); + SourceLookup source = context.lookup().source(); + Object value = source.filter(fetchSourceContext.includes(), fetchSourceContext.excludes()); try { - XContentBuilder builder = XContentFactory.contentBuilder(context.lookup().source().sourceContentType()); + final int initialCapacity = Math.min(1024, source.internalSourceRef().length()); + BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); + XContentBuilder builder = new XContentBuilder(context.lookup().source().sourceContentType().xContent(), streamOutput); builder.value(value); hitContext.hit().sourceRef(builder.bytes()); } catch (IOException e) {