Search in sources :

Example 96 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class DerivativePipelineAggregationBuilder method parse.

public static DerivativePipelineAggregationBuilder parse(String pipelineAggregatorName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    String currentFieldName = null;
    String[] bucketsPaths = null;
    String format = null;
    String units = null;
    GapPolicy gapPolicy = 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 (FORMAT_FIELD.match(currentFieldName)) {
                format = parser.text();
            } else if (BUCKETS_PATH_FIELD.match(currentFieldName)) {
                bucketsPaths = new String[] { parser.text() };
            } else if (GAP_POLICY_FIELD.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (UNIT_FIELD.match(currentFieldName)) {
                units = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH_FIELD.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPaths = paths.toArray(new String[paths.size()]);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + pipelineAggregatorName + "].");
        }
    }
    if (bucketsPaths == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH_FIELD.getPreferredName() + "] for derivative aggregation [" + pipelineAggregatorName + "]");
    }
    DerivativePipelineAggregationBuilder factory = new DerivativePipelineAggregationBuilder(pipelineAggregatorName, bucketsPaths[0]);
    if (format != null) {
        factory.format(format);
    }
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    if (units != null) {
        factory.unit(units);
    }
    return factory;
}
Also used : GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 97 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class MovAvgPipelineAggregationBuilder method parse.

public static MovAvgPipelineAggregationBuilder parse(ParseFieldRegistry<MovAvgModel.AbstractModelParser> movingAverageMdelParserRegistry, String pipelineAggregatorName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    String currentFieldName = null;
    String[] bucketsPaths = null;
    String format = null;
    GapPolicy gapPolicy = null;
    Integer window = null;
    Map<String, Object> settings = null;
    String model = null;
    Integer predict = null;
    Boolean minimize = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (WINDOW.match(currentFieldName)) {
                window = parser.intValue();
                if (window <= 0) {
                    throw new ParsingException(parser.getTokenLocation(), "[" + currentFieldName + "] value must be a positive, " + "non-zero integer.  Value supplied was [" + predict + "] in [" + pipelineAggregatorName + "].");
                }
            } else if (PREDICT.match(currentFieldName)) {
                predict = parser.intValue();
                if (predict <= 0) {
                    throw new ParsingException(parser.getTokenLocation(), "[" + currentFieldName + "] value must be a positive integer." + "  Value supplied was [" + predict + "] in [" + pipelineAggregatorName + "].");
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (FORMAT.match(currentFieldName)) {
                format = parser.text();
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPaths = new String[] { parser.text() };
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (MODEL.match(currentFieldName)) {
                model = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPaths = paths.toArray(new String[paths.size()]);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (SETTINGS.match(currentFieldName)) {
                settings = parser.map();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if (MINIMIZE.match(currentFieldName)) {
                minimize = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + pipelineAggregatorName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + pipelineAggregatorName + "].");
        }
    }
    if (bucketsPaths == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for movingAvg aggregation [" + pipelineAggregatorName + "]");
    }
    MovAvgPipelineAggregationBuilder factory = new MovAvgPipelineAggregationBuilder(pipelineAggregatorName, bucketsPaths[0]);
    if (format != null) {
        factory.format(format);
    }
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    if (window != null) {
        factory.window(window);
    }
    if (predict != null) {
        factory.predict(predict);
    }
    if (model != null) {
        MovAvgModel.AbstractModelParser modelParser = movingAverageMdelParserRegistry.lookup(model, parser.getTokenLocation());
        MovAvgModel movAvgModel;
        try {
            movAvgModel = modelParser.parse(settings, pipelineAggregatorName, factory.window());
        } catch (ParseException exception) {
            throw new ParsingException(parser.getTokenLocation(), "Could not parse settings for model [" + model + "].", exception);
        }
        factory.model(movAvgModel);
    }
    if (minimize != null) {
        factory.minimize(minimize);
    }
    return factory;
}
Also used : MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 98 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SerialDiffPipelineAggregationBuilder method parse.

public static SerialDiffPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    String currentFieldName = null;
    String[] bucketsPaths = null;
    String format = null;
    GapPolicy gapPolicy = null;
    Integer lag = 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 (FORMAT.match(currentFieldName)) {
                format = parser.text();
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPaths = new String[] { parser.text() };
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (LAG.match(currentFieldName)) {
                lag = parser.intValue(true);
                if (lag <= 0) {
                    throw new ParsingException(parser.getTokenLocation(), "Lag must be a positive, non-zero integer.  Value supplied was" + lag + " in [" + reducerName + "]: [" + currentFieldName + "].");
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPaths = paths.toArray(new String[paths.size()]);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].", parser.getTokenLocation());
        }
    }
    if (bucketsPaths == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for derivative aggregation [" + reducerName + "]");
    }
    SerialDiffPipelineAggregationBuilder factory = new SerialDiffPipelineAggregationBuilder(reducerName, bucketsPaths[0]);
    if (lag != null) {
        factory.lag(lag);
    }
    if (format != null) {
        factory.format(format);
    }
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    return factory;
}
Also used : GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 99 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class FetchSourceContext method fromXContent.

public static FetchSourceContext fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    boolean fetchSource = true;
    String[] includes = Strings.EMPTY_ARRAY;
    String[] excludes = Strings.EMPTY_ARRAY;
    if (token == XContentParser.Token.VALUE_BOOLEAN) {
        fetchSource = parser.booleanValue();
    } else if (token == XContentParser.Token.VALUE_STRING) {
        includes = new String[] { parser.text() };
    } else if (token == XContentParser.Token.START_ARRAY) {
        ArrayList<String> list = new ArrayList<>();
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            list.add(parser.text());
        }
        includes = list.toArray(new String[list.size()]);
    } else if (token == XContentParser.Token.START_OBJECT) {
        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.START_ARRAY) {
                if (INCLUDES_FIELD.match(currentFieldName)) {
                    List<String> includesList = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        if (token == XContentParser.Token.VALUE_STRING) {
                            includesList.add(parser.text());
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                        }
                    }
                    includes = includesList.toArray(new String[includesList.size()]);
                } else if (EXCLUDES_FIELD.match(currentFieldName)) {
                    List<String> excludesList = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        if (token == XContentParser.Token.VALUE_STRING) {
                            excludesList.add(parser.text());
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                        }
                    }
                    excludes = excludesList.toArray(new String[excludesList.size()]);
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                }
            } else if (token == XContentParser.Token.VALUE_STRING) {
                if (INCLUDES_FIELD.match(currentFieldName)) {
                    includes = new String[] { parser.text() };
                } else if (EXCLUDES_FIELD.match(currentFieldName)) {
                    excludes = new String[] { parser.text() };
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + currentFieldName + "].", parser.getTokenLocation());
            }
        }
    } else {
        throw new ParsingException(parser.getTokenLocation(), "Expected one of [" + XContentParser.Token.VALUE_BOOLEAN + ", " + XContentParser.Token.START_OBJECT + "] but found [" + token + "]", parser.getTokenLocation());
    }
    return new FetchSourceContext(fetchSource, includes, excludes);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 100 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ScriptedMetricAggregationBuilder method parse.

