-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Change how type is stored in an enrich policy. #45789
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
Changes from all commits
86c6597
9df41db
b9505ca
8c8a9ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| package org.elasticsearch.xpack.core.enrich; | ||
|
|
||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.ParsingException; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
|
|
@@ -17,6 +18,7 @@ | |
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser.Token; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
|
|
@@ -34,20 +36,21 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment { | |
| public static final String EXACT_MATCH_TYPE = "exact_match"; | ||
| public static final String[] SUPPORTED_POLICY_TYPES = new String[]{EXACT_MATCH_TYPE}; | ||
|
|
||
| private static final ParseField TYPE = new ParseField("type"); | ||
| private static final ParseField QUERY = new ParseField("query"); | ||
| private static final ParseField INDICES = new ParseField("indices"); | ||
| private static final ParseField MATCH_FIELD = new ParseField("match_field"); | ||
| private static final ParseField ENRICH_FIELDS = new ParseField("enrich_fields"); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static final ConstructingObjectParser<EnrichPolicy, Void> PARSER = new ConstructingObjectParser<>("policy", | ||
| args -> new EnrichPolicy( | ||
| (String) args[0], | ||
| (QuerySource) args[1], | ||
| (List<String>) args[2], | ||
| (String) args[3], | ||
| (List<String>) args[4] | ||
| private static final ConstructingObjectParser<EnrichPolicy, String> PARSER = new ConstructingObjectParser<>( | ||
| "policy", | ||
| false, | ||
| (args, policyType) -> new EnrichPolicy( | ||
| policyType, | ||
| (QuerySource) args[0], | ||
| (List<String>) args[1], | ||
| (String) args[2], | ||
| (List<String>) args[3] | ||
| ) | ||
| ); | ||
|
|
||
|
|
@@ -56,7 +59,6 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment { | |
| } | ||
|
|
||
| private static void declareParserOptions(ConstructingObjectParser<?, ?> parser) { | ||
| parser.declareString(ConstructingObjectParser.constructorArg(), TYPE); | ||
| parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { | ||
| XContentBuilder contentBuilder = XContentBuilder.builder(p.contentType().xContent()); | ||
| contentBuilder.generator().copyCurrentStructure(p); | ||
|
|
@@ -68,7 +70,24 @@ private static void declareParserOptions(ConstructingObjectParser<?, ?> parser) | |
| } | ||
|
|
||
| public static EnrichPolicy fromXContent(XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| Token token = parser.currentToken(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not super happy with this parsing code, but in ordere to use object parser, another class would need to be introduced for policy types and since there is currently only policy type, this seems overkill to me. I think the important thing here is that the policy format is future proof now. |
||
| if (token != Token.START_OBJECT) { | ||
| token = parser.nextToken(); | ||
| } | ||
| if (token != Token.START_OBJECT) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| token = parser.nextToken(); | ||
| if (token != Token.FIELD_NAME) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| String policyType = parser.currentName(); | ||
| EnrichPolicy policy = PARSER.parse(parser, policyType); | ||
| token = parser.nextToken(); | ||
| if (token != Token.END_OBJECT) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| return policy; | ||
| } | ||
|
|
||
| private final String type; | ||
|
|
@@ -134,14 +153,21 @@ public void writeTo(StreamOutput out) throws IOException { | |
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.field(TYPE.getPreferredName(), type); | ||
| builder.startObject(type); | ||
| { | ||
| toInnerXContent(builder); | ||
| } | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| private void toInnerXContent(XContentBuilder builder) throws IOException { | ||
| if (query != null) { | ||
| builder.field(QUERY.getPreferredName(), query.getQueryAsMap()); | ||
| } | ||
| builder.array(INDICES.getPreferredName(), indices.toArray(new String[0])); | ||
| builder.field(MATCH_FIELD.getPreferredName(), matchField); | ||
| builder.array(ENRICH_FIELDS.getPreferredName(), enrichFields.toArray(new String[0])); | ||
| return builder; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -222,14 +248,16 @@ public static class NamedPolicy implements Writeable, ToXContent { | |
|
|
||
| static final ParseField NAME = new ParseField("name"); | ||
| @SuppressWarnings("unchecked") | ||
| static final ConstructingObjectParser<NamedPolicy, Void> PARSER = new ConstructingObjectParser<>("named_policy", | ||
| args -> new NamedPolicy( | ||
| static final ConstructingObjectParser<NamedPolicy, String> PARSER = new ConstructingObjectParser<>( | ||
| "named_policy", | ||
| false, | ||
| (args, policyType) -> new NamedPolicy( | ||
| (String) args[0], | ||
| new EnrichPolicy((String) args[1], | ||
| (QuerySource) args[2], | ||
| (List<String>) args[3], | ||
| (String) args[4], | ||
| (List<String>) args[5]) | ||
| new EnrichPolicy(policyType, | ||
| (QuerySource) args[1], | ||
| (List<String>) args[2], | ||
| (String) args[3], | ||
| (List<String>) args[4]) | ||
| ) | ||
| ); | ||
|
|
||
|
|
@@ -268,16 +296,39 @@ public void writeTo(StreamOutput out) throws IOException { | |
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.startObject(policy.type); | ||
| { | ||
| builder.field(NAME.getPreferredName(), name); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should name appear outside the policy type object? I assumed not, because otherwise we have the name and policy type at the same level.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like name should stay within the policy's definition. The object above this will only have a few fields that are considered valid: the existing policy types. On top of that, I think the previously decided direction is to keep other metadata about the policy (like the es version it was created under) contained in the definition part. |
||
| policy.toXContent(builder, params); | ||
| policy.toInnerXContent(builder); | ||
| } | ||
| builder.endObject(); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| public static NamedPolicy fromXContent(XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| Token token = parser.currentToken(); | ||
| if (token != Token.START_OBJECT) { | ||
| token = parser.nextToken(); | ||
| } | ||
| if (token != Token.START_OBJECT) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| token = parser.nextToken(); | ||
| if (token != Token.FIELD_NAME) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| String policyType = parser.currentName(); | ||
| token = parser.nextToken(); | ||
| if (token != Token.START_OBJECT) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| NamedPolicy policy = PARSER.parse(parser, policyType); | ||
| token = parser.nextToken(); | ||
| if (token != Token.END_OBJECT) { | ||
| throw new ParsingException(parser.getTokenLocation(), "unexpected token"); | ||
| } | ||
| return policy; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have an example to includes the
query? (its fine if it is not part of the PR, but probably want it in there somewhere)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there is not. It makes sense to emphasise that in a special section with the fact that reference data can be read from multiple indices.