Search in sources :

Example 96 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class DisMaxQueryBuilder method fromXContent.

public static DisMaxQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    float tieBreaker = DisMaxQueryBuilder.DEFAULT_TIE_BREAKER;
    final List<QueryBuilder> queries = new ArrayList<>();
    boolean queriesFound = false;
    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 (QUERIES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queriesFound = true;
                queries.add(parseInnerQueryBuilder(parser));
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (QUERIES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queriesFound = true;
                while (token != XContentParser.Token.END_ARRAY) {
                    queries.add(parseInnerQueryBuilder(parser));
                    token = parser.nextToken();
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]");
            }
        } else {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (TIE_BREAKER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tieBreaker = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[dis_max] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (!queriesFound) {
        throw new ParsingException(parser.getTokenLocation(), "[dis_max] requires 'queries' field with at least one clause");
    }
    DisMaxQueryBuilder disMaxQuery = new DisMaxQueryBuilder();
    disMaxQuery.tieBreaker(tieBreaker);
    disMaxQuery.queryName(queryName);
    disMaxQuery.boost(boost);
    for (QueryBuilder query : queries) {
        disMaxQuery.add(query);
    }
    return disMaxQuery;
}
Also used : ParsingException(org.opensearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 97 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class GeoDistanceQueryBuilder method fromXContent.

public static GeoDistanceQueryBuilder fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    String currentFieldName = null;
    GeoPoint point = new GeoPoint(Double.NaN, Double.NaN);
    String fieldName = null;
    Object vDistance = null;
    DistanceUnit unit = GeoDistanceQueryBuilder.DEFAULT_DISTANCE_UNIT;
    GeoDistance geoDistance = GeoDistanceQueryBuilder.DEFAULT_GEO_DISTANCE;
    GeoValidationMethod validationMethod = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_ARRAY) {
            fieldName = currentFieldName;
            GeoUtils.parseGeoPoint(parser, point);
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            // the json in the format of -> field : { lat : 30, lon : 12 }
            String currentName = parser.currentName();
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentName = parser.currentName();
                } else if (token.isValue()) {
                    if (currentName.equals("lat")) {
                        point.resetLat(parser.doubleValue());
                    } else if (currentName.equals("lon")) {
                        point.resetLon(parser.doubleValue());
                    } else if (currentName.equals("geohash")) {
                        point.resetFromGeoHash(parser.text());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[geo_distance] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else if (token.isValue()) {
            if (DISTANCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                if (token == XContentParser.Token.VALUE_STRING) {
                    // a String
                    vDistance = parser.text();
                } else {
                    // a Number
                    vDistance = parser.numberValue();
                }
            } else if (UNIT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                unit = DistanceUnit.fromString(parser.text());
            } else if (DISTANCE_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                geoDistance = GeoDistance.fromString(parser.text());
            } else if (currentFieldName.endsWith(".lat")) {
                point.resetLat(parser.doubleValue());
                fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lat".length());
            } else if (currentFieldName.endsWith(".lon")) {
                point.resetLon(parser.doubleValue());
                fieldName = currentFieldName.substring(0, currentFieldName.length() - ".lon".length());
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (VALIDATION_METHOD_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                validationMethod = GeoValidationMethod.fromString(parser.text());
            } else {
                if (fieldName == null) {
                    point.resetFromString(parser.text());
                    fieldName = currentFieldName;
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "failed to parse [{}] query. unexpected field [{}]", NAME, currentFieldName);
                }
            }
        }
    }
    if (vDistance == null) {
        throw new ParsingException(parser.getTokenLocation(), "geo_distance requires 'distance' to be specified");
    }
    GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(fieldName);
    if (vDistance instanceof Number) {
        qb.distance(((Number) vDistance).doubleValue(), unit);
    } else {
        qb.distance((String) vDistance, unit);
    }
    qb.point(point);
    if (validationMethod != null) {
        qb.setValidationMethod(validationMethod);
    }
    qb.geoDistance(geoDistance);
    qb.boost(boost);
    qb.queryName(queryName);
    qb.ignoreUnmapped(ignoreUnmapped);
    return qb;
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ParsingException(org.opensearch.common.ParsingException) GeoDistance(org.opensearch.common.geo.GeoDistance) DistanceUnit(org.opensearch.common.unit.DistanceUnit) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 98 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class MultiMatchQueryBuilder method fromXContent.

public static MultiMatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
    Object value = null;
    Map<String, Float> fieldsBoosts = new HashMap<>();
    MultiMatchQueryBuilder.Type type = DEFAULT_TYPE;
    String analyzer = null;
    int slop = DEFAULT_PHRASE_SLOP;
    Fuzziness fuzziness = null;
    int prefixLength = DEFAULT_PREFIX_LENGTH;
    int maxExpansions = DEFAULT_MAX_EXPANSIONS;
    Operator operator = DEFAULT_OPERATOR;
    String minimumShouldMatch = null;
    String fuzzyRewrite = null;
    Boolean useDisMax = null;
    Float tieBreaker = null;
    Float cutoffFrequency = null;
    Boolean lenient = null;
    MatchQuery.ZeroTermsQuery zeroTermsQuery = DEFAULT_ZERO_TERMS_QUERY;
    boolean autoGenerateSynonymsPhraseQuery = true;
    boolean fuzzyTranspositions = DEFAULT_FUZZY_TRANSPOSITIONS;
    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 (FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
            if (token == XContentParser.Token.START_ARRAY) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    parseFieldAndBoost(parser, fieldsBoosts);
                }
            } else if (token.isValue()) {
                parseFieldAndBoost(parser, fieldsBoosts);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                value = parser.objectText();
            } else if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                type = MultiMatchQueryBuilder.Type.parse(parser.text(), parser.getDeprecationHandler());
            } else if (ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                analyzer = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                slop = parser.intValue();
            } else if (Fuzziness.FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fuzziness = Fuzziness.parse(parser);
            } else if (PREFIX_LENGTH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                prefixLength = parser.intValue();
            } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                maxExpansions = parser.intValue();
            } else if (OPERATOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                operator = Operator.fromString(parser.text());
            } else if (MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                minimumShouldMatch = parser.textOrNull();
            } else if (FUZZY_REWRITE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fuzzyRewrite = parser.textOrNull();
            } else if (TIE_BREAKER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tieBreaker = parser.floatValue();
            } else if (CUTOFF_FREQUENCY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                cutoffFrequency = parser.floatValue();
            } else if (LENIENT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                lenient = parser.booleanValue();
            } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                String zeroTermsValue = parser.text();
                if ("none".equalsIgnoreCase(zeroTermsValue)) {
                    zeroTermsQuery = MatchQuery.ZeroTermsQuery.NONE;
                } else if ("all".equalsIgnoreCase(zeroTermsValue)) {
                    zeroTermsQuery = MatchQuery.ZeroTermsQuery.ALL;
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
                }
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else if (GENERATE_SYNONYMS_PHRASE_QUERY.match(currentFieldName, parser.getDeprecationHandler())) {
                autoGenerateSynonymsPhraseQuery = parser.booleanValue();
            } else if (FUZZY_TRANSPOSITIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fuzzyTranspositions = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    if (value == null) {
        throw new ParsingException(parser.getTokenLocation(), "No text specified for multi_match query");
    }
    if (fuzziness != null && (type == Type.CROSS_FIELDS || type == Type.PHRASE || type == Type.PHRASE_PREFIX)) {
        throw new ParsingException(parser.getTokenLocation(), "Fuzziness not allowed for type [" + type.parseField.getPreferredName() + "]");
    }
    if (slop != DEFAULT_PHRASE_SLOP && type == Type.BOOL_PREFIX) {
        throw new ParsingException(parser.getTokenLocation(), "[" + SLOP_FIELD.getPreferredName() + "] not allowed for type [" + type.parseField.getPreferredName() + "]");
    }
    if (cutoffFrequency != null && type == Type.BOOL_PREFIX) {
        throw new ParsingException(parser.getTokenLocation(), "[" + CUTOFF_FREQUENCY_FIELD.getPreferredName() + "] not allowed for type [" + type.parseField.getPreferredName() + "]");
    }
    MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder(value).fields(fieldsBoosts).type(type).analyzer(analyzer).cutoffFrequency(cutoffFrequency).fuzziness(fuzziness).fuzzyRewrite(fuzzyRewrite).maxExpansions(maxExpansions).minimumShouldMatch(minimumShouldMatch).operator(operator).prefixLength(prefixLength).slop(slop).tieBreaker(tieBreaker).zeroTermsQuery(zeroTermsQuery).autoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery).boost(boost).queryName(queryName).fuzzyTranspositions(fuzzyTranspositions);
    if (lenient != null) {
        builder.lenient(lenient);
    }
    return builder;
}
Also used : HashMap(java.util.HashMap) Fuzziness(org.opensearch.common.unit.Fuzziness) ParsingException(org.opensearch.common.ParsingException) MultiMatchQuery(org.opensearch.index.search.MultiMatchQuery) MatchQuery(org.opensearch.index.search.MatchQuery) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 99 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class SpanNotQueryBuilder method fromXContent.