public static ScriptedMetricAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    Script initScript = null;
    Script mapScript = null;
    Script combineScript = null;
    Script reduceScript = null;
    Map<String, Object> params = null;
    XContentParser.Token token;
    String currentFieldName = null;
    Set<String> scriptParameters = new HashSet<>();
    scriptParameters.add(INIT_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(MAP_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(COMBINE_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(REDUCE_SCRIPT_FIELD.getPreferredName());
    XContentParser parser = context.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.VALUE_STRING) {
            if (INIT_SCRIPT_FIELD.match(currentFieldName)) {
                initScript = Script.parse(parser);
            } else if (MAP_SCRIPT_FIELD.match(currentFieldName)) {
                mapScript = Script.parse(parser);
            } else if (COMBINE_SCRIPT_FIELD.match(currentFieldName)) {
                combineScript = Script.parse(parser);
            } else if (REDUCE_SCRIPT_FIELD.match(currentFieldName)) {
                reduceScript = Script.parse(parser);
            } else if (token == XContentParser.Token.START_OBJECT && PARAMS_FIELD.match(currentFieldName)) {
                params = parser.map();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
        }
    }
    if (mapScript == null) {
        throw new ParsingException(parser.getTokenLocation(), "map_script field is required in [" + aggregationName + "].");
    }
    ScriptedMetricAggregationBuilder factory = new ScriptedMetricAggregationBuilder(aggregationName);
    if (initScript != null) {
        factory.initScript(initScript);
    }
    if (mapScript != null) {
        factory.mapScript(mapScript);
    }
    if (combineScript != null) {
        factory.combineScript(combineScript);
    }
    if (reduceScript != null) {
        factory.reduceScript(reduceScript);
    }
    if (params != null) {
        factory.params(params);
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchScript(org.elasticsearch.script.SearchScript) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) HashSet(java.util.HashSet)

Aggregations

ParsingException (org.elasticsearch.common.ParsingException)165 XContentParser (org.elasticsearch.common.xcontent.XContentParser)96 ArrayList (java.util.ArrayList)26 Matchers.containsString (org.hamcrest.Matchers.containsString)22 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Token (org.elasticsearch.common.xcontent.XContentParser.Token)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 Index (org.elasticsearch.index.Index)7 Script (org.elasticsearch.script.Script)7 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)7 List (java.util.List)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)6 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)6 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 ToXContent (org.elasticsearch.common.xcontent.ToXContent)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4