Search in sources :

Example 16 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class BasePrimaryAllocationCommand method createAllocatePrimaryParser.

protected static <T extends Builder<?>> ObjectParser<T, Void> createAllocatePrimaryParser(String command) {
    ObjectParser<T, Void> parser = AbstractAllocateAllocationCommand.createAllocateParser(command);
    parser.declareBoolean(Builder::setAcceptDataLoss, new ParseField(ACCEPT_DATA_LOSS_FIELD));
    return parser;
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParseField(org.elasticsearch.common.ParseField)

Example 17 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class SignificantTermsAggregationBuilder method getParser.

public static Aggregator.Parser getParser(ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry) {
    ObjectParser<SignificantTermsAggregationBuilder, QueryParseContext> parser = new ObjectParser<>(SignificantTermsAggregationBuilder.NAME);
    ValuesSourceParserHelper.declareAnyFields(parser, true, true);
    parser.declareInt(SignificantTermsAggregationBuilder::shardSize, TermsAggregationBuilder.SHARD_SIZE_FIELD_NAME);
    parser.declareLong(SignificantTermsAggregationBuilder::minDocCount, TermsAggregationBuilder.MIN_DOC_COUNT_FIELD_NAME);
    parser.declareLong(SignificantTermsAggregationBuilder::shardMinDocCount, TermsAggregationBuilder.SHARD_MIN_DOC_COUNT_FIELD_NAME);
    parser.declareInt(SignificantTermsAggregationBuilder::size, TermsAggregationBuilder.REQUIRED_SIZE_FIELD_NAME);
    parser.declareString(SignificantTermsAggregationBuilder::executionHint, TermsAggregationBuilder.EXECUTION_HINT_FIELD_NAME);
    parser.declareObject(SignificantTermsAggregationBuilder::backgroundFilter, (p, context) -> context.parseInnerQueryBuilder(), SignificantTermsAggregationBuilder.BACKGROUND_FILTER);
    parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(v, b.includeExclude())), IncludeExclude::parseInclude, IncludeExclude.INCLUDE_FIELD, ObjectParser.ValueType.OBJECT_ARRAY_OR_STRING);
    parser.declareField((b, v) -> b.includeExclude(IncludeExclude.merge(b.includeExclude(), v)), IncludeExclude::parseExclude, IncludeExclude.EXCLUDE_FIELD, ObjectParser.ValueType.STRING_ARRAY);
    for (String name : significanceHeuristicParserRegistry.getNames()) {
        parser.declareObject(SignificantTermsAggregationBuilder::significanceHeuristic, (p, context) -> {
            SignificanceHeuristicParser significanceHeuristicParser = significanceHeuristicParserRegistry.lookupReturningNullIfNotFound(name);
            return significanceHeuristicParser.parse(context);
        }, new ParseField(name));
    }
    return new Aggregator.Parser() {

        @Override
        public AggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
            return parser.parse(context.parser(), new SignificantTermsAggregationBuilder(aggregationName, null), context);
        }
    };
}
Also used : ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) SignificanceHeuristicParser(org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser) ParseField(org.elasticsearch.common.ParseField) SignificanceHeuristicParser(org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser)

Example 18 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ValuesSourceParserHelper method declareFields.

private static <VS extends ValuesSource> void declareFields(ObjectParser<? extends ValuesSourceAggregationBuilder<VS, ?>, QueryParseContext> objectParser, boolean scriptable, boolean formattable, boolean timezoneAware, ValueType targetValueType) {
    objectParser.declareField(ValuesSourceAggregationBuilder::field, XContentParser::text, new ParseField("field"), ObjectParser.ValueType.STRING);
    objectParser.declareField(ValuesSourceAggregationBuilder::missing, XContentParser::objectText, new ParseField("missing"), ObjectParser.ValueType.VALUE);
    objectParser.declareField(ValuesSourceAggregationBuilder::valueType, p -> {
        ValueType valueType = ValueType.resolveForScript(p.text());
        if (targetValueType != null && valueType.isNotA(targetValueType)) {
            throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" + valueType + "]. It can only work on value of type [" + targetValueType + "]");
        }
        return valueType;
    }, new ParseField("value_type", "valueType"), ObjectParser.ValueType.STRING);
    if (formattable) {
        objectParser.declareField(ValuesSourceAggregationBuilder::format, XContentParser::text, new ParseField("format"), ObjectParser.ValueType.STRING);
    }
    if (scriptable) {
        objectParser.declareField(ValuesSourceAggregationBuilder::script, (parser, context) -> Script.parse(parser), Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING);
    }
    if (timezoneAware) {
        objectParser.declareField(ValuesSourceAggregationBuilder::timeZone, p -> {
            if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
                return DateTimeZone.forID(p.text());
            } else {
                return DateTimeZone.forOffsetHours(p.intValue());
            }
        }, TIME_ZONE, ObjectParser.ValueType.LONG);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParseField(org.elasticsearch.common.ParseField)

