|
@@ -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.ToXContentFragment;
|
|
|
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 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment {
|
|
|
}
|
|
|
|
|
|
public static EnrichPolicy 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();
|
|
|
+ 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 final class EnrichPolicy implements Writeable, ToXContentFragment {
|
|
|
|
|
|
@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 final class EnrichPolicy implements Writeable, ToXContentFragment {
|
|
|
|
|
|
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 final class EnrichPolicy implements Writeable, ToXContentFragment {
|
|
|
@Override
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
builder.startObject();
|
|
|
+ builder.startObject(policy.type);
|
|
|
{
|
|
|
builder.field(NAME.getPreferredName(), name);
|
|
|
- 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
|