Search in sources :

Example 16 with Token

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

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

the class PhraseSuggestionBuilder method fromXContent.

public static PhraseSuggestionBuilder fromXContent(XContentParser parser) throws IOException {
    PhraseSuggestionBuilder tmpSuggestion = new PhraseSuggestionBuilder("_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 (PhraseSuggestionBuilder.RWE_LIKELIHOOD_FIELD.match(currentFieldName)) {
                tmpSuggestion.realWordErrorLikelihood(parser.floatValue());
            } else if (PhraseSuggestionBuilder.CONFIDENCE_FIELD.match(currentFieldName)) {
                tmpSuggestion.confidence(parser.floatValue());
            } else if (PhraseSuggestionBuilder.SEPARATOR_FIELD.match(currentFieldName)) {
                tmpSuggestion.separator(parser.text());
            } else if (PhraseSuggestionBuilder.MAXERRORS_FIELD.match(currentFieldName)) {
                tmpSuggestion.maxErrors(parser.floatValue());
            } else if (PhraseSuggestionBuilder.GRAMSIZE_FIELD.match(currentFieldName)) {
                tmpSuggestion.gramSize(parser.intValue());
            } else if (PhraseSuggestionBuilder.FORCE_UNIGRAM_FIELD.match(currentFieldName)) {
                tmpSuggestion.forceUnigrams(parser.booleanValue());
            } else if (PhraseSuggestionBuilder.TOKEN_LIMIT_FIELD.match(currentFieldName)) {
                tmpSuggestion.tokenLimit(parser.intValue());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase] doesn't support field [" + currentFieldName + "]");
            }
        } else if (token == Token.START_ARRAY) {
            if (DirectCandidateGeneratorBuilder.DIRECT_GENERATOR_FIELD.match(currentFieldName)) {
                // for now we only have a single type of generators
                while ((token = parser.nextToken()) == Token.START_OBJECT) {
                    tmpSuggestion.addCandidateGenerator(DirectCandidateGeneratorBuilder.PARSER.apply(parser, null));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase]  doesn't support array field [" + currentFieldName + "]");
            }
        } else if (token == Token.START_OBJECT) {
            if (PhraseSuggestionBuilder.SMOOTHING_MODEL_FIELD.match(currentFieldName)) {
                ensureNoSmoothing(tmpSuggestion);
                tmpSuggestion.smoothingModel(SmoothingModel.fromXContent(parser));
            } else if (PhraseSuggestionBuilder.HIGHLIGHT_FIELD.match(currentFieldName)) {
                String preTag = null;
                String postTag = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (token.isValue()) {
                        if (PhraseSuggestionBuilder.PRE_TAG_FIELD.match(currentFieldName)) {
                            preTag = parser.text();
                        } else if (PhraseSuggestionBuilder.POST_TAG_FIELD.match(currentFieldName)) {
                            postTag = parser.text();
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][highlight] doesn't support field [" + currentFieldName + "]");
                        }
                    }
                }
                tmpSuggestion.highlight(preTag, postTag);
            } else if (PhraseSuggestionBuilder.COLLATE_FIELD.match(currentFieldName)) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        currentFieldName = parser.currentName();
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_FIELD.match(currentFieldName)) {
                        if (tmpSuggestion.collateQuery() != null) {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] query already set, doesn't support additional [" + currentFieldName + "]");
                        }
                        Script template = Script.parse(parser, Script.DEFAULT_TEMPLATE_LANG);
                        tmpSuggestion.collateQuery(template);
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_PARAMS.match(currentFieldName)) {
                        tmpSuggestion.collateParams(parser.map());
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_PRUNE.match(currentFieldName)) {
                        if (parser.isBooleanValue()) {
                            tmpSuggestion.collatePrune(parser.booleanValue());
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] prune must be either 'true' or 'false'");
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "suggester[phrase][collate] doesn't support field [" + currentFieldName + "]");
                    }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[phrase]  doesn't support array field [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "suggester[phrase] doesn't support field [" + 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 PhraseSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 18 with Token

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

the class StupidBackoff method fromXContent.

public static SmoothingModel fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String fieldName = null;
    double discount = DEFAULT_BACKOFF_DISCOUNT;
    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        }
        if (token.isValue() && DISCOUNT_FIELD.match(fieldName)) {
            discount = parser.doubleValue();
        }
    }
    return new StupidBackoff(discount);
}
Also used : Token(org.elasticsearch.common.xcontent.XContentParser.Token) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 19 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)