Example 19 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class ParseFieldRegistry method lookupReturningNullIfNotFound.

/**
     * Lookup a value from the registry by name while checking that the name matches the ParseField.
     *
     * @param name The name of the thing to look up.
     * @return The value being looked up or null if it wasn't found.
     * @throws ParsingException if the named thing isn't in the registry or the name was deprecated and deprecated names aren't supported.
     */
public T lookupReturningNullIfNotFound(String name) {
    Tuple<ParseField, T> parseFieldAndValue = registry.get(name);
    if (parseFieldAndValue == null) {
        return null;
    }
    ParseField parseField = parseFieldAndValue.v1();
    T value = parseFieldAndValue.v2();
    boolean match = parseField.match(name);
    //this is always expected to match, ParseField is useful for deprecation warnings etc. here
    assert match : "ParseField did not match registered name [" + name + "][" + registryName + "]";
    return value;
}
Also used : ParseField(org.elasticsearch.common.ParseField)

Example 20 with ParseField

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.ParseField in project elasticsearch by elastic.

the class MultiValuesSourceParser method parse.

@Override
public final MultiValuesSourceAggregationBuilder<VS, ?> parse(String aggregationName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    List<String> fields = null;
    ValueType valueType = null;
    String format = null;
    Map<String, Object> missingMap = null;
    Map<ParseField, Object> otherOptions = new HashMap<>();
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (CommonFields.FIELDS.match(currentFieldName)) {
                fields = Collections.singletonList(parser.text());
            } else if (formattable && CommonFields.FORMAT.match(currentFieldName)) {
                format = parser.text();
            } else if (CommonFields.VALUE_TYPE.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "]. " + "Multi-field aggregations do not support scripts.");
            } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (CommonFields.MISSING.match(currentFieldName)) {
                missingMap = new HashMap<>();
                while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                    parseMissingAndAdd(aggregationName, currentFieldName, parser, missingMap);
                }
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "]. " + "Multi-field aggregations do not support scripts.");
            } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "]. " + "Multi-field aggregations do not support scripts.");
            } else if (CommonFields.FIELDS.match(currentFieldName)) {
                fields = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    if (token == XContentParser.Token.VALUE_STRING) {
                        fields.add(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "].");
                    }
                }
            } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "].");
            }
        } else if (!token(aggregationName, currentFieldName, token, parser, otherOptions)) {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "].");
        }
    }
    MultiValuesSourceAggregationBuilder<VS, ?> factory = createFactory(aggregationName, this.valuesSourceType, this.targetValueType, otherOptions);
    if (fields != null) {
        factory.fields(fields);
    }
    if (valueType != null) {
        factory.valueType(valueType);
    }
    if (format != null) {
        factory.format(format);
    }
    if (missingMap != null) {
        factory.missingMap(missingMap);
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParseField(org.elasticsearch.common.ParseField)

Aggregations

ParseField (org.elasticsearch.common.ParseField)31 NamedObjectParser (org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser)17 ParsingException (org.elasticsearch.common.ParsingException)7 IOException (java.io.IOException)6 List (java.util.List)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 ArrayList (java.util.ArrayList)4 ObjectParser (org.elasticsearch.common.xcontent.ObjectParser)4 UncheckedIOException (java.io.UncheckedIOException)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 Map (java.util.Map)2 CheckedFunction (org.elasticsearch.common.CheckedFunction)2 Nullable (org.elasticsearch.common.Nullable)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 ConstructingObjectParser.constructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg)2 ConstructingObjectParser.optionalConstructorArg (org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg)2 ValueType (org.elasticsearch.common.xcontent.ObjectParser.ValueType)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2