Search in sources :

Example 61 with ElasticsearchParseException

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

the class CreateIndexRequest method source.

/**
     * Sets the settings and mappings as a single source.
     */
@SuppressWarnings("unchecked")
public CreateIndexRequest source(Map<String, ?> source) {
    boolean found = false;
    for (Map.Entry<String, ?> entry : source.entrySet()) {
        String name = entry.getKey();
        if (name.equals("settings")) {
            found = true;
            settings((Map<String, Object>) entry.getValue());
        } else if (name.equals("mappings")) {
            found = true;
            Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
            for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
                mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
            }
        } else if (name.equals("aliases")) {
            found = true;
            aliases((Map<String, Object>) entry.getValue());
        } else {
            // maybe custom?
            IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
            if (proto != null) {
                found = true;
                try {
                    customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
                } catch (IOException e) {
                    throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
                }
            }
        }
    }
    if (!found) {
        // the top level are settings, use them
        settings(source);
    }
    return this;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) HashMap(java.util.HashMap) Map(java.util.Map) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 62 with ElasticsearchParseException

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

the class CompletionFieldMapper method parse.

/**
     * Acceptable inputs:
     *  "STRING" - interpreted as the field value (input)
     *  "OBJECT" - { "input": STRING|ARRAY, "weight": STRING|INT, "contexts": ARRAY|OBJECT }
     */
private void parse(ParseContext parseContext, Token token, XContentParser parser, Map<String, CompletionInputMetaData> inputMap) throws IOException {
    String currentFieldName = null;
    if (token == Token.VALUE_STRING) {
        inputMap.put(parser.text(), new CompletionInputMetaData(Collections.<String, Set<CharSequence>>emptyMap(), 1));
    } else if (token == Token.START_OBJECT) {
        Set<String> inputs = new HashSet<>();
        int weight = 1;
        Map<String, Set<CharSequence>> contextsMap = new HashMap<>();
        while ((token = parser.nextToken()) != Token.END_OBJECT) {
            if (token == Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
                if (!ALLOWED_CONTENT_FIELD_NAMES.contains(currentFieldName)) {
                    throw new IllegalArgumentException("unknown field name [" + currentFieldName + "], must be one of " + ALLOWED_CONTENT_FIELD_NAMES);
                }
            } else if (currentFieldName != null) {
                if (Fields.CONTENT_FIELD_NAME_INPUT.equals(currentFieldName)) {
                    if (token == Token.VALUE_STRING) {
                        inputs.add(parser.text());
                    } else if (token == Token.START_ARRAY) {
                        while ((token = parser.nextToken()) != Token.END_ARRAY) {
                            if (token == Token.VALUE_STRING) {
                                inputs.add(parser.text());
                            } else {
                                throw new IllegalArgumentException("input array must have string values, but was [" + token.name() + "]");
                            }
                        }
                    } else {
                        throw new IllegalArgumentException("input must be a string or array, but was [" + token.name() + "]");
                    }
                } else if (Fields.CONTENT_FIELD_NAME_WEIGHT.equals(currentFieldName)) {
                    final Number weightValue;
                    if (token == Token.VALUE_STRING) {
                        try {
                            weightValue = Long.parseLong(parser.text());
                        } catch (NumberFormatException e) {
                            throw new IllegalArgumentException("weight must be an integer, but was [" + parser.text() + "]");
                        }
                    } else if (token == Token.VALUE_NUMBER) {
                        NumberType numberType = parser.numberType();
                        if (NumberType.LONG != numberType && NumberType.INT != numberType) {
                            throw new IllegalArgumentException("weight must be an integer, but was [" + parser.numberValue() + "]");
                        }
                        weightValue = parser.numberValue();
                    } else {
                        throw new IllegalArgumentException("weight must be a number or string, but was [" + token.name() + "]");
                    }
                    if (weightValue.longValue() < 0 || weightValue.longValue() > Integer.MAX_VALUE) {
                        // always parse a long to make sure we don't get overflow
                        throw new IllegalArgumentException("weight must be in the interval [0..2147483647], but was [" + weightValue.longValue() + "]");
                    }
                    weight = weightValue.intValue();
                } else if (Fields.CONTENT_FIELD_NAME_CONTEXTS.equals(currentFieldName)) {
                    if (fieldType().hasContextMappings() == false) {
                        throw new IllegalArgumentException("contexts field is not supported for field: [" + fieldType().name() + "]");
                    }
                    ContextMappings contextMappings = fieldType().getContextMappings();
                    XContentParser.Token currentToken = parser.currentToken();
                    if (currentToken == XContentParser.Token.START_OBJECT) {
                        ContextMapping contextMapping = null;
                        String fieldName = null;
                        while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                            if (currentToken == XContentParser.Token.FIELD_NAME) {
                                fieldName = parser.currentName();
                                contextMapping = contextMappings.get(fieldName);
                            } else if (currentToken == XContentParser.Token.VALUE_STRING || currentToken == XContentParser.Token.START_ARRAY || currentToken == XContentParser.Token.START_OBJECT) {
                                assert fieldName != null;
                                assert !contextsMap.containsKey(fieldName);
                                contextsMap.put(fieldName, contextMapping.parseContext(parseContext, parser));
                            } else {
                                throw new IllegalArgumentException("contexts must be an object or an array , but was [" + currentToken + "]");
                            }
                        }
                    } else {
                        throw new IllegalArgumentException("contexts must be an object or an array , but was [" + currentToken + "]");
                    }
                }
            }
        }
        for (String input : inputs) {
            if (inputMap.containsKey(input) == false || inputMap.get(input).weight < weight) {
                inputMap.put(input, new CompletionInputMetaData(contextsMap, weight));
            }
        }
    } else {
        throw new ElasticsearchParseException("failed to parse expected text or object got" + token.name());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ContextMappings(org.elasticsearch.search.suggest.completion.context.ContextMappings) Token(org.elasticsearch.common.xcontent.XContentParser.Token) NumberType(org.elasticsearch.common.xcontent.XContentParser.NumberType) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) HashMap(java.util.HashMap) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 63 with ElasticsearchParseException

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

