getSourceAsString() for doc inserted as SMILE fails, auto convert to JSON, closes #2064.

This commit is contained in:
Shay Banon 2012-06-28 14:17:58 +02:00
parent a872c88f03
commit 07454243e3
5 changed files with 59 additions and 5 deletions

View File

@ -37,7 +37,6 @@ import java.util.Map;
/** /**
* The response of a get action. * The response of a get action.
* *
*
* @see GetRequest * @see GetRequest
* @see org.elasticsearch.client.Client#get(GetRequest) * @see org.elasticsearch.client.Client#get(GetRequest)
*/ */
@ -129,6 +128,13 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
return getResult.source(); return getResult.source();
} }
/**
* The source of the document if exists.
*/
public byte[] getSourceAsBytes() {
return source();
}
/** /**
* Returns bytes reference, also un compress the source if needed. * Returns bytes reference, also un compress the source if needed.
*/ */
@ -136,6 +142,13 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
return getResult.sourceRef(); return getResult.sourceRef();
} }
/**
* Returns bytes reference, also un compress the source if needed.
*/
public BytesHolder getSourceAsBytesRef() {
return sourceRef();
}
/** /**
* Is the source empty (not available) or not. * Is the source empty (not available) or not.
*/ */
@ -150,6 +163,10 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
return getResult.sourceAsString(); return getResult.sourceAsString();
} }
public String getSourceAsString() {
return sourceAsString();
}
/** /**
* The source of the document (As a map). * The source of the document (As a map).
*/ */

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.xcontent;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.compress.CompressedStreamInput; import org.elasticsearch.common.compress.CompressedStreamInput;
import org.elasticsearch.common.compress.Compressor; import org.elasticsearch.common.compress.Compressor;
@ -80,6 +81,14 @@ public class XContentHelper {
} }
} }
public static String convertToJson(BytesHolder bytes, boolean reformatJson) throws IOException {
return convertToJson(bytes.bytes(), bytes.offset(), bytes.length(), reformatJson);
}
public static String convertToJson(BytesHolder bytes, boolean reformatJson, boolean prettyPrint) throws IOException {
return convertToJson(bytes.bytes(), bytes.offset(), bytes.length(), reformatJson, prettyPrint);
}
public static String convertToJson(byte[] data, int offset, int length, boolean reformatJson) throws IOException { public static String convertToJson(byte[] data, int offset, int length, boolean reformatJson) throws IOException {
return convertToJson(data, offset, length, reformatJson, false); return convertToJson(data, offset, length, reformatJson, false);
} }

View File

@ -22,7 +22,6 @@ package org.elasticsearch.index.get;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.BytesHolder; import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -30,6 +29,7 @@ import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.search.lookup.SourceLookup; import org.elasticsearch.search.lookup.SourceLookup;
@ -197,7 +197,11 @@ public class GetResult implements Streamable, Iterable<GetField>, ToXContent {
return null; return null;
} }
BytesHolder source = sourceRef(); 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");
}
} }
/** /**

View File

@ -90,6 +90,11 @@ public interface SearchHit extends Streamable, ToXContent, Iterable<SearchHitFie
*/ */
BytesHolder sourceRef(); BytesHolder sourceRef();
/**
* Returns bytes reference, also un compress the source if needed.
*/
BytesHolder getSourceRef();
/** /**
* The source of the document (can be <tt>null</tt>). Note, its a copy of the source * The source of the document (can be <tt>null</tt>). Note, its a copy of the source
* into a byte array, consider using {@link #sourceRef()} so there won't be a need to copy. * 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, Iterable<SearchHitFie
*/ */
String sourceAsString(); String sourceAsString();
/**
* The source of the document as string (can be <tt>null</tt>).
*/
String getSourceAsString();
/** /**
* The source of the document as a map (can be <tt>null</tt>). * The source of the document as a map (can be <tt>null</tt>).
*/ */

View File

@ -25,12 +25,12 @@ import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.BytesHolder; import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.compress.CompressorFactory; import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.rest.action.support.RestXContentBuilder; import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField; 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.... * Internal source representation, might be compressed....
*/ */
@ -209,7 +214,16 @@ public class InternalSearchHit implements SearchHit {
return null; return null;
} }
BytesHolder source = sourceRef(); 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"}) @SuppressWarnings({"unchecked"})