Skip to content

Commit 4fb60b9

Browse files
committed
Change
1 parent 8ef499b commit 4fb60b9

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

server/src/main/java/org/elasticsearch/action/index/IndexRequest.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@
4040
import org.elasticsearch.index.shard.ShardId;
4141
import org.elasticsearch.ingest.ESONSource;
4242
import org.elasticsearch.ingest.IngestService;
43-
import org.elasticsearch.plugins.internal.XContentParserDecorator;
43+
import org.elasticsearch.xcontent.ToXContent;
4444
import org.elasticsearch.xcontent.XContentBuilder;
4545
import org.elasticsearch.xcontent.XContentFactory;
46+
import org.elasticsearch.xcontent.XContentParser;
47+
import org.elasticsearch.xcontent.XContentParserConfiguration;
4648
import org.elasticsearch.xcontent.XContentType;
4749

4850
import java.io.IOException;
51+
import java.io.UncheckedIOException;
4952
import java.util.ArrayList;
5053
import java.util.Collections;
5154
import java.util.List;
@@ -100,7 +103,8 @@ public class IndexRequest extends ReplicatedWriteRequest<IndexRequest> implement
100103
private String routing;
101104

102105
private BytesReference source;
103-
private ESONSource structuredSource;
106+
private ESONSource.ESONObject structuredSource;
107+
private boolean useStructuredSource = false;
104108

105109
private OpType opType = OpType.INDEX;
106110

@@ -414,20 +418,31 @@ public boolean isPipelineResolved() {
414418
* The source of the document to index, recopied to a new array if it is unsafe.
415419
*/
416420
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+
}
417430
return source;
418431
}
419432

420-
public void setStructuredSource(ESONSource esonSource) {
433+
public void setStructuredSource(ESONSource.ESONObject esonSource) {
434+
this.source = null;
421435
this.structuredSource = esonSource;
422-
source = null;
436+
this.useStructuredSource = true;
423437
}
424438

425439
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+
}
431446
}
432447

433448
/**
@@ -546,6 +561,15 @@ public static XContentBuilder getXContentBuilder(XContentType xContentType, Obje
546561
public IndexRequest source(BytesReference source, XContentType xContentType) {
547562
this.source = Objects.requireNonNull(source);
548563
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+
}
549573
return this;
550574
}
551575

@@ -776,7 +800,7 @@ private void writeBody(StreamOutput out) throws IOException {
776800
}
777801
out.writeOptionalString(id);
778802
out.writeOptionalString(routing);
779-
out.writeBytesReference(source);
803+
out.writeBytesReference(source());
780804
out.writeByte(opType.getId());
781805
out.writeLong(version);
782806
out.writeByte(versionType.getValue());
@@ -924,7 +948,7 @@ public Index getConcreteWriteIndex(IndexAbstraction ia, ProjectMetadata project)
924948

925949
@Override
926950
public int route(IndexRouting indexRouting) {
927-
return indexRouting.indexShard(id, routing, contentType, source);
951+
return indexRouting.indexShard(id, routing, contentType, source());
928952
}
929953

930954
public IndexRequest setRequireAlias(boolean requireAlias) {

server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.index.mapper.RoutingFieldMapper;
2929
import org.elasticsearch.index.shard.IndexShard;
3030
import org.elasticsearch.index.shard.ShardId;
31-
import org.elasticsearch.plugins.internal.XContentParserDecorator;
3231
import org.elasticsearch.script.Script;
3332
import org.elasticsearch.script.ScriptService;
3433
import org.elasticsearch.script.UpdateCtxMap;
@@ -193,11 +192,7 @@ Result prepareUpdateIndexRequest(IndexShard indexShard, UpdateRequest request, G
193192
final XContentType updateSourceContentType = sourceAndContent.v1();
194193
final Map<String, Object> updatedSourceAsMap = sourceAndContent.v2();
195194

196-
final boolean noop = XContentHelper.update(
197-
updatedSourceAsMap,
198-
currentRequest.sourceAsMap(XContentParserDecorator.NOOP),
199-
detectNoop
200-
) == false;
195+
final boolean noop = XContentHelper.update(updatedSourceAsMap, currentRequest.sourceAsMap(), detectNoop) == false;
201196

202197
// We can only actually turn the update into a noop if detectNoop is true to preserve backwards compatibility and to handle cases
203198
// where users repopulating multi-fields or adding synonyms, etc.

server/src/main/java/org/elasticsearch/ingest/IngestService.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.apache.logging.log4j.Logger;
1414
import org.apache.logging.log4j.util.Strings;
1515
import org.elasticsearch.ElasticsearchException;
16-
import org.elasticsearch.ElasticsearchGenerationException;
1716
import org.elasticsearch.ElasticsearchParseException;
1817
import org.elasticsearch.ExceptionsHelper;
1918
import org.elasticsearch.ResourceNotFoundException;
@@ -78,9 +77,7 @@
7877
import org.elasticsearch.script.ScriptService;
7978
import org.elasticsearch.threadpool.Scheduler;
8079
import org.elasticsearch.threadpool.ThreadPool;
81-
import org.elasticsearch.xcontent.ToXContent;
8280
import org.elasticsearch.xcontent.XContentBuilder;
83-
import org.elasticsearch.xcontent.XContentFactory;
8481
import org.elasticsearch.xcontent.XContentParser;
8582
import org.elasticsearch.xcontent.XContentParserConfiguration;
8683

@@ -1403,13 +1400,7 @@ private static void updateIndexRequestSource(final IndexRequest request, final I
14031400
assert ensureNoSelfReferences == false;
14041401
MapStructuredSource source = (MapStructuredSource) document.getSource();
14051402
ESONSource.ESONObject esonSource = (ESONSource.ESONObject) source.map();
1406-
try {
1407-
XContentBuilder builder = XContentFactory.contentBuilder(request.getContentType());
1408-
esonSource.toXContent(builder, ToXContent.EMPTY_PARAMS);
1409-
request.source(BytesReference.bytes(builder), builder.contentType());
1410-
} catch (IOException e) {
1411-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
1412-
}
1403+
request.setStructuredSource(esonSource);
14131404
}
14141405

14151406
/**

0 commit comments

Comments
 (0)