Search in sources :

Example 26 with ParsingException

use of org.elasticsearch.common.ParsingException 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 27 with ParsingException

use of org.elasticsearch.common.ParsingException 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 28 with ParsingException

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

Example 29 with ParsingException

use of org.elasticsearch.common.ParsingException 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 30 with ParsingException

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

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