Search in sources :

Example 31 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class PutIndexTemplateRequest method source.

/**
     * The template source definition.
     */
@SuppressWarnings("unchecked")
public PutIndexTemplateRequest source(Map templateSource) {
    Map<String, Object> source = templateSource;
    for (Map.Entry<String, Object> entry : source.entrySet()) {
        String name = entry.getKey();
        if (name.equals("template")) {
            // This is needed to allow for bwc (beats, logstash) with pre-5.0 templates (#21009)
            if (entry.getValue() instanceof String) {
                DEPRECATION_LOGGER.deprecated("Deprecated field [template] used, replaced by [index_patterns]");
                patterns(Collections.singletonList((String) entry.getValue()));
            }
        } else if (name.equals("index_patterns")) {
            if (entry.getValue() instanceof String) {
                patterns(Collections.singletonList((String) entry.getValue()));
            } else if (entry.getValue() instanceof List) {
                List<String> elements = ((List<?>) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList());
                patterns(elements);
            } else {
                throw new IllegalArgumentException("Malformed [template] value, should be a string or a list of strings");
            }
        } else if (name.equals("order")) {
            order(XContentMapValues.nodeIntegerValue(entry.getValue(), order()));
        } else if ("version".equals(name)) {
            if ((entry.getValue() instanceof Integer) == false) {
                throw new IllegalArgumentException("Malformed [version] value, should be an integer");
            }
            version((Integer) entry.getValue());
        } else if (name.equals("settings")) {
            if ((entry.getValue() instanceof Map) == false) {
                throw new IllegalArgumentException("Malformed [settings] section, should include an inner object");
            }
            settings((Map<String, Object>) entry.getValue());
        } else if (name.equals("mappings")) {
            Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
            for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
                if (!(entry1.getValue() instanceof Map)) {
                    throw new IllegalArgumentException("Malformed [mappings] section for type [" + entry1.getKey() + "], should include an inner object describing the mapping");
                }
                mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
            }
        } else if (name.equals("aliases")) {
            aliases((Map<String, Object>) entry.getValue());
        } else {
            // maybe custom?
            IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
            if (proto != null) {
                try {
                    customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
                } catch (IOException e) {
                    throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
                }
            }
        }
    }
    return this;
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class RepositoriesMetaData method fromXContent.

public static RepositoriesMetaData fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    List<RepositoryMetaData> repository = new ArrayList<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            String name = parser.currentName();
            if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                throw new ElasticsearchParseException("failed to parse repository [{}], expected object", name);
            }
            String type = null;
            Settings settings = Settings.EMPTY;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    if ("type".equals(currentFieldName)) {
                        if (parser.nextToken() != XContentParser.Token.VALUE_STRING) {
                            throw new ElasticsearchParseException("failed to parse repository [{}], unknown type", name);
                        }
                        type = parser.text();
                    } else if ("settings".equals(currentFieldName)) {
                        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                            throw new ElasticsearchParseException("failed to parse repository [{}], incompatible params", name);
                        }
                        settings = Settings.builder().put(SettingsLoader.Helper.loadNestedFromMap(parser.mapOrdered())).build();
                    } else {
                        throw new ElasticsearchParseException("failed to parse repository [{}], unknown field [{}]", name, currentFieldName);
                    }
                } else {
                    throw new ElasticsearchParseException("failed to parse repository [{}]", name);
                }
            }
            if (type == null) {
                throw new ElasticsearchParseException("failed to parse repository [{}], missing repository type", name);
            }
            repository.add(new RepositoryMetaData(name, type, settings));
        } else {
            throw new ElasticsearchParseException("failed to parse repositories");
        }
    }
    return new RepositoriesMetaData(repository.toArray(new RepositoryMetaData[repository.size()]));
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Settings(org.elasticsearch.common.settings.Settings)

Example 33 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project elasticsearch by elastic.

the class RestMultiSearchAction method parseRequest.

/**
     * Parses a {@link RestRequest} body and returns a {@link MultiSearchRequest}
     */
public static MultiSearchRequest parseRequest(RestRequest restRequest, boolean allowExplicitIndex) throws IOException {
    MultiSearchRequest multiRequest = new MultiSearchRequest();
    if (restRequest.hasParam("max_concurrent_searches")) {
        multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0));
    }
    parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex, (searchRequest, parser) -> {
        try {
            final QueryParseContext queryParseContext = new QueryParseContext(parser);
            searchRequest.source(SearchSourceBuilder.fromXContent(queryParseContext));
            multiRequest.add(searchRequest);
        } catch (IOException e) {
            throw new ElasticsearchParseException("Exception when parsing search request", e);
        }
    });
    return multiRequest;
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) IOException(java.io.IOException)

Example 34 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException in project crate by crate.

the class ExpressionToByteSizeValueVisitor method visitParameterExpression.

@Override
public ByteSizeValue visitParameterExpression(ParameterExpression node, Row context) {
    ByteSizeValue byteSizeValue;
    Object param = context.get(node.index());
    if (param instanceof Number) {
        byteSizeValue = new ByteSizeValue(((Number) param).longValue());
    } else if (param instanceof String) {
        try {
            byteSizeValue = ByteSizeValue.parseBytesSizeValue((String) param, DEFAULT_VALUE.toString());
        } catch (ElasticsearchParseException e) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Invalid byte size value '%s'", param));
        }
    } else {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Invalid byte size value %s", param));
    }
    return byteSizeValue;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue)

Example 35 with ElasticsearchParseException

use of org.elasticsearch.ElasticsearchParseException 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

ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)81 XContentParser (org.elasticsearch.common.xcontent.XContentParser)29 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)10 Map (java.util.Map)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 List (java.util.List)5 ParsingException (org.elasticsearch.common.ParsingException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 Token (org.elasticsearch.common.xcontent.XContentParser.Token)4 HashSet (java.util.HashSet)3 PutPipelineRequest (org.elasticsearch.action.ingest.PutPipelineRequest)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 Script (org.elasticsearch.script.Script)3 UncheckedIOException (java.io.UncheckedIOException)2 Set (java.util.Set)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2