Search in sources :

Example 21 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 22 with Token

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

the class LinearInterpolation method fromXContent.

public static LinearInterpolation fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String fieldName = null;
    double trigramLambda = 0.0;
    double bigramLambda = 0.0;
    double unigramLambda = 0.0;
    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TRIGRAM_FIELD.match(fieldName)) {
                trigramLambda = parser.doubleValue();
                if (trigramLambda < 0) {
                    throw new IllegalArgumentException("trigram_lambda must be positive");
                }
            } else if (BIGRAM_FIELD.match(fieldName)) {
                bigramLambda = parser.doubleValue();
                if (bigramLambda < 0) {
                    throw new IllegalArgumentException("bigram_lambda must be positive");
                }
            } else if (UNIGRAM_FIELD.match(fieldName)) {
                unigramLambda = parser.doubleValue();
                if (unigramLambda < 0) {
                    throw new IllegalArgumentException("unigram_lambda must be positive");
                }
            } else {
                throw new IllegalArgumentException("suggester[phrase][smoothing][linear] doesn't support field [" + fieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + fieldName + "]");
        }
    }
    return new LinearInterpolation(trigramLambda, bigramLambda, unigramLambda);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) ParsingException(org.elasticsearch.common.ParsingException) 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