From 07454243e3f4795602baa1879edb6ee874577038 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Thu, 28 Jun 2012 14:17:58 +0200 Subject: [PATCH] getSourceAsString() for doc inserted as SMILE fails, auto convert to JSON, closes #2064. --- .../elasticsearch/action/get/GetResponse.java | 19 ++++++++++++++++++- .../common/xcontent/XContentHelper.java | 9 +++++++++ .../elasticsearch/index/get/GetResult.java | 8 ++++++-- .../org/elasticsearch/search/SearchHit.java | 10 ++++++++++ .../search/internal/InternalSearchHit.java | 18 ++++++++++++++++-- 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/get/GetResponse.java b/src/main/java/org/elasticsearch/action/get/GetResponse.java index 5ba4a524f3b..46c16c3a2bf 100644 --- a/src/main/java/org/elasticsearch/action/get/GetResponse.java +++ b/src/main/java/org/elasticsearch/action/get/GetResponse.java @@ -37,7 +37,6 @@ import java.util.Map; /** * The response of a get action. * - * * @see GetRequest * @see org.elasticsearch.client.Client#get(GetRequest) */ @@ -129,6 +128,13 @@ public class GetResponse implements ActionResponse, Streamable, Iterable, ToXContent { return null; } BytesHolder source = sourceRef(); - return Unicode.fromBytes(source.bytes(), source.offset(), source.length()); + try { + return XContentHelper.convertToJson(source, false); + } catch (IOException e) { + throw new ElasticSearchParseException("failed to convert source to a json string"); + } } /** diff --git a/src/main/java/org/elasticsearch/search/SearchHit.java b/src/main/java/org/elasticsearch/search/SearchHit.java index 89d167b9d02..1da3ca48319 100644 --- a/src/main/java/org/elasticsearch/search/SearchHit.java +++ b/src/main/java/org/elasticsearch/search/SearchHit.java @@ -90,6 +90,11 @@ public interface SearchHit extends Streamable, ToXContent, Iterablenull). Note, its a copy of the source * into a byte array, consider using {@link #sourceRef()} so there won't be a need to copy. @@ -111,6 +116,11 @@ public interface SearchHit extends Streamable, ToXContent, Iterablenull). + */ + String getSourceAsString(); + /** * The source of the document as a map (can be null). */ diff --git a/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java b/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java index dd0a9ab7852..d91eb2e6d9a 100644 --- a/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java +++ b/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java @@ -25,12 +25,12 @@ import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.common.BytesHolder; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.Unicode; import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; @@ -174,6 +174,11 @@ public class InternalSearchHit implements SearchHit { } } + @Override + public BytesHolder getSourceRef() { + return sourceRef(); + } + /** * Internal source representation, might be compressed.... */ @@ -209,7 +214,16 @@ public class InternalSearchHit implements SearchHit { return null; } BytesHolder source = sourceRef(); - return Unicode.fromBytes(source.bytes(), source.offset(), source.length()); + try { + return XContentHelper.convertToJson(source, false); + } catch (IOException e) { + throw new ElasticSearchParseException("failed to convert source to a json string"); + } + } + + @Override + public String getSourceAsString() { + return sourceAsString(); } @SuppressWarnings({"unchecked"})