Search in sources :

Example 66 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class CompositeValuesSourceParserHelper method declareValuesSourceFields.

static <VB extends CompositeValuesSourceBuilder<VB>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> objectParser) {
    objectParser.declareField(VB::field, XContentParser::text, new ParseField("field"), ObjectParser.ValueType.STRING);
    objectParser.declareBoolean(VB::missingBucket, new ParseField("missing_bucket"));
    objectParser.declareString(VB::missingOrder, new ParseField(MissingOrder.NAME));
    objectParser.declareField(VB::userValuetypeHint, p -> {
        ValueType valueType = ValueType.lenientParse(p.text());
        return valueType;
    }, new ParseField("value_type"), ObjectParser.ValueType.STRING);
    objectParser.declareField(VB::script, (parser, context) -> Script.parse(parser), Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING);
    objectParser.declareField(VB::order, XContentParser::text, new ParseField("order"), ObjectParser.ValueType.STRING);
}
Also used : ValueType(org.opensearch.search.aggregations.support.ValueType) XContentParser(org.opensearch.common.xcontent.XContentParser) ParseField(org.opensearch.common.ParseField)

Example 67 with ParseField

use of org.opensearch.common.ParseField in project OpenSearch by opensearch-project.

the class ArrayValuesSourceParser method parse.

@Override
public final ArrayValuesSourceAggregationBuilder<?> parse(String aggregationName, XContentParser parser) throws IOException {
    List<String> fields = 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, parser.getDeprecationHandler())) {
                fields = Collections.singletonList(parser.text());
            } else if (formattable && CommonFields.FORMAT.match(currentFieldName, parser.getDeprecationHandler())) {
                format = parser.text();
            } else if (CommonFields.VALUE_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                missingMap = new HashMap<>();
                while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                    parseMissingAndAdd(aggregationName, currentFieldName, parser, missingMap);
                }
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " [" + currentFieldName + "] in [" + aggregationName + "]. " + "Multi-field aggregations do not support scripts.");
            } else if (CommonFields.FIELDS.match(currentFieldName, parser.getDeprecationHandler())) {
                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 + "].");
        }
    }
    ArrayValuesSourceAggregationBuilder<?> factory = createFactory(aggregationName, this.valuesSourceType, this.targetValueType, otherOptions);
    if (fields != null) {
        factory.fields(fields);
    }
    if (format != null) {
        factory.format(format);
    }
    if (missingMap != null) {
        factory.missingMap(missingMap);
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) ParsingException(org.opensearch.common.ParsingException) ParseField(org.opensearch.common.ParseField) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

ParseField (org.opensearch.common.ParseField)67 NamedObjectParser (org.opensearch.common.xcontent.ObjectParser.NamedObjectParser)26 List (java.util.List)15 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)9 Map (java.util.Map)8 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)7 Aggregation (org.opensearch.search.aggregations.Aggregation)7 HashMap (java.util.HashMap)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 UncheckedIOException (java.io.UncheckedIOException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3 OpenSearchException (org.opensearch.OpenSearchException)3 ExplainResponse (org.opensearch.action.explain.ExplainResponse)3 ContextParser (org.opensearch.common.xcontent.ContextParser)3 NamedObject (org.opensearch.common.xcontent.ObjectParserTests.NamedObject)3