Skip to content

Commit 80473e3

Browse files
committed
Changes
1 parent 2b18de7 commit 80473e3

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
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: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,28 @@ public void setStructuredSource(ESONSource.ESONObject esonSource) {
434434
}
435435
}
436436

437+
public void ensureStructureSource() {
438+
if (useStructuredSource == false) {
439+
this.useStructuredSource = true;
440+
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
441+
try {
442+
XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, contentType);
443+
structuredSource = builder.parse(parser);
444+
} catch (IOException e) {
445+
throw new UncheckedIOException(e);
446+
}
447+
}
448+
449+
}
450+
437451
public boolean isStructuredSource() {
438452
return useStructuredSource;
439453
}
440454

455+
public ESONSource.ESONObject structuredSource() {
456+
return structuredSource;
457+
}
458+
441459
public Map<String, Object> sourceAsMap() {
442460
if (useStructuredSource) {
443461
assert structuredSource != null;
@@ -564,13 +582,7 @@ public IndexRequest source(BytesReference source, XContentType xContentType) {
564582
this.source = Objects.requireNonNull(source);
565583
this.contentType = Objects.requireNonNull(xContentType);
566584
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-
}
585+
ensureStructureSource();
574586
}
575587
return this;
576588
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.CharBuffer;
2424
import java.util.ArrayDeque;
2525
import java.util.Deque;
26+
import java.util.List;
2627

2728
/**
2829
* Simplified XContentParser for flattened ESON structures.
@@ -40,7 +41,7 @@ public class ESONXContentParser extends AbstractXContentParser {
4041
private final XContentType xContentType;
4142

4243
// Key array iteration state
43-
private final ESONSource.KeyEntry[] keyArray;
44+
private final List<ESONSource.KeyEntry> keyArray;
4445
private int currentIndex = 0;
4546

4647
// Current token state
@@ -85,7 +86,7 @@ public ESONXContentParser(
8586
this.xContentType = xContentType;
8687

8788
// Convert to array for efficient indexed access
88-
this.keyArray = root.getKeyArray().toArray(new ESONSource.KeyEntry[0]);
89+
this.keyArray = root.getKeyArray();
8990
}
9091

9192
@Override
@@ -108,20 +109,22 @@ public Token nextToken() throws IOException {
108109
currentValue = null;
109110
valueComputed = false;
110111

112+
int size = keyArray.size();
113+
111114
// First token - start root object
112115
if (currentToken == null) {
113-
if (currentIndex >= keyArray.length) {
116+
if (currentIndex >= size) {
114117
return null;
115118
}
116-
ESONSource.ObjectEntry rootEntry = (ESONSource.ObjectEntry) keyArray[currentIndex];
119+
ESONSource.ObjectEntry rootEntry = (ESONSource.ObjectEntry) keyArray.get(currentIndex);
117120
containerStack.push(new ContainerContext(ContainerContext.Type.OBJECT, rootEntry.fieldCount));
118121
currentIndex++;
119122
currentToken = Token.START_OBJECT;
120123
return currentToken;
121124
}
122125

123126
// Check if we've finished parsing
124-
if (containerStack.isEmpty() && currentIndex >= keyArray.length) {
127+
if (containerStack.isEmpty() && currentIndex >= size) {
125128
return null;
126129
}
127130

@@ -133,14 +136,14 @@ public Token nextToken() throws IOException {
133136
}
134137

135138
// If we've exhausted the array but still have containers, close them
136-
if (currentIndex >= keyArray.length) {
139+
if (currentIndex >= size) {
137140
ContainerContext ctx = containerStack.pop();
138141
currentToken = ctx.type == ContainerContext.Type.OBJECT ? Token.END_OBJECT : Token.END_ARRAY;
139142
return currentToken;
140143
}
141144

142145
// Process next entry
143-
ESONSource.KeyEntry entry = keyArray[currentIndex];
146+
ESONSource.KeyEntry entry = keyArray.get(currentIndex);
144147
ContainerContext currentContainer = containerStack.peek();
145148

146149
if (currentContainer == null) {

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878
import org.elasticsearch.threadpool.Scheduler;
7979
import org.elasticsearch.threadpool.ThreadPool;
8080
import org.elasticsearch.xcontent.XContentBuilder;
81-
import org.elasticsearch.xcontent.XContentParser;
82-
import org.elasticsearch.xcontent.XContentParserConfiguration;
8381

8482
import java.io.IOException;
8583
import java.util.ArrayList;
@@ -1341,21 +1339,15 @@ static String getProcessorName(Processor processor) {
13411339
* Builds a new ingest document from the passed-in index request.
13421340
*/
13431341
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-
}
1342+
request.ensureStructureSource();
1343+
return new IngestDocument(
1344+
request.index(),
1345+
request.id(),
1346+
request.version(),
1347+
request.routing(),
1348+
request.versionType(),
1349+
new MapStructuredSource(request.structuredSource())
1350+
);
13591351
}
13601352

13611353
/**

0 commit comments

Comments
 (0)