Search in sources :

Example 86 with ParsingException

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

the class ReverseNestedAggregationBuilder method parse.

public static ReverseNestedAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    String path = null;
    XContentParser.Token token;
    String currentFieldName = null;
    XContentParser parser = context.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("path".equals(currentFieldName)) {
                path = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
        }
    }
    ReverseNestedAggregationBuilder factory = new ReverseNestedAggregationBuilder(aggregationName);
    if (path != null) {
        factory.path(path);
    }
    return factory;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 87 with ParsingException

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

the class GeoDistanceAggregationBuilder method parseGeoPoint.

private static GeoPoint parseGeoPoint(XContentParser parser, QueryParseContext context) throws IOException {
    Token token = parser.currentToken();
    if (token == XContentParser.Token.VALUE_STRING) {
        GeoPoint point = new GeoPoint();
        point.resetFromString(parser.text());
        return point;
    }
    if (token == XContentParser.Token.START_ARRAY) {
        double lat = Double.NaN;
        double lon = Double.NaN;
        while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (Double.isNaN(lon)) {
                lon = parser.doubleValue();
            } else if (Double.isNaN(lat)) {
                lat = parser.doubleValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "malformed [" + ORIGIN_FIELD.getPreferredName() + "]: a geo point array must be of the form [lon, lat]");
            }
        }
        return new GeoPoint(lat, lon);
    }
    if (token == XContentParser.Token.START_OBJECT) {
        String currentFieldName = null;
        double lat = Double.NaN;
        double lon = Double.NaN;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token == XContentParser.Token.VALUE_NUMBER) {
                if ("lat".equals(currentFieldName)) {
                    lat = parser.doubleValue();
                } else if ("lon".equals(currentFieldName)) {
                    lon = parser.doubleValue();
                }
            }
        }
        if (Double.isNaN(lat) || Double.isNaN(lon)) {
            throw new ParsingException(parser.getTokenLocation(), "malformed [" + currentFieldName + "] geo point object. either [lat] or [lon] (or both) are " + "missing");
        }
        return new GeoPoint(lat, lon);
    }
    // should not happen since we only parse geo points when we encounter a string, an object or an array
    throw new IllegalArgumentException("Unexpected token [" + token + "] while parsing geo point");
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token)

Example 88 with ParsingException

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

the class FiltersAggregationBuilder method parse.

public static FiltersAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    List<FiltersAggregator.KeyedFilter> keyedFilters = null;
    List<QueryBuilder> nonKeyedFilters = null;
    XContentParser.Token token = null;
    String currentFieldName = null;
    String otherBucketKey = null;
    Boolean otherBucket = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if (OTHER_BUCKET_FIELD.match(currentFieldName)) {
                otherBucket = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (OTHER_BUCKET_KEY_FIELD.match(currentFieldName)) {
                otherBucketKey = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (FILTERS_FIELD.match(currentFieldName)) {
                keyedFilters = new ArrayList<>();
                String key = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        key = parser.currentName();
                    } else {
                        QueryBuilder filter = context.parseInnerQueryBuilder();
                        keyedFilters.add(new FiltersAggregator.KeyedFilter(key, filter));
                    }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (FILTERS_FIELD.match(currentFieldName)) {
                nonKeyedFilters = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    QueryBuilder filter = context.parseInnerQueryBuilder();
                    nonKeyedFilters.add(filter);
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
        }
    }
    if (otherBucket == null && otherBucketKey != null) {
        // automatically enable the other bucket if a key is set, as per the doc
        otherBucket = true;
    }
    FiltersAggregationBuilder factory;
    if (keyedFilters != null) {
        factory = new FiltersAggregationBuilder(aggregationName, keyedFilters.toArray(new FiltersAggregator.KeyedFilter[keyedFilters.size()]));
    } else {
        factory = new FiltersAggregationBuilder(aggregationName, nonKeyedFilters.toArray(new QueryBuilder[nonKeyedFilters.size()]));
    }
    if (otherBucket != null) {
        factory.otherBucket(otherBucket);
    }
    if (otherBucketKey != null) {
        factory.otherBucketKey(otherBucketKey);
    }
    return factory;
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 89 with ParsingException

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

the class ChildrenAggregationBuilder method parse.

public static ChildrenAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    String childType = null;
    XContentParser.Token token;
    String currentFieldName = null;
    XContentParser parser = context.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("type".equals(currentFieldName)) {
                childType = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
        }
    }
    if (childType == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing [child_type] field for children aggregation [" + aggregationName + "]");
    }
    return new ChildrenAggregationBuilder(aggregationName, childType);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 90 with ParsingException

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

the class MultiGetRequest method parseDocuments.

public static void parseDocuments(XContentParser parser, List<Item> items, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, boolean allowExplicitIndex) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        if (token != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("docs array element should include an object");
        }
        String index = defaultIndex;
        String type = defaultType;
        String id = null;
        String routing = defaultRouting;
        String parent = null;
        List<String> storedFields = null;
        long version = Versions.MATCH_ANY;
        VersionType versionType = VersionType.INTERNAL;
        FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("_index".equals(currentFieldName)) {
                    if (!allowExplicitIndex) {
                        throw new IllegalArgumentException("explicit index in multi get is not allowed");
                    }
                    index = parser.text();
                } else if ("_type".equals(currentFieldName)) {
                    type = parser.text();
                } else if ("_id".equals(currentFieldName)) {
                    id = parser.text();
                } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                    routing = parser.text();
                } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
                    parent = parser.text();
                } else if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    storedFields.add(parser.text());
                } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                    version = parser.longValue();
                } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                    versionType = VersionType.fromString(parser.text());
                } else if ("_source".equals(currentFieldName)) {
                    // check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
                    if (parser.isBooleanValueLenient()) {
                        fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(), fetchSourceContext.excludes());
                    } else if (token == XContentParser.Token.VALUE_STRING) {
                        fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), new String[] { parser.text() }, fetchSourceContext.excludes());
                    } else {
                        throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
                    }
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        storedFields.add(parser.text());
                    }
                } else if ("_source".equals(currentFieldName)) {
                    ArrayList<String> includes = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        includes.add(parser.text());
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes.toArray(Strings.EMPTY_ARRAY), fetchSourceContext.excludes());
                }
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("_source".equals(currentFieldName)) {
                    List<String> currentList = null, includes = null, excludes = null;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                            if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
                                currentList = includes != null ? includes : (includes = new ArrayList<>(2));
                            } else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) {
                                currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
                            } else {
                                throw new ElasticsearchParseException("source definition may not contain [{}]", parser.text());
                            }
                        } else if (token == XContentParser.Token.START_ARRAY) {
                            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                                currentList.add(parser.text());
                            }
                        } else if (token.isValue()) {
                            currentList.add(parser.text());
                        } else {
                            throw new ElasticsearchParseException("unexpected token while parsing source settings");
                        }
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()]));
                }
            }
        }
        String[] aFields;
        if (storedFields != null) {
            aFields = storedFields.toArray(new String[storedFields.size()]);
        } else {
            aFields = defaultFields;
        }
        items.add(new Item(index, type, id).routing(routing).storedFields(aFields).parent(parent).version(version).versionType(versionType).fetchSourceContext(fetchSourceContext == FetchSourceContext.FETCH_SOURCE ? defaultFetchSource : fetchSourceContext));
    }
}
Also used : ArrayList(java.util.ArrayList) VersionType(org.elasticsearch.index.VersionType) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) 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