Skip to content

Commit b87bc4d

Browse files
committed
getSourceAsString() for doc inserted as SMILE fails, auto convert to JSON, closes elastic#2064.
1 parent d08a408 commit b87bc4d

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

src/main/java/org/elasticsearch/action/get/GetResponse.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
/**
3838
* The response of a get action.
3939
*
40-
*
4140
* @see GetRequest
4241
* @see org.elasticsearch.client.Client#get(GetRequest)
4342
*/
@@ -129,13 +128,27 @@ public byte[] source() {
129128
return getResult.source();
130129
}
131130

131+
/**
132+
* The source of the document if exists.
133+
*/
134+
public byte[] getSourceAsBytes() {
135+
return source();
136+
}
137+
132138
/**
133139
* Returns bytes reference, also un compress the source if needed.
134140
*/
135141
public BytesHolder sourceRef() {
136142
return getResult.sourceRef();
137143
}
138144

145+
/**
146+
* Returns bytes reference, also un compress the source if needed.
147+
*/
148+
public BytesHolder getSourceAsBytesRef() {
149+
return sourceRef();
150+
}
151+
139152
/**
140153
* Is the source empty (not available) or not.
141154
*/
@@ -150,6 +163,10 @@ public String sourceAsString() {
150163
return getResult.sourceAsString();
151164
}
152165

166+
public String getSourceAsString() {
167+
return sourceAsString();
168+
}
169+
153170
/**
154171
* The source of the document (As a map).
155172
*/

src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.base.Charsets;
2323
import com.google.common.collect.Maps;
2424
import org.elasticsearch.ElasticSearchParseException;
25+
import org.elasticsearch.common.BytesHolder;
2526
import org.elasticsearch.common.collect.Tuple;
2627
import org.elasticsearch.common.compress.CompressedStreamInput;
2728
import org.elasticsearch.common.compress.Compressor;
@@ -75,6 +76,14 @@ public static Tuple<XContentType, Map<String, Object>> convertToMap(byte[] data,
7576
}
7677
}
7778

79+
public static String convertToJson(BytesHolder bytes, boolean reformatJson) throws IOException {
80+
return convertToJson(bytes.bytes(), bytes.offset(), bytes.length(), reformatJson);
81+
}
82+
83+
public static String convertToJson(BytesHolder bytes, boolean reformatJson, boolean prettyPrint) throws IOException {
84+
return convertToJson(bytes.bytes(), bytes.offset(), bytes.length(), reformatJson, prettyPrint);
85+
}
86+
7887
public static String convertToJson(byte[] data, int offset, int length, boolean reformatJson) throws IOException {
7988
return convertToJson(data, offset, length, reformatJson, false);
8089
}

src/main/java/org/elasticsearch/index/get/GetResult.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import org.elasticsearch.ElasticSearchParseException;
2424
import org.elasticsearch.common.BytesHolder;
25-
import org.elasticsearch.common.Unicode;
2625
import org.elasticsearch.common.compress.CompressorFactory;
2726
import org.elasticsearch.common.io.stream.StreamInput;
2827
import org.elasticsearch.common.io.stream.StreamOutput;
2928
import org.elasticsearch.common.io.stream.Streamable;
3029
import org.elasticsearch.common.xcontent.ToXContent;
3130
import org.elasticsearch.common.xcontent.XContentBuilder;
3231
import org.elasticsearch.common.xcontent.XContentBuilderString;
32+
import org.elasticsearch.common.xcontent.XContentHelper;
3333
import org.elasticsearch.rest.action.support.RestXContentBuilder;
3434
import org.elasticsearch.search.lookup.SourceLookup;
3535

@@ -197,7 +197,11 @@ public String sourceAsString() {
197197
return null;
198198
}
199199
BytesHolder source = sourceRef();
200-
return Unicode.fromBytes(source.bytes(), source.offset(), source.length());
200+
try {
201+
return XContentHelper.convertToJson(source, false);
202+
} catch (IOException e) {
203+
throw new ElasticSearchParseException("failed to convert source to a json string");
204+
}
201205
}
202206

203207
/**

src/main/java/org/elasticsearch/search/SearchHit.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public interface SearchHit extends Streamable, ToXContent, Iterable<SearchHitFie
9090
*/
9191
BytesHolder sourceRef();
9292

93+
/**
94+
* Returns bytes reference, also un compress the source if needed.
95+
*/
96+
BytesHolder getSourceRef();
97+
9398
/**
9499
* The source of the document (can be <tt>null</tt>). Note, its a copy of the source
95100
* 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
111116
*/
112117
String sourceAsString();
113118

119+
/**
120+
* The source of the document as string (can be <tt>null</tt>).
121+
*/
122+
String getSourceAsString();
123+
114124
/**
115125
* The source of the document as a map (can be <tt>null</tt>).
116126
*/

src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import org.elasticsearch.common.BytesHolder;
2626
import org.elasticsearch.common.Nullable;
2727
import org.elasticsearch.common.Strings;
28-
import org.elasticsearch.common.Unicode;
2928
import org.elasticsearch.common.compress.CompressorFactory;
3029
import org.elasticsearch.common.io.stream.StreamInput;
3130
import org.elasticsearch.common.io.stream.StreamOutput;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
3332
import org.elasticsearch.common.xcontent.XContentBuilderString;
33+
import org.elasticsearch.common.xcontent.XContentHelper;
3434
import org.elasticsearch.rest.action.support.RestXContentBuilder;
3535
import org.elasticsearch.search.SearchHit;
3636
import org.elasticsearch.search.SearchHitField;
@@ -174,6 +174,11 @@ public BytesHolder sourceRef() {
174174
}
175175
}
176176

177+
@Override
178+
public BytesHolder getSourceRef() {
179+
return sourceRef();
180+
}
181+
177182
/**
178183
* Internal source representation, might be compressed....
179184
*/
@@ -209,7 +214,16 @@ public String sourceAsString() {
209214
return null;
210215
}
211216
BytesHolder source = sourceRef();
212-
return Unicode.fromBytes(source.bytes(), source.offset(), source.length());
217+
try {
218+
return XContentHelper.convertToJson(source, false);
219+
} catch (IOException e) {
220+
throw new ElasticSearchParseException("failed to convert source to a json string");
221+
}
222+
}
223+
224+
@Override
225+
public String getSourceAsString() {
226+
return sourceAsString();
213227
}
214228

215229
@SuppressWarnings({"unchecked"})

0 commit comments

Comments
 (0)