public static SpanNotQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    SpanQueryBuilder include = null;
    SpanQueryBuilder exclude = null;
    Integer dist = null;
    Integer pre = null;
    Integer post = 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 (INCLUDE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                QueryBuilder query = parseInnerQueryBuilder(parser);
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_not [include] must be of type span query");
                }
                include = (SpanQueryBuilder) query;
                checkNoBoost(NAME, currentFieldName, parser, include);
            } else if (EXCLUDE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                QueryBuilder query = parseInnerQueryBuilder(parser);
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_not [exclude] must be of type span query");
                }
                exclude = (SpanQueryBuilder) query;
                checkNoBoost(NAME, currentFieldName, parser, exclude);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_not] query does not support [" + currentFieldName + "]");
            }
        } else {
            if (DIST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                dist = parser.intValue();
            } else if (PRE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                pre = parser.intValue();
            } else if (POST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                post = parser.intValue();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_not] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (include == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_not must have [include] span query clause");
    }
    if (exclude == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_not must have [exclude] span query clause");
    }
    if (dist != null && (pre != null || post != null)) {
        throw new ParsingException(parser.getTokenLocation(), "span_not can either use [dist] or [pre] & [post] (or none)");
    }
    SpanNotQueryBuilder spanNotQuery = new SpanNotQueryBuilder(include, exclude);
    if (dist != null) {
        spanNotQuery.dist(dist);
    }
    if (pre != null) {
        spanNotQuery.pre(pre);
    }
    if (post != null) {
        spanNotQuery.post(post);
    }
    spanNotQuery.boost(boost);
    spanNotQuery.queryName(queryName);
    return spanNotQuery;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 100 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class RandomScoreFunctionBuilder method fromXContent.

public static RandomScoreFunctionBuilder fromXContent(XContentParser parser) throws IOException, ParsingException {
    RandomScoreFunctionBuilder randomScoreFunctionBuilder = new RandomScoreFunctionBuilder();
    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.isValue()) {
            if ("seed".equals(currentFieldName)) {
                if (token == XContentParser.Token.VALUE_NUMBER) {
                    if (parser.numberType() == XContentParser.NumberType.INT) {
                        randomScoreFunctionBuilder.seed(parser.intValue());
                    } else if (parser.numberType() == XContentParser.NumberType.LONG) {
                        randomScoreFunctionBuilder.seed(parser.longValue());
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "random_score seed must be an int, long or string, not '" + token.toString() + "'");
                    }
                } else if (token == XContentParser.Token.VALUE_STRING) {
                    randomScoreFunctionBuilder.seed(parser.text());
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "random_score seed must be an int/long or string, not '" + token.toString() + "'");
                }
            } else if ("field".equals(currentFieldName)) {
                randomScoreFunctionBuilder.setField(parser.text());
            } else if (FunctionScoreQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                randomScoreFunctionBuilder.setFunctionName(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), NAME + " query does not support [" + currentFieldName + "]");
            }
        }
    }
    return randomScoreFunctionBuilder;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.opensearch.common.xcontent.XContentParser)602 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)222 ParsingException (org.opensearch.common.ParsingException)105 BytesReference (org.opensearch.common.bytes.BytesReference)104 IOException (java.io.IOException)97 XContentType (org.opensearch.common.xcontent.XContentType)88 ArrayList (java.util.ArrayList)73 OpenSearchParseException (org.opensearch.OpenSearchParseException)55 Matchers.containsString (org.hamcrest.Matchers.containsString)51 List (java.util.List)43 XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 HashMap (java.util.HashMap)38 ToXContent (org.opensearch.common.xcontent.ToXContent)38 Map (java.util.Map)32 BytesArray (org.opensearch.common.bytes.BytesArray)26 ShardId (org.opensearch.index.shard.ShardId)25 XContent (org.opensearch.common.xcontent.XContent)19 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)16 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)16 Collections (java.util.Collections)15