Skip to content

Commit ceb0138

Browse files
committed
better handling of source return value based on content type (embed it if its the same content type)
1 parent 34d99c3 commit ceb0138

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/get/RestGetAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.rest.*;
2828
import org.elasticsearch.util.guice.inject.Inject;
2929
import org.elasticsearch.util.settings.Settings;
30+
import org.elasticsearch.util.xcontent.XContentFactory;
3031
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
3132

3233
import java.io.IOException;
@@ -87,7 +88,12 @@ public class RestGetAction extends BaseRestHandler {
8788
builder.field("_type", response.type());
8889
builder.field("_id", response.id());
8990
if (response.source() != null) {
90-
builder.rawField("_source", response.source());
91+
if (builder.contentType() == XContentFactory.xContentType(response.source())) {
92+
builder.rawField("_source", response.source());
93+
} else {
94+
builder.field("_source");
95+
builder.value(response.source());
96+
}
9197
}
9298

9399
if (response.fields() != null && !response.fields().isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ public void shard(SearchShardTarget target) {
206206
builder.field("_id", id());
207207
if (source() != null) {
208208
if (XContentFactory.xContentType(source()) == builder.contentType()) {
209+
builder.rawField("_source", source());
210+
} else {
209211
builder.field("_source");
210212
builder.value(source());
211-
} else {
212-
builder.rawField("_source", source());
213213
}
214214
}
215215
if (fields != null && !fields.isEmpty()) {

0 commit comments

Comments
 (0)