Search in sources :

Example 11 with Token

use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.

the class XContentParserUtils method parseStoredFieldsValue.

/**
     * Parse the current token depending on its token type. The following token types will be
     * parsed by the corresponding parser methods:
     * <ul>
     *    <li>XContentParser.Token.VALUE_STRING: parser.text()</li>
     *    <li>XContentParser.Token.VALUE_NUMBER: parser.numberValue()</li>
     *    <li>XContentParser.Token.VALUE_BOOLEAN: parser.booleanValue()</li>
     *    <li>XContentParser.Token.VALUE_EMBEDDED_OBJECT: parser.binaryValue()</li>
     * </ul>
     *
     * @throws ParsingException if the token none of the allowed values
     */
public static Object parseStoredFieldsValue(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    Object value = null;
    if (token == XContentParser.Token.VALUE_STRING) {
        //binary values will be parsed back and returned as base64 strings when reading from json and yaml
        value = parser.text();
    } else if (token == XContentParser.Token.VALUE_NUMBER) {
        value = parser.numberValue();
    } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
        value = parser.booleanValue();
    } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
        //binary values will be parsed back and returned as BytesArray when reading from cbor and smile
        value = new BytesArray(parser.binaryValue());
    } else {
        throwUnknownToken(token, parser.getTokenLocation());
    }
    return value;
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) BytesArray(org.elasticsearch.common.bytes.BytesArray)

Example 12 with Token

use of org.elasticsearch.common.xcontent.XContentParser.Token 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 13 with Token

use of org.elasticsearch.common.xcontent.XContentParser.Token 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)

Example 14 with Token

use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.

the class Script method parse.

/**
     * This will parse XContent into a {@link Script}.  The following formats can be parsed:
     *
     * The simple format defaults to an {@link ScriptType#INLINE} with no compiler options or user-defined params:
     *
     * Example:
     * {@code
     * "return Math.log(doc.popularity) * 100;"
     * }
     *
     * The complex format where {@link ScriptType} and idOrCode are required while lang, options and params are not required.
     *
     * {@code
     * {
     *     "<type (inline, stored, file)>" : "<idOrCode>",
     *     "lang" : "<lang>",
     *     "options" : {
     *         "option0" : "<option0>",
     *         "option1" : "<option1>",
     *         ...
     *     },
     *     "params" : {
     *         "param0" : "<param0>",
     *         "param1" : "<param1>",
     *         ...
     *     }
     * }
     * }
     *
     * Example:
     * {@code
     * {
     *     "inline" : "return Math.log(doc.popularity) * params.multiplier",
     *     "lang" : "painless",
     *     "params" : {
     *         "multiplier" : 100.0
     *     }
     * }
     * }
     *
     * This also handles templates in a special way.  If a complexly formatted query is specified as another complex
     * JSON object the query is assumed to be a template, and the format will be preserved.
     *
     * {@code
     * {
     *     "inline" : { "query" : ... },
     *     "lang" : "<lang>",
     *     "options" : {
     *         "option0" : "<option0>",
     *         "option1" : "<option1>",
     *         ...
     *     },
     *     "params" : {
     *         "param0" : "<param0>",
     *         "param1" : "<param1>",
     *         ...
     *     }
     * }
     * }
     *
     * @param parser       The {@link XContentParser} to be used.
     * @param defaultLang  The default language to use if no language is specified.  The default language isn't necessarily
     *                     the one defined by {@link Script#DEFAULT_SCRIPT_LANG} due to backwards compatibility requirements
     *                     related to stored queries using previously default languages.
     *
     * @return             The parsed {@link Script}.
     */
public static Script parse(XContentParser parser, String defaultLang) throws IOException {
    Objects.requireNonNull(defaultLang);
    Token token = parser.currentToken();
    if (token == null) {
        token = parser.nextToken();
    }
    if (token == Token.VALUE_STRING) {
        return new Script(ScriptType.INLINE, defaultLang, parser.text(), Collections.emptyMap());
    }
    return PARSER.apply(parser, null).build(defaultLang);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token)

Example 15 with Token

use of org.elasticsearch.common.xcontent.XContentParser.Token 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)

Aggregations

Token (org.elasticsearch.common.xcontent.XContentParser.Token)22 XContentParser (org.elasticsearch.common.xcontent.XContentParser)10 ParsingException (org.elasticsearch.common.ParsingException)9 ArrayList (java.util.ArrayList)4 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)4 GeoPoint (org.elasticsearch.common.geo.GeoPoint)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Set (java.util.Set)1 SuggestField (org.apache.lucene.search.suggest.document.SuggestField)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 GeoDistance (org.elasticsearch.common.geo.GeoDistance)1 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 NumberType (org.elasticsearch.common.xcontent.XContentParser.NumberType)1 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)1 GeoPointFieldMapper (org.elasticsearch.index.mapper.GeoPointFieldMapper)1