the class GeoPointFieldMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    context.path().add(simpleName());
    GeoPoint sparse = context.parseExternalValue(GeoPoint.class);
    if (sparse != null) {
        parse(context, sparse);
    } else {
        sparse = new GeoPoint();
        XContentParser.Token token = context.parser().currentToken();
        if (token == XContentParser.Token.START_ARRAY) {
            token = context.parser().nextToken();
            if (token == XContentParser.Token.START_ARRAY) {
                // its an array of array of lon/lat [ [1.2, 1.3], [1.4, 1.5] ]
                while (token != XContentParser.Token.END_ARRAY) {
                    try {
                        parse(context, GeoUtils.parseGeoPoint(context.parser(), sparse));
                    } catch (ElasticsearchParseException e) {
                        if (ignoreMalformed.value() == false) {
                            throw e;
                        }
                    }
                    token = context.parser().nextToken();
                }
            } else {
                // its an array of other possible values
                if (token == XContentParser.Token.VALUE_NUMBER) {
                    double lon = context.parser().doubleValue();
                    token = context.parser().nextToken();
                    double lat = context.parser().doubleValue();
                    while ((token = context.parser().nextToken()) != XContentParser.Token.END_ARRAY) ;
                    parse(context, sparse.reset(lat, lon));
                } else {
                    while (token != XContentParser.Token.END_ARRAY) {
                        if (token == XContentParser.Token.VALUE_STRING) {
                            parsePointFromString(context, sparse, context.parser().text());
                        } else {
                            try {
                                parse(context, GeoUtils.parseGeoPoint(context.parser(), sparse));
                            } catch (ElasticsearchParseException e) {
                                if (ignoreMalformed.value() == false) {
                                    throw e;
                                }
                            }
                        }
                        token = context.parser().nextToken();
                    }
                }
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            parsePointFromString(context, sparse, context.parser().text());
        } else if (token != XContentParser.Token.VALUE_NULL) {
            try {
                parse(context, GeoUtils.parseGeoPoint(context.parser(), sparse));
            } catch (ElasticsearchParseException e) {
                if (ignoreMalformed.value() == false) {
                    throw e;
                }
            }
        }
    }
    context.path().remove();
    return null;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 64 with ElasticsearchParseException

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

the class CategoryContextMapping method parseContext.

/**
     * Parse a set of {@link CharSequence} contexts at index-time.
     * Acceptable formats:
     *
     *  <ul>
     *     <li>Array: <pre>[<i>&lt;string&gt;</i>, ..]</pre></li>
     *     <li>String: <pre>&quot;string&quot;</pre></li>
     *  </ul>
     */
@Override
public Set<CharSequence> parseContext(ParseContext parseContext, XContentParser parser) throws IOException, ElasticsearchParseException {
    final Set<CharSequence> contexts = new HashSet<>();
    Token token = parser.currentToken();
    if (token == Token.VALUE_STRING) {
        contexts.add(parser.text());
    } else if (token == Token.START_ARRAY) {
        while ((token = parser.nextToken()) != Token.END_ARRAY) {
            if (token == Token.VALUE_STRING) {
                contexts.add(parser.text());
            } else {
                throw new ElasticsearchParseException("context array must have string values");
            }
        }
    } else {
        throw new ElasticsearchParseException("contexts must be a string or a list of strings");
    }
    return contexts;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Token(org.elasticsearch.common.xcontent.XContentParser.Token) HashSet(java.util.HashSet)

Example 65 with ElasticsearchParseException

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

the class CategoryQueryContext method fromXContent.

public static CategoryQueryContext fromXContent(QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    Builder builder = builder();
    if (token == XContentParser.Token.START_OBJECT) {
        CATEGORY_PARSER.parse(parser, builder, null);
    } else if (token == XContentParser.Token.VALUE_STRING) {
        builder.setCategory(parser.text());
    } else {
        throw new ElasticsearchParseException("category context must be an object or string");
    }
    return builder.build();
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)106 XContentParser (org.elasticsearch.common.xcontent.XContentParser)44 HashMap (java.util.HashMap)28 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)13 Map (java.util.Map)11 List (java.util.List)7 GeoPoint (org.elasticsearch.common.geo.GeoPoint)7 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ParsingException (org.elasticsearch.common.ParsingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 Version (org.elasticsearch.Version)4 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 PutPipelineRequest (org.elasticsearch.action.ingest.PutPipelineRequest)3