Search in sources :

Example 31 with ParsingException

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

the class SimpleQueryStringBuilder method fromXContent.

public static SimpleQueryStringBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String currentFieldName = null;
    String queryBody = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String minimumShouldMatch = null;
    Map<String, Float> fieldsAndWeights = new HashMap<>();
    Operator defaultOperator = null;
    String analyzerName = null;
    int flags = SimpleQueryStringFlag.ALL.value();
    Boolean lenient = null;
    boolean analyzeWildcard = SimpleQueryStringBuilder.DEFAULT_ANALYZE_WILDCARD;
    String quoteFieldSuffix = null;
    Boolean useAllFields = null;
    XContentParser.Token token;
    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 (FIELDS_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String fField = null;
                    float fBoost = 1;
                    char[] text = parser.textCharacters();
                    int end = parser.textOffset() + parser.textLength();
                    for (int i = parser.textOffset(); i < end; i++) {
                        if (text[i] == '^') {
                            int relativeLocation = i - parser.textOffset();
                            fField = new String(text, parser.textOffset(), relativeLocation);
                            fBoost = Float.parseFloat(new String(text, i + 1, parser.textLength() - relativeLocation - 1));
                            break;
                        }
                    }
                    if (fField == null) {
                        fField = parser.text();
                    }
                    fieldsAndWeights.put(fField, fBoost);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + SimpleQueryStringBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (QUERY_FIELD.match(currentFieldName)) {
                queryBody = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (ANALYZER_FIELD.match(currentFieldName)) {
                analyzerName = parser.text();
            } else if (DEFAULT_OPERATOR_FIELD.match(currentFieldName)) {
                defaultOperator = Operator.fromString(parser.text());
            } else if (FLAGS_FIELD.match(currentFieldName)) {
                if (parser.currentToken() != XContentParser.Token.VALUE_NUMBER) {
                    // Possible options are:
                    // ALL, NONE, AND, OR, PREFIX, PHRASE, PRECEDENCE, ESCAPE, WHITESPACE, FUZZY, NEAR, SLOP
                    flags = SimpleQueryStringFlag.resolveFlags(parser.text());
                } else {
                    flags = parser.intValue();
                    if (flags < 0) {
                        flags = SimpleQueryStringFlag.ALL.value();
                    }
                }
            } else if (LOCALE_FIELD.match(currentFieldName)) {
            // ignore, deprecated setting
            } else if (LOWERCASE_EXPANDED_TERMS_FIELD.match(currentFieldName)) {
            // ignore, deprecated setting
            } else if (LENIENT_FIELD.match(currentFieldName)) {
                lenient = parser.booleanValue();
            } else if (ANALYZE_WILDCARD_FIELD.match(currentFieldName)) {
                analyzeWildcard = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName)) {
                minimumShouldMatch = parser.textOrNull();
            } else if (QUOTE_FIELD_SUFFIX_FIELD.match(currentFieldName)) {
                quoteFieldSuffix = parser.textOrNull();
            } else if (ALL_FIELDS_FIELD.match(currentFieldName)) {
                useAllFields = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + SimpleQueryStringBuilder.NAME + "] unsupported field [" + parser.currentName() + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + SimpleQueryStringBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    // Query text is required
    if (queryBody == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + SimpleQueryStringBuilder.NAME + "] query text missing");
    }
    if ((useAllFields != null && useAllFields) && (fieldsAndWeights.size() != 0)) {
        throw new ParsingException(parser.getTokenLocation(), "cannot use [all_fields] parameter in conjunction with [fields]");
    }
    SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder(queryBody);
    qb.boost(boost).fields(fieldsAndWeights).analyzer(analyzerName).queryName(queryName).minimumShouldMatch(minimumShouldMatch);
    qb.flags(flags).defaultOperator(defaultOperator);
    if (lenient != null) {
        qb.lenient(lenient);
    }
    qb.analyzeWildcard(analyzeWildcard).boost(boost).quoteFieldSuffix(quoteFieldSuffix);
    qb.useAllFields(useAllFields);
    return qb;
}
Also used : HashMap(java.util.HashMap) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 32 with ParsingException

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

the class GeoBoundingBoxQueryBuilder method fromXContent.

