From ceb0138aa8d57bc8dd9660c6d6306b2eb7197a92 Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 30 Apr 2010 00:08:33 +0300 Subject: [PATCH] better handling of source return value based on content type (embed it if its the same content type) --- .../org/elasticsearch/rest/action/get/RestGetAction.java | 8 +++++++- .../elasticsearch/search/internal/InternalSearchHit.java | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) 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()) {