|
30 | 30 | import org.elasticsearch.index.IndexVersion; |
31 | 31 | import org.elasticsearch.index.IndexVersions; |
32 | 32 | import org.elasticsearch.index.mapper.TimeSeriesRoutingHashFieldMapper; |
| 33 | +import org.elasticsearch.ingest.ESONSource; |
| 34 | +import org.elasticsearch.ingest.ESONXContentParser; |
33 | 35 | import org.elasticsearch.transport.Transports; |
| 36 | +import org.elasticsearch.xcontent.DeprecationHandler; |
| 37 | +import org.elasticsearch.xcontent.NamedXContentRegistry; |
34 | 38 | import org.elasticsearch.xcontent.XContentParser; |
35 | 39 | import org.elasticsearch.xcontent.XContentParser.Token; |
36 | 40 | import org.elasticsearch.xcontent.XContentParserConfiguration; |
@@ -100,6 +104,8 @@ public void postProcess(IndexRequest indexRequest) {} |
100 | 104 | */ |
101 | 105 | public abstract int indexShard(String id, @Nullable String routing, XContentType sourceType, BytesReference source); |
102 | 106 |
|
| 107 | + public abstract int indexShard(String id, @Nullable String routing, XContentType sourceType, ESONSource.ESONObject structuredSource); |
| 108 | + |
103 | 109 | /** |
104 | 110 | * Called when updating a document to generate the shard id that should contain |
105 | 111 | * a document with the provided {@code _id} and (optional) {@code _routing}. |
@@ -220,6 +226,11 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy |
220 | 226 | return rerouteIfResharding(shardId); |
221 | 227 | } |
222 | 228 |
|
| 229 | + @Override |
| 230 | + public int indexShard(String id, String routing, XContentType sourceType, ESONSource.ESONObject structuredSource) { |
| 231 | + return indexShard(id, routing, sourceType, (BytesReference) null); |
| 232 | + } |
| 233 | + |
223 | 234 | @Override |
224 | 235 | public int updateShard(String id, @Nullable String routing) { |
225 | 236 | checkRoutingRequired(id, routing); |
@@ -342,6 +353,26 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy |
342 | 353 | return (rerouteIfResharding(shardId)); |
343 | 354 | } |
344 | 355 |
|
| 356 | + @Override |
| 357 | + public int indexShard(String id, @Nullable String routing, XContentType sourceType, ESONSource.ESONObject structuredSource) { |
| 358 | + assert Transports.assertNotTransportThread("parsing the _source can get slow"); |
| 359 | + checkNoRouting(routing); |
| 360 | + try ( |
| 361 | + XContentParser parser = new ESONXContentParser( |
| 362 | + structuredSource, |
| 363 | + NamedXContentRegistry.EMPTY, |
| 364 | + DeprecationHandler.IGNORE_DEPRECATIONS, |
| 365 | + sourceType |
| 366 | + ) |
| 367 | + ) { |
| 368 | + hash = hashSource(parser).buildHash(IndexRouting.ExtractFromSource::defaultOnEmpty); |
| 369 | + int shardId = hashToShardId(hash); |
| 370 | + return (rerouteIfResharding(shardId)); |
| 371 | + } catch (IOException | ParsingException e) { |
| 372 | + throw new IllegalArgumentException("Error extracting routing: " + e.getMessage(), e); |
| 373 | + } |
| 374 | + } |
| 375 | + |
345 | 376 | public String createId(XContentType sourceType, BytesReference source, byte[] suffix) { |
346 | 377 | return hashSource(sourceType, source).createId(suffix, IndexRouting.ExtractFromSource::defaultOnEmpty); |
347 | 378 | } |
@@ -371,18 +402,22 @@ public Builder builder() { |
371 | 402 | } |
372 | 403 |
|
373 | 404 | private Builder hashSource(XContentType sourceType, BytesReference source) { |
374 | | - Builder b = builder(); |
375 | 405 | try (XContentParser parser = XContentHelper.createParserNotCompressed(parserConfig, source, sourceType)) { |
376 | | - parser.nextToken(); // Move to first token |
377 | | - if (parser.currentToken() == null) { |
378 | | - throw new IllegalArgumentException("Error extracting routing: source didn't contain any routing fields"); |
379 | | - } |
380 | | - parser.nextToken(); |
381 | | - b.extractObject(null, parser); |
382 | | - ensureExpectedToken(null, parser.nextToken(), parser); |
| 406 | + return hashSource(parser); |
383 | 407 | } catch (IOException | ParsingException e) { |
384 | 408 | throw new IllegalArgumentException("Error extracting routing: " + e.getMessage(), e); |
385 | 409 | } |
| 410 | + } |
| 411 | + |
| 412 | + private Builder hashSource(XContentParser parser) throws IOException { |
| 413 | + Builder b = builder(); |
| 414 | + parser.nextToken(); // Move to first token |
| 415 | + if (parser.currentToken() == null) { |
| 416 | + throw new IllegalArgumentException("Error extracting routing: source didn't contain any routing fields"); |
| 417 | + } |
| 418 | + parser.nextToken(); |
| 419 | + b.extractObject(null, parser); |
| 420 | + ensureExpectedToken(null, parser.nextToken(), parser); |
386 | 421 | return b; |
387 | 422 | } |
388 | 423 |
|
|
0 commit comments