public static GeoBoundingBoxQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    double top = Double.NaN;
    double bottom = Double.NaN;
    double left = Double.NaN;
    double right = Double.NaN;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    GeoValidationMethod validationMethod = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    GeoPoint sparse = new GeoPoint();
    String type = "memory";
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (parseContext.isDeprecatedSetting(currentFieldName)) {
                    // skip
                    } else if (FIELD_FIELD.match(currentFieldName)) {
                        fieldName = parser.text();
                    } else if (TOP_FIELD.match(currentFieldName)) {
                        top = parser.doubleValue();
                    } else if (BOTTOM_FIELD.match(currentFieldName)) {
                        bottom = parser.doubleValue();
                    } else if (LEFT_FIELD.match(currentFieldName)) {
                        left = parser.doubleValue();
                    } else if (RIGHT_FIELD.match(currentFieldName)) {
                        right = parser.doubleValue();
                    } else {
                        if (TOP_LEFT_FIELD.match(currentFieldName)) {
                            GeoUtils.parseGeoPoint(parser, sparse);
                            top = sparse.getLat();
                            left = sparse.getLon();
                        } else if (BOTTOM_RIGHT_FIELD.match(currentFieldName)) {
                            GeoUtils.parseGeoPoint(parser, sparse);
                            bottom = sparse.getLat();
                            right = sparse.getLon();
                        } else if (TOP_RIGHT_FIELD.match(currentFieldName)) {
                            GeoUtils.parseGeoPoint(parser, sparse);
                            top = sparse.getLat();
                            right = sparse.getLon();
                        } else if (BOTTOM_LEFT_FIELD.match(currentFieldName)) {
                            GeoUtils.parseGeoPoint(parser, sparse);
                            bottom = sparse.getLat();
                            left = sparse.getLon();
                        } else {
                            throw new ElasticsearchParseException("failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
                        }
                    }
                } else {
                    throw new ElasticsearchParseException("failed to parse [{}] query. field name expected but [{}] found", NAME, token);
                }
            }
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (VALIDATION_METHOD_FIELD.match(currentFieldName)) {
                validationMethod = GeoValidationMethod.fromString(parser.text());
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (TYPE_FIELD.match(currentFieldName)) {
                type = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
            }
        }
    }
    //just keep the object
    final GeoPoint topLeft = sparse.reset(top, left);
    final GeoPoint bottomRight = new GeoPoint(bottom, right);
    GeoBoundingBoxQueryBuilder builder = new GeoBoundingBoxQueryBuilder(fieldName);
    builder.setCorners(topLeft, bottomRight);
    builder.queryName(queryName);
    builder.boost(boost);
    builder.type(GeoExecType.fromString(type));
    builder.ignoreUnmapped(ignoreUnmapped);
    if (validationMethod != null) {
        // ignore deprecated coerce/ignoreMalformed settings if validationMethod is set
        builder.setValidationMethod(validationMethod);
    }
    return builder;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 33 with ParsingException

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

the class GeoPolygonQueryBuilder method fromXContent.

public static GeoPolygonQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    List<GeoPoint> shell = null;
    Float boost = null;
    GeoValidationMethod validationMethod = null;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    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) {
            fieldName = currentFieldName;
            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 (POINTS_FIELD.match(currentFieldName)) {
                        shell = new ArrayList<GeoPoint>();
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            shell.add(GeoUtils.parseGeoPoint(parser));
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support token type [" + token.name() + "] under [" + currentFieldName + "]");
                }
            }
        } else if (token.isValue()) {
            if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (VALIDATION_METHOD.match(currentFieldName)) {
                validationMethod = GeoValidationMethod.fromString(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] unexpected token type [" + token.name() + "]");
        }
    }
    GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell);
    if (validationMethod != null) {
        // if GeoValidationMethod was explicitly set ignore deprecated coerce and ignoreMalformed settings
        builder.setValidationMethod(validationMethod);
    }
    if (queryName != null) {
        builder.queryName(queryName);
    }
    if (boost != null) {
        builder.boost(boost);
    }
    builder.ignoreUnmapped(ignoreUnmapped);
    return builder;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Token(org.elasticsearch.common.xcontent.XContentParser.Token) ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 34 with ParsingException

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

the class SpanFirstQueryBuilder method fromXContent.

public static SpanFirstQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    SpanQueryBuilder match = null;
    Integer end = null;
    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 (token == XContentParser.Token.START_OBJECT) {
            if (MATCH_FIELD.match(currentFieldName)) {
                QueryBuilder query = parseContext.parseInnerQueryBuilder();
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "spanFirst [match] must be of type span query");
                }
                match = (SpanQueryBuilder) query;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]");
            }
        } else {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (END_FIELD.match(currentFieldName)) {
                end = parser.intValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_first] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (match == null) {
        throw new ParsingException(parser.getTokenLocation(), "spanFirst must have [match] span query clause");
    }
    if (end == null) {
        throw new ParsingException(parser.getTokenLocation(), "spanFirst must have [end] set for it");
    }
    SpanFirstQueryBuilder queryBuilder = new SpanFirstQueryBuilder(match, end);
    queryBuilder.boost(boost).queryName(queryName);
    return queryBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 35 with ParsingException

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

the class SpanNearQueryBuilder method fromXContent.

public static SpanNearQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    Integer slop = null;
    boolean inOrder = SpanNearQueryBuilder.DEFAULT_IN_ORDER;
    String queryName = null;
    List<SpanQueryBuilder> clauses = new ArrayList<>();
    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 (token == XContentParser.Token.START_ARRAY) {
            if (CLAUSES_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    QueryBuilder query = parseContext.parseInnerQueryBuilder();
                    if (query instanceof SpanQueryBuilder == false) {
                        throw new ParsingException(parser.getTokenLocation(), "spanNear [clauses] must be of type span query");
                    }
                    clauses.add((SpanQueryBuilder) query);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (IN_ORDER_FIELD.match(currentFieldName)) {
                inOrder = parser.booleanValue();
            } else if (SLOP_FIELD.match(currentFieldName)) {
                slop = parser.intValue();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[span_near] query does not support [" + currentFieldName + "]");
        }
    }
    if (clauses.isEmpty()) {
        throw new ParsingException(parser.getTokenLocation(), "span_near must include [clauses]");
    }
    if (slop == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_near must include [slop]");
    }
    SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(clauses.get(0), slop);
    for (int i = 1; i < clauses.size(); i++) {
        queryBuilder.addClause(clauses.get(i));
    }
    queryBuilder.inOrder(inOrder);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
}
Also used : ArrayList(java.util.ArrayList) 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