Search in sources :

Example 41 with ParsingException

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

the class WildcardQueryBuilder method fromXContent.

public static WildcardQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    String rewrite = null;
    String value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else {
                    if (WILDCARD_FIELD.match(currentFieldName)) {
                        value = parser.text();
                    } else if (VALUE_FIELD.match(currentFieldName)) {
                        value = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (REWRITE_FIELD.match(currentFieldName)) {
                        rewrite = parser.textOrNull();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[wildcard] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.text();
        }
    }
    return new WildcardQueryBuilder(fieldName, value).rewrite(rewrite).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 42 with ParsingException

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

the class WrapperQueryBuilder method fromXContent.

public static WrapperQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    XContentParser.Token token = parser.nextToken();
    if (token != XContentParser.Token.FIELD_NAME) {
        throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed");
    }
    String fieldName = parser.currentName();
    if (!QUERY_FIELD.match(fieldName)) {
        throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed, expected `query` but was " + fieldName);
    }
    parser.nextToken();
    byte[] source = parser.binaryValue();
    parser.nextToken();
    if (source == null) {
        throw new ParsingException(parser.getTokenLocation(), "wrapper query has no [query] specified");
    }
    return new WrapperQueryBuilder(source);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 43 with ParsingException

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

the class RestValidateQueryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
    validateQueryRequest.explain(request.paramAsBoolean("explain", false));
    validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    validateQueryRequest.rewrite(request.paramAsBoolean("rewrite", false));
    Exception bodyParsingException = null;
    try {
        request.withContentOrSourceParamParserOrNull(parser -> {
            if (parser != null) {
                validateQueryRequest.query(RestActions.getQueryContent(parser));
            } else if (request.hasParam("q")) {
                validateQueryRequest.query(RestActions.urlParamsToQueryBuilder(request));
            }
        });
    } catch (Exception e) {
        bodyParsingException = e;
    }
    final Exception finalBodyParsingException = bodyParsingException;
    return channel -> {
        if (finalBodyParsingException != null) {
            if (finalBodyParsingException instanceof ParsingException) {
                handleException(validateQueryRequest, ((ParsingException) finalBodyParsingException).getDetailedMessage(), channel);
            } else {
                handleException(validateQueryRequest, finalBodyParsingException.getMessage(), channel);
            }
        } else {
            client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {

                @Override
                public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
                    builder.startObject();
                    builder.field(VALID_FIELD, response.isValid());
                    buildBroadcastShardsHeader(builder, request, response);
                    if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
                        builder.startArray(EXPLANATIONS_FIELD);
                        for (QueryExplanation explanation : response.getQueryExplanation()) {
                            builder.startObject();
                            if (explanation.getIndex() != null) {
                                builder.field(INDEX_FIELD, explanation.getIndex());
                            }
                            builder.field(VALID_FIELD, explanation.isValid());
                            if (explanation.getError() != null) {
                                builder.field(ERROR_FIELD, explanation.getError());
                            }
                            if (explanation.getExplanation() != null) {
                                builder.field(EXPLANATION_FIELD, explanation.getExplanation());
                            }
                            builder.endObject();
                        }
                        builder.endArray();
                    }
                    builder.endObject();
                    return new BytesRestResponse(OK, builder);
                }
            });
        }
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) ParsingException(org.elasticsearch.common.ParsingException) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) RestChannel(org.elasticsearch.rest.RestChannel) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) QueryExplanation(org.elasticsearch.action.admin.indices.validate.query.QueryExplanation) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ParsingException(org.elasticsearch.common.ParsingException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryExplanation(org.elasticsearch.action.admin.indices.validate.query.QueryExplanation)

Example 44 with ParsingException

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

the class BucketSelectorPipelineAggregationBuilder method parse.

public static BucketSelectorPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = 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 (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } 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);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } 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 + "].");
        }
    }
    if (bucketsPathsMap == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
    }
    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + Script.SCRIPT_PARSE_FIELD.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
    }
    BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) ParsingException(org.elasticsearch.common.ParsingException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 45 with ParsingException

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

the class IpRangeAggregationBuilder method parseRange.

private static Range parseRange(XContentParser parser, QueryParseContext context) throws IOException {
    String key = null;
    String from = null;
    String to = null;
    String mask = null;
    if (parser.currentToken() != Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[ranges] must contain objects, but hit a " + parser.currentToken());
    }
    while (parser.nextToken() != Token.END_OBJECT) {
        if (parser.currentToken() == Token.FIELD_NAME) {
            continue;
        }
        if (RangeAggregator.Range.KEY_FIELD.match(parser.currentName())) {
            key = parser.text();
        } else if (RangeAggregator.Range.FROM_FIELD.match(parser.currentName())) {
            from = parser.textOrNull();
        } else if (RangeAggregator.Range.TO_FIELD.match(parser.currentName())) {
            to = parser.textOrNull();
        } else if (MASK_FIELD.match(parser.currentName())) {
            mask = parser.text();
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected ip range parameter: [" + parser.currentName() + "]");
        }
    }
    if (mask != null) {
        if (key == null) {
            key = mask;
        }
        return new Range(key, mask);
    } else {
        return new Range(key, from, to);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException)

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