|
40 | 40 | import org.elasticsearch.index.shard.ShardId; |
41 | 41 | import org.elasticsearch.ingest.ESONSource; |
42 | 42 | import org.elasticsearch.ingest.IngestService; |
43 | | -import org.elasticsearch.plugins.internal.XContentParserDecorator; |
| 43 | +import org.elasticsearch.xcontent.ToXContent; |
44 | 44 | import org.elasticsearch.xcontent.XContentBuilder; |
45 | 45 | import org.elasticsearch.xcontent.XContentFactory; |
| 46 | +import org.elasticsearch.xcontent.XContentParser; |
| 47 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
46 | 48 | import org.elasticsearch.xcontent.XContentType; |
47 | 49 |
|
48 | 50 | import java.io.IOException; |
| 51 | +import java.io.UncheckedIOException; |
49 | 52 | import java.util.ArrayList; |
50 | 53 | import java.util.Collections; |
51 | 54 | import java.util.List; |
@@ -100,7 +103,8 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement |
100 | 103 | private String routing; |
101 | 104 |
|
102 | 105 | private BytesReference source; |
103 | | - private ESONSource structuredSource; |
| 106 | + private ESONSource.ESONObject structuredSource; |
| 107 | + private boolean useStructuredSource = false; |
104 | 108 |
|
105 | 109 | private OpType opType = OpType.INDEX; |
106 | 110 |
|
@@ -414,20 +418,31 @@ public boolean isPipelineResolved() { |
414 | 418 | * The source of the document to index, recopied to a new array if it is unsafe. |
415 | 419 | */ |
416 | 420 | public BytesReference source() { |
| 421 | + if (useStructuredSource && source == null) { |
| 422 | + try { |
| 423 | + XContentBuilder builder = XContentFactory.contentBuilder(contentType); |
| 424 | + structuredSource.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 425 | + source = BytesReference.bytes(builder); |
| 426 | + } catch (IOException e) { |
| 427 | + throw new UncheckedIOException(e); |
| 428 | + } |
| 429 | + } |
417 | 430 | return source; |
418 | 431 | } |
419 | 432 |
|
420 | | - public void setStructuredSource(ESONSource esonSource) { |
| 433 | + public void setStructuredSource(ESONSource.ESONObject esonSource) { |
| 434 | + this.source = null; |
421 | 435 | this.structuredSource = esonSource; |
422 | | - source = null; |
| 436 | + this.useStructuredSource = true; |
423 | 437 | } |
424 | 438 |
|
425 | 439 | public Map<String, Object> sourceAsMap() { |
426 | | - return XContentHelper.convertToMap(source, false, contentType).v2(); |
427 | | - } |
428 | | - |
429 | | - public Map<String, Object> sourceAsMap(XContentParserDecorator parserDecorator) { |
430 | | - return XContentHelper.convertToMap(source, false, contentType, parserDecorator).v2(); |
| 440 | + if (useStructuredSource) { |
| 441 | + assert structuredSource != null; |
| 442 | + return structuredSource; |
| 443 | + } else { |
| 444 | + return XContentHelper.convertToMap(source, false, contentType).v2(); |
| 445 | + } |
431 | 446 | } |
432 | 447 |
|
433 | 448 | /** |
@@ -546,6 +561,15 @@ public static XContentBuilder getXContentBuilder(XContentType xContentType, Obje |
546 | 561 | public IndexRequest source(BytesReference source, XContentType xContentType) { |
547 | 562 | this.source = Objects.requireNonNull(source); |
548 | 563 | this.contentType = Objects.requireNonNull(xContentType); |
| 564 | + if (useStructuredSource) { |
| 565 | + ESONSource.Builder builder = new ESONSource.Builder(false); |
| 566 | + try { |
| 567 | + XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, xContentType); |
| 568 | + structuredSource = builder.parse(parser); |
| 569 | + } catch (IOException e) { |
| 570 | + throw new UncheckedIOException(e); |
| 571 | + } |
| 572 | + } |
549 | 573 | return this; |
550 | 574 | } |
551 | 575 |
|
@@ -776,7 +800,7 @@ private void writeBody(StreamOutput out) throws IOException { |
776 | 800 | } |
777 | 801 | out.writeOptionalString(id); |
778 | 802 | out.writeOptionalString(routing); |
779 | | - out.writeBytesReference(source); |
| 803 | + out.writeBytesReference(source()); |
780 | 804 | out.writeByte(opType.getId()); |
781 | 805 | out.writeLong(version); |
782 | 806 | out.writeByte(versionType.getValue()); |
@@ -924,7 +948,7 @@ public Index getConcreteWriteIndex(IndexAbstraction ia, ProjectMetadata project) |
924 | 948 |
|
925 | 949 | @Override |
926 | 950 | public int route(IndexRouting indexRouting) { |
927 | | - return indexRouting.indexShard(id, routing, contentType, source); |
| 951 | + return indexRouting.indexShard(id, routing, contentType, source()); |
928 | 952 | } |
929 | 953 |
|
930 | 954 | public IndexRequest setRequireAlias(boolean requireAlias) { |
|
0 commit comments