-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add an option to replace _source field values with synthetic references #113036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
85dad44
Always build SourceProvider from the mapping lookup
jimczi be076e6
license
jimczi 223d852
Add a way to replace the value for a specific field in the original s…
jimczi 1bb7c4d
fix license
jimczi dd017ea
fix test
jimczi 0d49121
spotless
jimczi 4754f47
fix tests
jimczi a5ebbcd
fix another test
jimczi caea069
cosmetic
jimczi d96cb8d
Merge remote-tracking branch 'upstream/main' into patch_source_mapping
jimczi ba6e5ca
Add a benchmark and the support to filter source fields when loading …
jimczi 37e68fa
Merge remote-tracking branch 'upstream/main' into patch_source_mapping
jimczi 1b1a352
fix compil
jimczi 136c699
spotless
jimczi 2f71bec
Update docs/changelog/113036.yaml
jimczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
server/src/main/java/org/elasticsearch/index/mapper/PatchSourceUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.mapper; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.xcontent.XContent; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentGenerator; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Set; | ||
|
|
||
| public class PatchSourceUtils { | ||
| public interface CheckedTriConsumer<S, T, U> { | ||
| void apply(S s, T t, U u) throws IOException; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the given {@code source} and returns a new version with all values referenced by the | ||
| * provided {@code patchFullPaths} replaced using the {@code patchApply} consumer. | ||
| */ | ||
| public static BytesReference patchSource( | ||
| BytesReference source, | ||
| XContent xContent, | ||
| Set<String> patchFullPaths, | ||
| CheckedTriConsumer<String, XContentParser, XContentGenerator> patchApply | ||
| ) throws IOException { | ||
| BytesStreamOutput streamOutput = new BytesStreamOutput(); | ||
| XContentBuilder builder = new XContentBuilder(xContent, streamOutput); | ||
| try (XContentParser parser = XContentHelper.createParserNotCompressed(XContentParserConfiguration.EMPTY, source, xContent.type())) { | ||
| if ((parser.currentToken() == null) && (parser.nextToken() == null)) { | ||
| return source; | ||
| } | ||
| parseAndPatchSource(builder.generator(), parser, "", patchFullPaths, patchApply); | ||
| return BytesReference.bytes(builder); | ||
| } | ||
| } | ||
|
|
||
| private static void parseAndPatchSource( | ||
| XContentGenerator destination, | ||
| XContentParser parser, | ||
| String fullPath, | ||
| Set<String> patchFullPaths, | ||
| CheckedTriConsumer<String, XContentParser, XContentGenerator> patchApply | ||
| ) throws IOException { | ||
| XContentParser.Token token = parser.currentToken(); | ||
| if (token == XContentParser.Token.FIELD_NAME) { | ||
| String fieldName = parser.currentName(); | ||
| destination.writeFieldName(fieldName); | ||
| token = parser.nextToken(); | ||
| fullPath = fullPath + (fullPath.isEmpty() ? "" : ".") + fieldName; | ||
| if (patchFullPaths.contains(fullPath)) { | ||
| patchApply.apply(fullPath, parser, destination); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| switch (token) { | ||
| case START_ARRAY -> { | ||
| destination.writeStartArray(); | ||
| while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
| parseAndPatchSource(destination, parser, fullPath, patchFullPaths, patchApply); | ||
| } | ||
| destination.writeEndArray(); | ||
| } | ||
| case START_OBJECT -> { | ||
| destination.writeStartObject(); | ||
| while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| parseAndPatchSource(destination, parser, fullPath, patchFullPaths, patchApply); | ||
| } | ||
| destination.writeEndObject(); | ||
| } | ||
| default -> // others are simple: | ||
| destination.copyCurrentEvent(parser); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.