Search in sources :

Example 46 with ParsingException

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

the class SmoothingModel method fromXContent.

public static SmoothingModel fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String fieldName = null;
    SmoothingModel model = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (LinearInterpolation.PARSE_FIELD.match(fieldName)) {
                model = LinearInterpolation.fromXContent(parser);
            } else if (Laplace.PARSE_FIELD.match(fieldName)) {
                model = Laplace.fromXContent(parser);
            } else if (StupidBackoff.PARSE_FIELD.match(fieldName)) {
                model = StupidBackoff.fromXContent(parser);
            } else {
                throw new IllegalArgumentException("suggester[phrase] doesn't support object field [" + fieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[smoothing] unknown token [" + token + "] after [" + fieldName + "]");
        }
    }
    return model;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 47 with ParsingException

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

the class TermSuggestionBuilder method fromXContent.

public static TermSuggestionBuilder fromXContent(XContentParser parser) throws IOException {
    TermSuggestionBuilder tmpSuggestion = new TermSuggestionBuilder("_na_");
    XContentParser.Token token;
    String currentFieldName = null;
    String fieldname = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SuggestionBuilder.ANALYZER_FIELD.match(currentFieldName)) {
                tmpSuggestion.analyzer(parser.text());
            } else if (SuggestionBuilder.FIELDNAME_FIELD.match(currentFieldName)) {
                fieldname = parser.text();
            } else if (SuggestionBuilder.SIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.size(parser.intValue());
            } else if (SuggestionBuilder.SHARDSIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.shardSize(parser.intValue());
            } else if (SUGGESTMODE_FIELD.match(currentFieldName)) {
                tmpSuggestion.suggestMode(SuggestMode.resolve(parser.text()));
            } else if (ACCURACY_FIELD.match(currentFieldName)) {
                tmpSuggestion.accuracy(parser.floatValue());
            } else if (SORT_FIELD.match(currentFieldName)) {
                tmpSuggestion.sort(SortBy.resolve(parser.text()));
            } else if (STRING_DISTANCE_FIELD.match(currentFieldName)) {
                tmpSuggestion.stringDistance(StringDistanceImpl.resolve(parser.text()));
            } else if (MAX_EDITS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxEdits(parser.intValue());
            } else if (MAX_INSPECTIONS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxInspections(parser.intValue());
            } else if (MAX_TERM_FREQ_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxTermFreq(parser.floatValue());
            } else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
                tmpSuggestion.prefixLength(parser.intValue());
            } else if (MIN_WORD_LENGTH_FIELD.match(currentFieldName)) {
                tmpSuggestion.minWordLength(parser.intValue());
            } else if (MIN_DOC_FREQ_FIELD.match(currentFieldName)) {
                tmpSuggestion.minDocFreq(parser.floatValue());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[term] doesn't support field [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "suggester[term] parsing failed on [" + currentFieldName + "]");
        }
    }
    // now we should have field name, check and copy fields over to the suggestion builder we return
    if (fieldname == null) {
        throw new ElasticsearchParseException("the required field option [" + FIELDNAME_FIELD.getPreferredName() + "] is missing");
    }
    return new TermSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 48 with ParsingException

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

the class ShardSearchFailureTests method testToXContent.

public void testToXContent() throws IOException {
    ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(0, 0, "some message", null), new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123)));
    BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean());
    assertEquals("{\"shard\":123," + "\"index\":\"indexName\"," + "\"node\":\"nodeId\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"some message\"," + "\"line\":0," + "\"col\":0" + "}" + "}", xContent.utf8ToString());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsingException(org.elasticsearch.common.ParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index)

Example 49 with ParsingException

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

the class ScriptMetaData method fromXContent.

/**
     * This will parse XContent into {@link ScriptMetaData}.
     *
     * The following format will be parsed for the new namespace:
     *
     * {@code
     * {
     *     "<id>" : "<{@link StoredScriptSource#fromXContent(XContentParser)}>",
     *     "<id>" : "<{@link StoredScriptSource#fromXContent(XContentParser)}>",
     *     ...
     * }
     * }
     *
     * The following format will be parsed for the deprecated namespace:
     *
     * {@code
     * {
     *     "<id>" : "<code>",
     *     "<id>" : "<code>",
     *     ...
     * }
     * }
     *
     * Note when using the deprecated namespace, the language will be pulled from
     * the id and options will be set to an empty {@link Map}.
     */
public static ScriptMetaData fromXContent(XContentParser parser) throws IOException {
    Map<String, StoredScriptSource> scripts = new HashMap<>();
    String id = null;
    StoredScriptSource source;
    Token token = parser.currentToken();
    if (token == null) {
        token = parser.nextToken();
    }
    if (token != Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{]");
    }
    token = parser.nextToken();
    while (token != Token.END_OBJECT) {
        switch(token) {
            case FIELD_NAME:
                id = parser.currentName();
                break;
            case VALUE_STRING:
                if (id == null) {
                    throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [<id>, <code>, {]");
                }
                int split = id.indexOf('#');
                if (split == -1) {
                    throw new IllegalArgumentException("illegal stored script id [" + id + "], does not contain lang");
                } else {
                    source = new StoredScriptSource(id.substring(0, split), parser.text(), Collections.emptyMap());
                }
                scripts.put(id, source);
                id = null;
                break;
            case START_OBJECT:
                if (id == null) {
                    throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [<id>, <code>, {]");
                }
                source = StoredScriptSource.fromXContent(parser);
                scripts.put(id, source);
                break;
            default:
                throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [<id>, <code>, {]");
        }
        token = parser.nextToken();
    }
    return new ScriptMetaData(scripts);
}
Also used : HashMap(java.util.HashMap) ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token)

Example 50 with ParsingException

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

the class DateHistogramAggregationBuilder method parseOrder.

// similar to the parsing oh histogram orders, but also accepts _time as an alias for _key
private static InternalOrder parseOrder(XContentParser parser, QueryParseContext context) throws IOException {
    InternalOrder order = null;
    Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            String dir = parser.text();
            boolean asc = "asc".equals(dir);
            if (!asc && !"desc".equals(dir)) {
                throw new ParsingException(parser.getTokenLocation(), "Unknown order direction: [" + dir + "]. Should be either [asc] or [desc]");
            }
            order = resolveOrder(currentFieldName, asc);
        }
    }
    return order;
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token)

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