From a76d914cfa4c4ff2a866b8f1539c5cb3ef48afbb Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 27 Mar 2012 14:57:29 +0200 Subject: [PATCH] Java API: Calling SearchHit#sourceAsString will not decompress the source to convert it to string, closes #1814. --- .../search/internal/InternalSearchHit.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java b/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java index 46a7bb7050e..489a4b6a9e3 100644 --- a/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java +++ b/src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java @@ -32,13 +32,12 @@ 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.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.search.highlight.HighlightField; +import org.elasticsearch.search.lookup.SourceLookup; import java.io.IOException; import java.util.Iterator; @@ -83,6 +82,7 @@ public class InternalSearchHit implements SearchHit { private SearchShardTarget shard; private Map sourceAsMap; + private byte[] sourceAsBytes; private InternalSearchHit() { @@ -162,11 +162,11 @@ public class InternalSearchHit implements SearchHit { return type(); } - @Override - public byte[] source() { - if (source == null) { - return null; - } + + /** + * Returns bytes reference, also un compress the source if needed. + */ + public BytesHolder sourceRef() { if (LZF.isCompressed(source.bytes(), source.offset(), source.length())) { try { this.source = new BytesHolder(LZFDecoder.decode(source.bytes(), source.offset(), source.length())); @@ -174,7 +174,26 @@ public class InternalSearchHit implements SearchHit { throw new ElasticSearchParseException("failed to decompress source", e); } } - return this.source.copyBytes(); + return this.source; + } + + /** + * Internal source representation, might be compressed.... + */ + public BytesHolder internalSourceRef() { + return source; + } + + @Override + public byte[] source() { + if (source == null) { + return null; + } + if (sourceAsBytes != null) { + return sourceAsBytes; + } + this.sourceAsBytes = sourceRef().copyBytes(); + return this.sourceAsBytes; } @Override @@ -192,6 +211,7 @@ public class InternalSearchHit implements SearchHit { if (source == null) { return null; } + BytesHolder source = sourceRef(); return Unicode.fromBytes(source.bytes(), source.offset(), source.length()); } @@ -204,20 +224,9 @@ public class InternalSearchHit implements SearchHit { if (sourceAsMap != null) { return sourceAsMap; } - byte[] source = source(); - XContentParser parser = null; - try { - parser = XContentFactory.xContent(source).createParser(source); - sourceAsMap = parser.map(); - parser.close(); - return sourceAsMap; - } catch (Exception e) { - throw new ElasticSearchParseException("Failed to parse source to map", e); - } finally { - if (parser != null) { - parser.close(); - } - } + + sourceAsMap = SourceLookup.sourceAsMap(source.bytes(), source.offset(), source.length()); + return sourceAsMap; } @Override