Search in sources :

Example 6 with Token

use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.

the class MultiGetResponse method fromXContent.

public static MultiGetResponse fromXContent(XContentParser parser) throws IOException {
    String currentFieldName = null;
    List<MultiGetItemResponse> items = new ArrayList<>();
    for (Token token = parser.nextToken(); token != Token.END_OBJECT; token = parser.nextToken()) {
        switch(token) {
            case FIELD_NAME:
                currentFieldName = parser.currentName();
                break;
            case START_ARRAY:
                if (DOCS.getPreferredName().equals(currentFieldName)) {
                    for (token = parser.nextToken(); token != Token.END_ARRAY; token = parser.nextToken()) {
                        if (token == Token.START_OBJECT) {
                            items.add(parseItem(parser));
                        }
                    }
                }
                break;
            default:
                // this is parsing logic on the client side.
                break;
        }
    }
    return new MultiGetResponse(items.toArray(new MultiGetItemResponse[0]));
}
Also used : ArrayList(java.util.ArrayList) Token(org.opensearch.common.xcontent.XContentParser.Token)

Example 7 with Token

use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.

the class MultiGetResponse method parseItem.

private static MultiGetItemResponse parseItem(XContentParser parser) throws IOException {
    String currentFieldName = null;
    String index = null;
    String id = null;
    OpenSearchException exception = null;
    GetResult getResult = null;
    for (Token token = parser.nextToken(); token != Token.END_OBJECT; token = parser.nextToken()) {
        switch(token) {
            case FIELD_NAME:
                currentFieldName = parser.currentName();
                if (INDEX.match(currentFieldName, parser.getDeprecationHandler()) == false && ID.match(currentFieldName, parser.getDeprecationHandler()) == false && ERROR.match(currentFieldName, parser.getDeprecationHandler()) == false) {
                    getResult = GetResult.fromXContentEmbedded(parser, index, id);
                }
                break;
            case VALUE_STRING:
                if (INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
                    index = parser.text();
                } else if (ID.match(currentFieldName, parser.getDeprecationHandler())) {
                    id = parser.text();
                }
                break;
            case START_OBJECT:
                if (ERROR.match(currentFieldName, parser.getDeprecationHandler())) {
                    exception = OpenSearchException.fromXContent(parser);
                }
                break;
            default:
                // this is parsing logic on the client side.
                break;
        }
        if (getResult != null) {
            break;
        }
    }
    if (exception != null) {
        return new MultiGetItemResponse(null, new Failure(index, id, exception));
    } else {
        GetResponse getResponse = new GetResponse(getResult);
        return new MultiGetItemResponse(getResponse, null);
    }
}
Also used : GetResult(org.opensearch.index.get.GetResult) OpenSearchException(org.opensearch.OpenSearchException) Token(org.opensearch.common.xcontent.XContentParser.Token)

Example 8 with Token

use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.

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(parser.text(), Collections.<String, Set<String>>emptyMap(), 1));
    } else if (token == Token.START_OBJECT) {
        Set<String> inputs = new HashSet<>();
        int weight = 1;
        Map<String, Set<String>> 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() + "]");
                    }
                    // always parse a long to make sure we don't get overflow
                    if (weightValue.longValue() < 0 || weightValue.longValue() > Integer.MAX_VALUE) {
                        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 {
                                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 + "]");
                    }
                }
            }
        }
        for (String input : inputs) {
            if (inputMap.containsKey(input) == false || inputMap.get(input).weight < weight) {
                inputMap.put(input, new CompletionInputMetadata(input, contextsMap, weight));
            }
        }
    } else {
        throw new ParsingException(parser.getTokenLocation(), "failed to parse [" + parser.currentName() + "]: expected text or object, but got " + token.name());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ContextMappings(org.opensearch.search.suggest.completion.context.ContextMappings) Token(org.opensearch.common.xcontent.XContentParser.Token) NumberType(org.opensearch.common.xcontent.XContentParser.NumberType) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) ParsingException(org.opensearch.common.ParsingException) HashMap(java.util.HashMap) Map(java.util.Map) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 9 with Token

