Search in sources :

Example 56 with XContentParser

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

the class RandomScoreFunctionBuilder method fromXContent.

public static RandomScoreFunctionBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
    XContentParser parser = parseContext.parser();
    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 {
                throw new ParsingException(parser.getTokenLocation(), NAME + " query does not support [" + currentFieldName + "]");
            }
        }
    }
    return randomScoreFunctionBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 57 with XContentParser

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

the class ScriptScoreFunctionBuilder method fromXContent.

public static ScriptScoreFunctionBuilder fromXContent(QueryParseContext parseContext) throws IOException, ParsingException {
    XContentParser parser = parseContext.parser();
    Script script = 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 (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), NAME + " query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), NAME + " requires 'script' field");
    }
    return new ScriptScoreFunctionBuilder(script);
}
Also used : Script(org.elasticsearch.script.Script) SearchScript(org.elasticsearch.script.SearchScript) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 58 with XContentParser

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

the class GeoShapeQueryBuilder method fromXContent.

public static GeoShapeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    ShapeRelation shapeRelation = null;
    SpatialStrategy strategy = null;
    ShapeBuilder shape = null;
    String id = null;
    String type = null;
    String index = null;
    String shapePath = null;
    XContentParser.Token token;
    String currentFieldName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = 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_OBJECT) {
            if (fieldName != null) {
                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] point specified twice. [" + currentFieldName + "]");
            }
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                    token = parser.nextToken();
                    if (SHAPE_FIELD.match(currentFieldName)) {
                        shape = ShapeBuilder.parse(parser);
                    } else if (STRATEGY_FIELD.match(currentFieldName)) {
                        String strategyName = parser.text();
                        strategy = SpatialStrategy.fromString(strategyName);
                        if (strategy == null) {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown strategy [" + strategyName + " ]");
                        }
                    } else if (RELATION_FIELD.match(currentFieldName)) {
                        shapeRelation = ShapeRelation.getRelationByName(parser.text());
                        if (shapeRelation == null) {
                            throw new ParsingException(parser.getTokenLocation(), "Unknown shape operation [" + parser.text() + " ]");
                        }
                    } else if (INDEXED_SHAPE_FIELD.match(currentFieldName)) {
                        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                            if (token == XContentParser.Token.FIELD_NAME) {
                                currentFieldName = parser.currentName();
                            } else if (token.isValue()) {
                                if (SHAPE_ID_FIELD.match(currentFieldName)) {
                                    id = parser.text();
                                } else if (SHAPE_TYPE_FIELD.match(currentFieldName)) {
                                    type = parser.text();
                                } else if (SHAPE_INDEX_FIELD.match(currentFieldName)) {
                                    index = parser.text();
                                } else if (SHAPE_PATH_FIELD.match(currentFieldName)) {
                                    shapePath = parser.text();
                                }
                            } else {
                                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                            }
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
                    }
                }
            }
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + GeoShapeQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        }
    }
    GeoShapeQueryBuilder builder;
    if (shape != null) {
        builder = new GeoShapeQueryBuilder(fieldName, shape);
    } else {
        builder = new GeoShapeQueryBuilder(fieldName, id, type);
    }
    if (index != null) {
        builder.indexedShapeIndex(index);
    }
    if (shapePath != null) {
        builder.indexedShapePath(shapePath);
    }
    if (shapeRelation != null) {
        builder.relation(shapeRelation);
    }
    if (strategy != null) {
        builder.strategy(strategy);
    }
    if (queryName != null) {
        builder.queryName(queryName);
    }
    builder.boost(boost);
    builder.ignoreUnmapped(ignoreUnmapped);
    return builder;
}
Also used : ShapeRelation(org.elasticsearch.common.geo.ShapeRelation) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ParsingException(org.elasticsearch.common.ParsingException) SpatialStrategy(org.elasticsearch.common.geo.SpatialStrategy) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 59 with XContentParser

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

the class HasChildQueryBuilder method fromXContent.

public static HasChildQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String childType = null;
    ScoreMode scoreMode = ScoreMode.None;
    int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
    int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String queryName = null;
    InnerHitBuilder innerHitBuilder = null;
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = 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 (QUERY_FIELD.match(currentFieldName)) {
                iqb = parseContext.parseInnerQueryBuilder();
            } else if (INNER_HITS_FIELD.match(currentFieldName)) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName)) {
                childType = parser.text();
            } else if (SCORE_MODE_FIELD.match(currentFieldName)) {
                scoreMode = parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (MIN_CHILDREN_FIELD.match(currentFieldName)) {
                minChildren = parser.intValue(true);
            } else if (MAX_CHILDREN_FIELD.match(currentFieldName)) {
                maxChildren = parser.intValue(true);
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, scoreMode);
    hasChildQueryBuilder.minMaxChildren(minChildren, maxChildren);
    hasChildQueryBuilder.queryName(queryName);
    hasChildQueryBuilder.boost(boost);
    hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
    if (innerHitBuilder != null) {
        hasChildQueryBuilder.innerHit(innerHitBuilder, ignoreUnmapped);
    }
    return hasChildQueryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 60 with XContentParser

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

the class HasParentQueryBuilder method fromXContent.

public static HasParentQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String parentType = null;
    boolean score = false;
    String queryName = null;
    InnerHitBuilder innerHits = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = null;
    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 (QUERY_FIELD.match(currentFieldName)) {
                iqb = parseContext.parseInnerQueryBuilder();
            } else if (INNER_HITS_FIELD.match(currentFieldName)) {
                innerHits = InnerHitBuilder.fromXContent(parseContext);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName)) {
                parentType = parser.text();
            } else if (SCORE_MODE_FIELD.match(currentFieldName)) {
                String scoreModeValue = parser.text();
                if ("score".equals(scoreModeValue)) {
                    score = true;
                } else if ("none".equals(scoreModeValue)) {
                    score = false;
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + scoreModeValue + "] as an option for score_mode");
                }
            } else if (SCORE_FIELD.match(currentFieldName)) {
                score = parser.booleanValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) {
                ignoreUnmapped = parser.booleanValue();
            } 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(), "[has_parent] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder(parentType, iqb, score).ignoreUnmapped(ignoreUnmapped).queryName(queryName).boost(boost);
    if (innerHits != null) {
        queryBuilder.innerHit(innerHits, ignoreUnmapped);
    }
    return queryBuilder;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14