Search in sources :

Example 61 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class RangeQueryBuilder method fromXContent.

public static RangeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object from = null;
    Object to = null;
    boolean includeLower = RangeQueryBuilder.DEFAULT_INCLUDE_LOWER;
    boolean includeUpper = RangeQueryBuilder.DEFAULT_INCLUDE_UPPER;
    String timeZone = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String format = null;
    String relation = 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 (FROM_FIELD.match(currentFieldName)) {
                        from = parser.objectBytes();
                    } else if (TO_FIELD.match(currentFieldName)) {
                        to = parser.objectBytes();
                    } else if (INCLUDE_LOWER_FIELD.match(currentFieldName)) {
                        includeLower = parser.booleanValue();
                    } else if (INCLUDE_UPPER_FIELD.match(currentFieldName)) {
                        includeUpper = parser.booleanValue();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (GT_FIELD.match(currentFieldName)) {
                        from = parser.objectBytes();
                        includeLower = false;
                    } else if (GTE_FIELD.match(currentFieldName)) {
                        from = parser.objectBytes();
                        includeLower = true;
                    } else if (LT_FIELD.match(currentFieldName)) {
                        to = parser.objectBytes();
                        includeUpper = false;
                    } else if (LTE_FIELD.match(currentFieldName)) {
                        to = parser.objectBytes();
                        includeUpper = true;
                    } else if (TIME_ZONE_FIELD.match(currentFieldName)) {
                        timeZone = parser.text();
                    } else if (FORMAT_FIELD.match(currentFieldName)) {
                        format = parser.text();
                    } else if (RELATION_FIELD.match(currentFieldName)) {
                        relation = parser.text();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[range] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else if (token.isValue()) {
            if (NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (FIELDDATA_FIELD.match(currentFieldName)) {
            // ignore
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[range] query does not support [" + currentFieldName + "]");
            }
        }
    }
    RangeQueryBuilder rangeQuery = new RangeQueryBuilder(fieldName);
    rangeQuery.from(from);
    rangeQuery.to(to);
    rangeQuery.includeLower(includeLower);
    rangeQuery.includeUpper(includeUpper);
    if (timeZone != null) {
        rangeQuery.timeZone(timeZone);
    }
    rangeQuery.boost(boost);
    rangeQuery.queryName(queryName);
    if (format != null) {
        rangeQuery.format(format);
    }
    if (relation != null) {
        rangeQuery.relation(relation);
    }
    return rangeQuery;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 62 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class ScriptQueryBuilder method fromXContent.

public static ScriptQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    // also, when caching, since its isCacheable is false, will result in loading all bit set...
    Script script = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    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 (parseContext.isDeprecatedSetting(currentFieldName)) {
        // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]");
            }
        } 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 (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), "script must be provided with a [script] filter");
    }
    return new ScriptQueryBuilder(script).boost(boost).queryName(queryName);
}
Also used : Script(org.elasticsearch.script.Script) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 63 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser 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 64 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser 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 65 with XContentParser

use of org.elasticsearch.common.xcontent.XContentParser 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)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)411 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)125 ParsingException (org.elasticsearch.common.ParsingException)109 IOException (java.io.IOException)69 BytesReference (org.elasticsearch.common.bytes.BytesReference)57 ArrayList (java.util.ArrayList)50 XContentType (org.elasticsearch.common.xcontent.XContentType)43 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)43 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)42 HashMap (java.util.HashMap)28 Matchers.containsString (org.hamcrest.Matchers.containsString)23 Map (java.util.Map)22 List (java.util.List)17 Settings (org.elasticsearch.common.settings.Settings)17 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 ToXContent (org.elasticsearch.common.xcontent.ToXContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)14