diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java index b2a57bf8e08..72c879398cf 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java @@ -27,6 +27,7 @@ import org.elasticsearch.client.Client; import org.elasticsearch.rest.*; import org.elasticsearch.util.guice.inject.Inject; import org.elasticsearch.util.settings.Settings; +import org.elasticsearch.util.xcontent.XContentFactory; import org.elasticsearch.util.xcontent.builder.XContentBuilder; import java.io.IOException; @@ -87,7 +88,12 @@ public class RestGetAction extends BaseRestHandler { builder.field("_type", response.type()); builder.field("_id", response.id()); if (response.source() != null) { - builder.rawField("_source", response.source()); + if (builder.contentType() == XContentFactory.xContentType(response.source())) { + builder.rawField("_source", response.source()); + } else { + builder.field("_source"); + builder.value(response.source()); + } } if (response.fields() != null && !response.fields().isEmpty()) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java index 1e17bc20bf5..ac16f7e6493 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java @@ -206,10 +206,10 @@ public class InternalSearchHit implements SearchHit { builder.field("_id", id()); if (source() != null) { if (XContentFactory.xContentType(source()) == builder.contentType()) { + builder.rawField("_source", source()); + } else { builder.field("_source"); builder.value(source()); - } else { - builder.rawField("_source", source()); } } if (fields != null && !fields.isEmpty()) {