Search in sources :

Example 31 with Token

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

the class CompletionFieldMapper method parse.

/**
 * Parses and indexes inputs
 *
 * Parsing:
 *  Acceptable format:
 *   "STRING" - interpreted as field value (input)
 *   "ARRAY" - each element can be one of "OBJECT" (see below)
 *   "OBJECT" - { "input": STRING|ARRAY, "weight": STRING|INT, "contexts": ARRAY|OBJECT }
 *
 * Indexing:
 *  if context mappings are defined, delegates to {@link ContextMappings#addField(ParseContext.Document, String, String, int, Map)}
 *  else adds inputs as a {@link org.apache.lucene.search.suggest.document.SuggestField}
 */
@Override
public void parse(ParseContext context) throws IOException {
    // parse
    XContentParser parser = context.parser();
    Token token = parser.currentToken();
    Map<String, CompletionInputMetadata> inputMap = new HashMap<>(1);
    if (context.externalValueSet()) {
        inputMap = getInputMapFromExternalValue(context);
    } else if (token == Token.VALUE_NULL) {
        // ignore null values
        return;
    } else if (token == Token.START_ARRAY) {
        while ((token = parser.nextToken()) != Token.END_ARRAY) {
            parse(context, token, parser, inputMap);
        }
    } else {
        parse(context, token, parser, inputMap);
    }
    // index
    for (Map.Entry<String, CompletionInputMetadata> completionInput : inputMap.entrySet()) {
        String input = completionInput.getKey();
        if (input.trim().isEmpty()) {
            context.addIgnoredField(mappedFieldType.name());
            continue;
        }
        // truncate input
        if (input.length() > maxInputLength) {
            int len = Math.min(maxInputLength, input.length());
            if (Character.isHighSurrogate(input.charAt(len - 1))) {
                assert input.length() >= len + 1 && Character.isLowSurrogate(input.charAt(len));
                len += 1;
            }
            input = input.substring(0, len);
        }
        CompletionInputMetadata metadata = completionInput.getValue();
        if (fieldType().hasContextMappings()) {
            fieldType().getContextMappings().addField(context.doc(), fieldType().name(), input, metadata.weight, metadata.contexts);
        } else {
            context.doc().add(new SuggestField(fieldType().name(), input, metadata.weight));
        }
    }
    createFieldNamesField(context);
    for (CompletionInputMetadata metadata : inputMap.values()) {
        ParseContext externalValueContext = context.createExternalValueContext(metadata);
        multiFields.parse(this, externalValueContext);
    }
}
Also used : SuggestField(org.apache.lucene.search.suggest.document.SuggestField) HashMap(java.util.HashMap) Token(org.opensearch.common.xcontent.XContentParser.Token) HashMap(java.util.HashMap) Map(java.util.Map) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 32 with Token

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

the class GeoDistanceAggregationBuilder method parseRange.

private static Range parseRange(XContentParser parser) throws IOException {
    String fromAsStr = null;
    String toAsStr = null;
    double from = 0.0;
    double to = Double.POSITIVE_INFINITY;
    String key = null;
    String currentFieldName = null;
    Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                from = parser.doubleValue();
            } else if (TO_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                to = parser.doubleValue();
            } else {
                XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (KEY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                key = parser.text();
            } else if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fromAsStr = parser.text();
            } else if (TO_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                toAsStr = parser.text();
            } else {
                XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.VALUE_NULL) {
            if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler()) || TO_FIELD.match(currentFieldName, parser.getDeprecationHandler()) || KEY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
            // ignore null value
            } else {
                XContentParserUtils.throwUnknownField(currentFieldName, parser.getTokenLocation());
            }
        } else {
            XContentParserUtils.throwUnknownToken(token, parser.getTokenLocation());
        }
    }
    if (fromAsStr != null || toAsStr != null) {
        return new Range(key, Double.parseDouble(fromAsStr), Double.parseDouble(toAsStr));
    } else {
        return new Range(key, from, to);
    }
}
Also used : Token(org.opensearch.common.xcontent.XContentParser.Token)

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