use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.

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, parser.getDeprecationHandler())) {
                tmpSuggestion.analyzer(parser.text());
            } else if (SuggestionBuilder.FIELDNAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fieldname = parser.text();
            } else if (SuggestionBuilder.SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.size(parser.intValue());
            } else if (SuggestionBuilder.SHARDSIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.shardSize(parser.intValue());
            } else if (PhraseSuggestionBuilder.RWE_LIKELIHOOD_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.realWordErrorLikelihood(parser.floatValue());
            } else if (PhraseSuggestionBuilder.CONFIDENCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.confidence(parser.floatValue());
            } else if (PhraseSuggestionBuilder.SEPARATOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.separator(parser.text());
            } else if (PhraseSuggestionBuilder.MAXERRORS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.maxErrors(parser.floatValue());
            } else if (PhraseSuggestionBuilder.GRAMSIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.gramSize(parser.intValue());
            } else if (PhraseSuggestionBuilder.FORCE_UNIGRAM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.forceUnigrams(parser.booleanValue());
            } else if (PhraseSuggestionBuilder.TOKEN_LIMIT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                // 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, parser.getDeprecationHandler())) {
                ensureNoSmoothing(tmpSuggestion);
                tmpSuggestion.smoothingModel(SmoothingModel.fromXContent(parser));
            } else if (PhraseSuggestionBuilder.HIGHLIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                            preTag = parser.text();
                        } else if (PhraseSuggestionBuilder.POST_TAG_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                            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, parser.getDeprecationHandler())) {
                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, parser.getDeprecationHandler())) {
                        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, parser.getDeprecationHandler())) {
                        tmpSuggestion.collateParams(parser.map());
                    } else if (PhraseSuggestionBuilder.COLLATE_QUERY_PRUNE.match(currentFieldName, parser.getDeprecationHandler())) {
                        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 OpenSearchParseException("the required field option [" + FIELDNAME_FIELD.getPreferredName() + "] is missing");
    }
    return new PhraseSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : Token(org.opensearch.common.xcontent.XContentParser.Token) TemplateScript(org.opensearch.script.TemplateScript) Script(org.opensearch.script.Script) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 10 with Token

use of org.opensearch.common.xcontent.XContentParser.Token in project OpenSearch by opensearch-project.

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 source are required but options is optional:
 *
 * {@code
 * {
 *     "script" : {
 *         "lang" : "<lang>",
 *         "source" : "<source>",
 *         "options" : {
 *             "option0" : "<option0>",
 *             "option1" : "<option1>",
 *             ...
 *         }
 *     }
 * }
 * }
 *
 * Example:
 * {@code
 * {
 *     "script": {
 *         "lang" : "painless",
 *         "source" : "return Math.log(doc.popularity) * params.multiplier"
 *     }
 * }
 * }
 *
 * The use of "source" may also be substituted with "code" for backcompat with 5.3 to 5.5 format. For example:
 *
 * {@code
 * {
 *     "script" : {
 *         "lang" : "<lang>",
 *         "code" : "<source>",
 *         "options" : {
 *             "option0" : "<option0>",
 *             "option1" : "<option1>",
 *             ...
 *         }
 *     }
 * }
 * }
 *
 * Note that the "source" parameter can also handle template parsing including from
 * a complex JSON object.
 *
 * @param content The content from the request to be parsed as described above.
 * @return        The parsed {@link StoredScriptSource}.
 */
public static StoredScriptSource parse(BytesReference content, XContentType xContentType) {
    try (InputStream stream = content.streamInput();
        XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
        Token token = parser.nextToken();
        if (token != Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{]");
        }
        token = parser.nextToken();
        if (token == Token.END_OBJECT) {
            deprecationLogger.deprecate("empty_templates", "empty templates should no longer be used");
            return new StoredScriptSource(Script.DEFAULT_TEMPLATE_LANG, "", Collections.emptyMap());
        }
        if (token != Token.FIELD_NAME) {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + ", expected [" + SCRIPT_PARSE_FIELD.getPreferredName() + "]");
        }
        String name = parser.currentName();
        if (SCRIPT_PARSE_FIELD.getPreferredName().equals(name)) {
            token = parser.nextToken();
            if (token == Token.START_OBJECT) {
                return PARSER.apply(parser, null).build(false);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "], expected [{, <source>]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "unexpected field [" + name + "], expected [" + SCRIPT_PARSE_FIELD.getPreferredName() + "]");
        }
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
Also used : InputStream(java.io.InputStream) ParsingException(org.opensearch.common.ParsingException) Token(org.opensearch.common.xcontent.XContentParser.Token) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

Token (org.opensearch.common.xcontent.XContentParser.Token)32 ParsingException (org.opensearch.common.ParsingException)11 XContentParser (org.opensearch.common.xcontent.XContentParser)10 ArrayList (java.util.ArrayList)6 OpenSearchParseException (org.opensearch.OpenSearchParseException)6 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 GeoPoint (org.opensearch.common.geo.GeoPoint)4 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)4 OpenSearchException (org.opensearch.OpenSearchException)3 Map (java.util.Map)2 Set (java.util.Set)2 BytesArray (org.opensearch.common.bytes.BytesArray)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 List (java.util.List)1 FeatureField (org.apache.lucene.document.FeatureField)1 Explanation (org.apache.lucene.search.Explanation)1 SuggestField (org.apache.lucene.search.suggest.document.SuggestField)1