Search in sources :

Example 81 with ParsingException

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

the class StoredScriptSource method parse.

/**
     * This will parse XContent into a {@link StoredScriptSource}.  The following formats can be parsed:
     *
     * The simple script format with no compiler options or user-defined params:
     *
     * Example:
     * {@code
     * {"script": "return Math.log(doc.popularity) * 100;"}
     * }
     *
     * The above format requires the lang to be specified using the deprecated stored script namespace
     * (as a url parameter during a put request).  See {@link ScriptMetaData} for more information about
     * the stored script namespaces.
     *
     * The complex script format using the new stored script namespace
     * where lang and code are required but options is optional:
     *
     * {@code
     * {
     *     "script" : {
     *         "lang" : "<lang>",
     *         "code" : "<code>",
     *         "options" : {
     *             "option0" : "<option0>",
     *             "option1" : "<option1>",
     *             ...
     *         }
     *     }
     * }
     * }
     *
     * Example:
     * {@code
     * {
     *     "script": {
     *         "lang" : "painless",
     *         "code" : "return Math.log(doc.popularity) * params.multiplier"
     *     }
     * }
     * }
     *
     * The simple template format:
     *
     * {@code
     * {
     *     "query" : ...
     * }
     * }
     *
     * The complex template format:
     *
     * {@code
     * {
     *     "template": {
     *         "query" : ...
     *     }
     * }
     * }
     *
     * Note that templates can be handled as both strings and complex JSON objects.
     * Also templates may be part of the 'code' parameter in a script.  The Parser
     * can handle this case as well.
     *
     * @param lang    An optional parameter to allow for use of the deprecated stored
     *                script namespace.  This will be used to specify the language
     *                coming in as a url parameter from a request or for stored templates.
     * @param content The content from the request to be parsed as described above.
     * @return        The parsed {@link StoredScriptSource}.
     */
public static StoredScriptSource parse(String lang, BytesReference content, XContentType xContentType) {
    try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
        Token token = parser.nextToken();
        if (token != Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{]");
        }
        token = parser.nextToken();
        if (token != Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" + SCRIPT_PARSE_FIELD.getPreferredName() + ", " + TEMPLATE_PARSE_FIELD.getPreferredName());
        }
        String name = parser.currentName();
        if (SCRIPT_PARSE_FIELD.getPreferredName().equals(name)) {
            token = parser.nextToken();
            if (token == Token.VALUE_STRING) {
                if (lang == null) {
                    throw new IllegalArgumentException("must specify lang as a url parameter when using the deprecated stored script namespace");
                }
                return new StoredScriptSource(lang, parser.text(), Collections.emptyMap());
            } else if (token == Token.START_OBJECT) {
                if (lang == null) {
                    return PARSER.apply(parser, null).build();
                } else {
                    //this is really for search templates, that need to be converted to json format
                    try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
                        builder.copyCurrentStructure(parser);
                        return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
                    }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, <code>]");
            }
        } else {
            if (lang == null) {
                throw new IllegalArgumentException("unexpected stored script format");
            }
            if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) {
                token = parser.nextToken();
                if (token == Token.VALUE_STRING) {
                    return new StoredScriptSource(lang, parser.text(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
                }
            }
            try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
                if (token != Token.START_OBJECT) {
                    builder.startObject();
                    builder.copyCurrentStructure(parser);
                    builder.endObject();
                } else {
                    builder.copyCurrentStructure(parser);
                }
                return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
            }
        }
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 82 with ParsingException

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

the class TermsAggregationBuilder method parseOrderParam.

private static Terms.Order parseOrderParam(XContentParser parser, QueryParseContext context) throws IOException {
    XContentParser.Token token;
    Terms.Order orderParam = null;
    String orderKey = null;
    boolean orderAsc = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            orderKey = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String dir = parser.text();
            if ("asc".equalsIgnoreCase(dir)) {
                orderAsc = true;
            } else if ("desc".equalsIgnoreCase(dir)) {
                orderAsc = false;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown terms order direction [" + dir + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " for [order]");
        }
    }
    if (orderKey == null) {
        throw new ParsingException(parser.getTokenLocation(), "Must specify at least one field for [order]");
    } else {
        orderParam = resolveOrder(orderKey, orderAsc);
    }
    return orderParam;
}
Also used : Order(org.elasticsearch.search.aggregations.bucket.terms.Terms.Order) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 83 with ParsingException

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

the class SamplerAggregationBuilder method parse.

public static SamplerAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    Integer shardSize = null;
    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.VALUE_NUMBER) {
            if (SamplerAggregator.SHARD_SIZE_FIELD.match(currentFieldName)) {
                shardSize = parser.intValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unsupported property \"" + currentFieldName + "\" for aggregation \"" + aggregationName);
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unsupported property \"" + currentFieldName + "\" for aggregation \"" + aggregationName);
        }
    }
    SamplerAggregationBuilder factory = new SamplerAggregationBuilder(aggregationName);
    if (shardSize != null) {
        factory.shardSize(shardSize);
    }
    return factory;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 84 with ParsingException

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

the class HistogramAggregationBuilder method parseOrder.

private static InternalOrder parseOrder(XContentParser parser, QueryParseContext context) throws IOException {
    InternalOrder order = null;
    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) {
            String dir = parser.text();
            boolean asc = "asc".equals(dir);
            if (!asc && !"desc".equals(dir)) {
                throw new ParsingException(parser.getTokenLocation(), "Unknown order direction: [" + dir + "]. Should be either [asc] or [desc]");
            }
            order = resolveOrder(currentFieldName, asc);
        }
    }
    return order;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token)

Example 85 with ParsingException

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

the class NestedAggregationBuilder method parse.

public static NestedAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    String path = null;
    XContentParser.Token token;
    String currentFieldName = null;
    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.VALUE_STRING) {
            if (NestedAggregator.PATH_FIELD.match(currentFieldName)) {
                path = parser.text();
            } 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 (path == null) {
        // "field" doesn't exist, so we fall back to the context of the ancestors
        throw new ParsingException(parser.getTokenLocation(), "Missing [path] field for nested aggregation [" + aggregationName + "]");
    }
    return new NestedAggregationBuilder(aggregationName, path);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

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