Example 20 with Token

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

the class StoredScriptSource method parse.

/**
     * This will parse XContent into a {@link StoredScriptSource}.  The following formats can be parsed:
     *
     * The simple script format with no compiler options or user-defined params:
     *
     * Example:
     * {@code
     * {"script": "return Math.log(doc.popularity) * 100;"}
     * }
     *
     * The above format requires the lang to be specified using the deprecated stored script namespace
     * (as a url parameter during a put request).  See {@link ScriptMetaData} for more information about
     * the stored script namespaces.
     *
     * The complex script format using the new stored script namespace
     * where lang and code are required but options is optional:
     *
     * {@code
     * {
     *     "script" : {
     *         "lang" : "<lang>",
     *         "code" : "<code>",
     *         "options" : {
     *             "option0" : "<option0>",
     *             "option1" : "<option1>",
     *             ...
     *         }
     *     }
     * }
     * }
     *
     * Example:
     * {@code
     * {
     *     "script": {
     *         "lang" : "painless",
     *         "code" : "return Math.log(doc.popularity) * params.multiplier"
     *     }
     * }
     * }
     *
     * The simple template format:
     *
     * {@code
     * {
     *     "query" : ...
     * }
     * }
     *
     * The complex template format:
     *
     * {@code
     * {
     *     "template": {
     *         "query" : ...
     *     }
     * }
     * }
     *
     * Note that templates can be handled as both strings and complex JSON objects.
     * Also templates may be part of the 'code' parameter in a script.  The Parser
     * can handle this case as well.
     *
     * @param lang    An optional parameter to allow for use of the deprecated stored
     *                script namespace.  This will be used to specify the language
     *                coming in as a url parameter from a request or for stored templates.
     * @param content The content from the request to be parsed as described above.
     * @return        The parsed {@link StoredScriptSource}.
     */
public static StoredScriptSource parse(String lang, BytesReference content, XContentType xContentType) {
    try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, content)) {
        Token token = parser.nextToken();
        if (token != Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{]");
        }
        token = parser.nextToken();
        if (token != Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" + SCRIPT_PARSE_FIELD.getPreferredName() + ", " + TEMPLATE_PARSE_FIELD.getPreferredName());
        }
        String name = parser.currentName();
        if (SCRIPT_PARSE_FIELD.getPreferredName().equals(name)) {
            token = parser.nextToken();
            if (token == Token.VALUE_STRING) {
                if (lang == null) {
                    throw new IllegalArgumentException("must specify lang as a url parameter when using the deprecated stored script namespace");
                }
                return new StoredScriptSource(lang, parser.text(), Collections.emptyMap());
            } else if (token == Token.START_OBJECT) {
                if (lang == null) {
                    return PARSER.apply(parser, null).build();
                } else {
                    //this is really for search templates, that need to be converted to json format
                    try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
                        builder.copyCurrentStructure(parser);
                        return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
                    }
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, <code>]");
            }
        } else {
            if (lang == null) {
                throw new IllegalArgumentException("unexpected stored script format");
            }
            if (TEMPLATE_PARSE_FIELD.getPreferredName().equals(name)) {
                token = parser.nextToken();
                if (token == Token.VALUE_STRING) {
                    return new StoredScriptSource(lang, parser.text(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
                }
            }
            try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
                if (token != Token.START_OBJECT) {
                    builder.startObject();
                    builder.copyCurrentStructure(parser);
                    builder.endObject();
                } else {
                    builder.copyCurrentStructure(parser);
                }
                return new StoredScriptSource(lang, builder.string(), Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
            }
        }
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Token(org.elasticsearch.common.xcontent.XContentParser.Token) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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