Skip to content

Commit beaae12

Browse files
committed
Always structure source
1 parent 9523da9 commit beaae12

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

server/src/main/java/org/elasticsearch/action/bulk/TransportAbstractBulkAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ private void forkAndExecute(Task task, BulkRequest bulkRequest, Executor executo
181181
executor.execute(new ActionRunnable<>(releasingListener) {
182182
@Override
183183
protected void doRun() throws IOException {
184+
for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
185+
if (actionRequest instanceof IndexRequest ir) {
186+
ir.ensureStructureSource();
187+
}
188+
}
184189
applyPipelinesAndDoInternalExecute(task, bulkRequest, executor, releasingListener);
185190
}
186191
});

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.util.RamUsageEstimator;
1313
import org.elasticsearch.ElasticsearchException;
1414
import org.elasticsearch.ElasticsearchGenerationException;
15+
import org.elasticsearch.ElasticsearchParseException;
1516
import org.elasticsearch.TransportVersion;
1617
import org.elasticsearch.TransportVersions;
1718
import org.elasticsearch.action.ActionRequestValidationException;
@@ -40,7 +41,9 @@
4041
import org.elasticsearch.index.shard.ShardId;
4142
import org.elasticsearch.ingest.ESONSource;
4243
import org.elasticsearch.ingest.ESONXContentSerializer;
44+
import org.elasticsearch.ingest.IngestDocument;
4345
import org.elasticsearch.ingest.IngestService;
46+
import org.elasticsearch.ingest.MapStructuredSource;
4447
import org.elasticsearch.xcontent.ToXContent;
4548
import org.elasticsearch.xcontent.XContentBuilder;
4649
import org.elasticsearch.xcontent.XContentFactory;
@@ -434,10 +437,28 @@ public void setStructuredSource(ESONSource.ESONObject esonSource) {
434437
}
435438
}
436439

440+
public void ensureStructureSource() {
441+
if (useStructuredSource == false) {
442+
this.useStructuredSource = true;
443+
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
444+
try {
445+
XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, contentType);
446+
structuredSource = builder.parse(parser);
447+
} catch (IOException e) {
448+
throw new UncheckedIOException(e);
449+
}
450+
}
451+
452+
}
453+
437454
public boolean isStructuredSource() {
438455
return useStructuredSource;
439456
}
440457

458+
public ESONSource.ESONObject structuredSource() {
459+
return structuredSource;
460+
}
461+
441462
public Map<String, Object> sourceAsMap() {
442463
if (useStructuredSource) {
443464
assert structuredSource != null;
@@ -564,13 +585,7 @@ public IndexRequest source(BytesReference source, XContentType xContentType) {
564585
this.source = Objects.requireNonNull(source);
565586
this.contentType = Objects.requireNonNull(xContentType);
566587
if (useStructuredSource) {
567-
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
568-
try {
569-
XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, xContentType);
570-
structuredSource = builder.parse(parser);
571-
} catch (IOException e) {
572-
throw new UncheckedIOException(e);
573-
}
588+
ensureStructureSource();
574589
}
575590
return this;
576591
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,21 +1341,15 @@ static String getProcessorName(Processor processor) {
13411341
* Builds a new ingest document from the passed-in index request.
13421342
*/
13431343
private static IngestDocument newIngestDocument(final IndexRequest request) {
1344-
BytesReference source = request.source();
1345-
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
1346-
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, request.getContentType());) {
1347-
ESONSource.ESONObject esonObject = builder.parse(parser);
1348-
return new IngestDocument(
1349-
request.index(),
1350-
request.id(),
1351-
request.version(),
1352-
request.routing(),
1353-
request.versionType(),
1354-
new MapStructuredSource(esonObject)
1355-
);
1356-
} catch (IOException e) {
1357-
throw new ElasticsearchParseException("Failed to parse content to type", e);
1358-
}
1344+
request.ensureStructureSource();
1345+
return new IngestDocument(
1346+
request.index(),
1347+
request.id(),
1348+
request.version(),
1349+
request.routing(),
1350+
request.versionType(),
1351+
new MapStructuredSource(request.structuredSource())
1352+
);
13591353
}
13601354

13611355
/**

0 commit comments

Comments
 (0)