Java Code Examples for org.elasticsearch.common.xcontent.XContentParser#Token

The following examples show how to use org.elasticsearch.common.xcontent.XContentParser#Token . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQLRequestParser.java    From crate with Apache License 2.0 6 votes vote down vote up
public static void parse(SQLRequestParseContext parseContext, XContentParser parser) throws Exception {
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            String fieldName = parser.currentName();
            parser.nextToken();
            SQLParseElement element = ELEMENT_PARSERS.get(fieldName);
            if (element == null) {
                throw new SQLParseException("No parser for element [" + fieldName + "]");
            }
            element.parse(parser, parseContext);
        } else if (token == null) {
            break;
        }
    }
}
 
Example 2
Source File: PathHierarchyTests.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
public void testParser() throws Exception {
    // can create the factory with utf8 separator
    String separator = "夢";
    XContentParser stParser = createParser(JsonXContent.jsonXContent,
            "{\"field\":\"path\", \"separator\": \"" + separator + "\"}");
    XContentParser.Token token = stParser.nextToken();
    assertSame(XContentParser.Token.START_OBJECT, token);
    assertNotNull(PathHierarchyAggregationBuilder.parse("path_hierarchy", stParser));

    // can create the factory with an array of orders
    String orders = "[{\"_key\": \"asc\"}, {\"_count\": \"desc\"}]";
    stParser = createParser(JsonXContent.jsonXContent,
            "{\"field\":\"path\", \"order\": " + orders + "}");
    assertNotNull(PathHierarchyAggregationBuilder.parse("path_hierarchy", stParser));

}
 
Example 3
Source File: SignificantTermsParametersParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void parseSpecial(String aggregationName, XContentParser parser, SearchContext context, XContentParser.Token token, String currentFieldName) throws IOException {

    if (token == XContentParser.Token.START_OBJECT) {
        SignificanceHeuristicParser significanceHeuristicParser = significanceHeuristicParserMapper.get(currentFieldName);
        if (significanceHeuristicParser != null) {
            significanceHeuristic = significanceHeuristicParser.parse(parser, context.parseFieldMatcher(), context);
        } else if (context.parseFieldMatcher().match(currentFieldName, BACKGROUND_FILTER)) {
            filter = context.queryParserService().parseInnerFilter(parser).query();
        } else {
            throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                    + currentFieldName + "].", parser.getTokenLocation());
        }
    } else {
        throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName
                + "].", parser.getTokenLocation());
    }
}
 
Example 4
Source File: DocumentParser.java    From crate with Apache License 2.0 6 votes vote down vote up
private static void parseNonDynamicArray(ParseContext context, ObjectMapper mapper, String lastFieldName, String arrayFieldName) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        if (token == XContentParser.Token.START_OBJECT) {
            parseObject(context, mapper, lastFieldName);
        } else if (token == XContentParser.Token.START_ARRAY) {
            parseArray(context, mapper, lastFieldName);
        } else if (token == XContentParser.Token.FIELD_NAME) {
            lastFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_NULL) {
            parseNullValue(context, mapper, lastFieldName);
        } else if (token == null) {
            throw new MapperParsingException("object mapping for [" + mapper.name() + "] with array for [" + arrayFieldName + "] tried to parse as array, but got EOF, is there a mismatch in types for the same field?");
        } else {
            parseValue(context, mapper, lastFieldName, token);
        }
    }
}
 
Example 5
Source File: Alias.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an alias and returns its parsed representation
 */
public static Alias fromXContent(XContentParser parser) throws IOException {
    Alias alias = new Alias(parser.currentName());

    String currentFieldName = null;
    XContentParser.Token token = parser.nextToken();
    if (token == null) {
        throw new IllegalArgumentException("No alias is specified");
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("filter".equals(currentFieldName)) {
                Map<String, Object> filter = parser.mapOrdered();
                alias.filter(filter);
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("routing".equals(currentFieldName)) {
                alias.routing(parser.text());
            } else if ("index_routing".equals(currentFieldName) || "indexRouting".equals(currentFieldName) || "index-routing".equals(currentFieldName)) {
                alias.indexRouting(parser.text());
            } else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName) || "search-routing".equals(currentFieldName)) {
                alias.searchRouting(parser.text());
            }
        }
    }
    return alias;
}
 
Example 6
Source File: DocumentParser.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void validateEnd(XContentParser parser) throws IOException {
    XContentParser.Token token;// only check for end of tokens if we created the parser here
    // try to parse the next token, this should be null if the object is ended properly
    // but will throw a JSON exception if the extra tokens is not valid JSON (this will be handled by the catch)
    token = parser.nextToken();
    if (token != null) {
        throw new IllegalArgumentException("Malformed content, found extra data after parsing: " + token);
    }
}
 
Example 7
Source File: DecayFunctionParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private AbstractDistanceScoreFunction parseGeoVariable(String fieldName, XContentParser parser, QueryParseContext parseContext,
        GeoPointFieldMapper.GeoPointFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    GeoPoint origin = new GeoPoint();
    String scaleString = null;
    String offsetString = "0km";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (parameterName.equals(DecayFunctionBuilder.SCALE)) {
            scaleString = parser.text();
        } else if (parameterName.equals(DecayFunctionBuilder.ORIGIN)) {
            origin = GeoUtils.parseGeoPoint(parser);
        } else if (parameterName.equals(DecayFunctionBuilder.DECAY)) {
            decay = parser.doubleValue();
        } else if (parameterName.equals(DecayFunctionBuilder.OFFSET)) {
            offsetString = parser.text();
        } else {
            throw new ElasticsearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (origin == null || scaleString == null) {
        throw new ElasticsearchParseException("[{}] and [{}] must be set for geo fields.", DecayFunctionBuilder.ORIGIN, DecayFunctionBuilder.SCALE);
    }
    double scale = DistanceUnit.DEFAULT.parse(scaleString, DistanceUnit.DEFAULT);
    double offset = DistanceUnit.DEFAULT.parse(offsetString, DistanceUnit.DEFAULT);
    IndexGeoPointFieldData indexFieldData = parseContext.getForField(fieldType);
    return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode);

}
 
Example 8
Source File: BooleanFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
        return;
    }

    Boolean value = context.parseExternalValue(Boolean.class);
    if (value == null) {
        XContentParser.Token token = context.parser().currentToken();
        if (token == XContentParser.Token.VALUE_NULL) {
            if (fieldType().nullValue() != null) {
                value = fieldType().nullValue();
            }
        } else {
            value = context.parser().booleanValue();
        }
    }

    if (value == null) {
        return;
    }
    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        fields.add(new Field(fieldType().name(), value ? "T" : "F", fieldType()));
    }
    if (fieldType().hasDocValues()) {
        fields.add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0));
    } else {
        createFieldNamesField(context, fields);
    }
}
 
Example 9
Source File: PercolatorQueriesRegistry.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
Query parsePercolatorDocument(String id, BytesReference source) {
    String type = null;
    BytesReference querySource = null;
    try (XContentParser sourceParser = XContentHelper.createParser(source)) {
        String currentFieldName = null;
        XContentParser.Token token = sourceParser.nextToken(); // move the START_OBJECT
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchException("failed to parse query [" + id + "], not starting with OBJECT");
        }
        while ((token = sourceParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = sourceParser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    if (type != null) {
                        return parseQuery(type, sourceParser);
                    } else {
                        XContentBuilder builder = XContentFactory.contentBuilder(sourceParser.contentType());
                        builder.copyCurrentStructure(sourceParser);
                        querySource = builder.bytes();
                        builder.close();
                    }
                } else {
                    sourceParser.skipChildren();
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                sourceParser.skipChildren();
            } else if (token.isValue()) {
                if ("type".equals(currentFieldName)) {
                    type = sourceParser.text();
                }
            }
        }
        try (XContentParser queryParser = XContentHelper.createParser(querySource)) {
            return parseQuery(type, queryParser);
        }
    } catch (Exception e) {
        throw new PercolatorException(shardId().index(), "failed to parse query [" + id + "]", e);
    }
}
 
Example 10
Source File: GeoJsonParser.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Recursive method which parses the arrays of coordinates used to define
 * Shapes
 *
 * @param parser
 *            Parser that will be read from
 * @return CoordinateNode representing the start of the coordinate tree
 * @throws IOException
 *             Thrown if an error occurs while reading from the
 *             XContentParser
 */
private static CoordinateNode parseCoordinates(XContentParser parser, boolean ignoreZValue) throws IOException {
    if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
        parser.skipChildren();
        parser.nextToken();
        throw new ElasticsearchParseException("coordinates cannot be specified as objects");
    }

    XContentParser.Token token = parser.nextToken();
    // Base cases
    if (token != XContentParser.Token.START_ARRAY &&
        token != XContentParser.Token.END_ARRAY &&
        token != XContentParser.Token.VALUE_NULL) {
        return new CoordinateNode(parseCoordinate(parser, ignoreZValue));
    } else if (token == XContentParser.Token.VALUE_NULL) {
        throw new IllegalArgumentException("coordinates cannot contain NULL values)");
    }

    List<CoordinateNode> nodes = new ArrayList<>();
    while (token != XContentParser.Token.END_ARRAY) {
        CoordinateNode node = parseCoordinates(parser, ignoreZValue);
        if (nodes.isEmpty() == false && nodes.get(0).numDimensions() != node.numDimensions()) {
            throw new ElasticsearchParseException("Exception parsing coordinates: number of dimensions do not match");
        }
        nodes.add(node);
        token = parser.nextToken();
    }

    return new CoordinateNode(nodes);
}
 
Example 11
Source File: VersionParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        context.version(parser.booleanValue());
    }
}
 
Example 12
Source File: StandardnumberMapper.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (context.externalValueSet()) {
        return;
    }
    XContentParser parser = context.parser();
    if (parser.currentToken() == XContentParser.Token.VALUE_NULL) {
        return;
    }
    String value = fieldType().nullValueAsString();
    if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
        XContentParser.Token token;
        String currentFieldName = null;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else {
                if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) {
                    value = parser.textOrNull();
                }
            }
        }
    } else {
        value = parser.textOrNull();
    }
    try {
        Collection<CharSequence> stdnums = service.lookup(settings, value);
        for (CharSequence stdnum : stdnums) {
            Field field = new Field(fieldType().name(), stdnum.toString(), fieldType());
            fields.add(field);
        }
    } catch (NumberFormatException e) {
        logger.trace(e.getMessage(), e);
        context.createExternalValueContext("unknown");
    }
}
 
Example 13
Source File: RescoreParseElement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void parseSingleRescoreContext(XContentParser parser, SearchContext context) throws Exception {
    String fieldName = null;
    RescoreSearchContext rescoreContext = null;
    Integer windowSize = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
            if (QueryRescorer.NAME.equals(fieldName)) {
                // we only have one at this point
                Rescorer rescorer = QueryRescorer.INSTANCE;
                token = parser.nextToken();
                if (token != XContentParser.Token.START_OBJECT) {
                    throw new ElasticsearchParseException("rescore type malformed, must start with start_object");
                }
                rescoreContext = rescorer.parse(parser, context);
            }
        } else if (token.isValue()) {
            if ("window_size".equals(fieldName)) {
                windowSize = parser.intValue();
            } else {
                throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]");
            }
        }
    }
    if (rescoreContext == null) {
        throw new IllegalArgumentException("missing rescore type");
    }
    if (windowSize != null) {
        rescoreContext.setWindowSize(windowSize.intValue());
    }
    context.addRescore(rescoreContext);
}
 
Example 14
Source File: ImportSettingsParseElement.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(XContentParser parser, ImportContext context)
        throws Exception {
    XContentParser.Token token = parser.currentToken();
    if (token.isValue()) {
        ((ImportContext)context).settings(parser.booleanValue());
    }
}
 
Example 15
Source File: PhraseCountQueryBuilder.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public static Optional<PhraseCountQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    boolean inOrder = false;
    boolean weightedCount = false;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (PhraseCountQueryBuilder.QUERY_FIELD.match(currentFieldName)) {
                        value = parser.objectText();
                    } else if (ANALYZER_FIELD.match(currentFieldName)) {
                        analyzer = parser.text();
                    } else if(IN_ORDER_FIELD.match(currentFieldName)) {
                        inOrder = parser.booleanValue();
                    } else if (WEIGHTED_COUNT_FIELD.match(currentFieldName)) {
                        weightedCount = parser.booleanValue();
                    } else if (BOOST_FIELD.match(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if (SLOP_FIELD.match(currentFieldName)) {
                        slop = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                        queryName = parser.text();
                    } else {
                        throw new ParsingException(parser.getTokenLocation(),
                            "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(),
                        "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }

    PhraseCountQueryBuilder phraseCountQuery = new PhraseCountQueryBuilder(fieldName, value);
    phraseCountQuery.analyzer(analyzer);
    phraseCountQuery.slop(slop);
    phraseCountQuery.inOrder(inOrder);
    phraseCountQuery.weightedCount(weightedCount);
    phraseCountQuery.queryName(queryName);
    phraseCountQuery.boost(boost);
    return Optional.of(phraseCountQuery);
}
 
Example 16
Source File: ChildrenParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    String childType = null;

    XContentParser.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) {
            if ("type".equals(currentFieldName)) {
                childType = parser.text();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (childType == null) {
        throw new SearchParseException(context, "Missing [child_type] field for children aggregation [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    ValuesSourceConfig<ValuesSource.Bytes.WithOrdinals.ParentChild> config = new ValuesSourceConfig<>(ValuesSource.Bytes.WithOrdinals.ParentChild.class);
    DocumentMapper childDocMapper = context.mapperService().documentMapper(childType);

    String parentType = null;
    Query parentFilter = null;
    Query childFilter = null;
    if (childDocMapper != null) {
        ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
        if (!parentFieldMapper.active()) {
            throw new SearchParseException(context, "[children] no [_parent] field not configured that points to a parent type", parser.getTokenLocation());
        }
        parentType = parentFieldMapper.type();
        DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType);
        if (parentDocMapper != null) {
            // TODO: use the query API
            parentFilter = parentDocMapper.typeFilter();
            childFilter = childDocMapper.typeFilter();
            ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType());
            config.fieldContext(new FieldContext(parentFieldMapper.fieldType().names().indexName(), parentChildIndexFieldData, parentFieldMapper.fieldType()));
        } else {
            config.unmapped(true);
        }
    } else {
        config.unmapped(true);
    }
    return new ParentToChildrenAggregator.Factory(aggregationName, config, parentType, parentFilter, childFilter);
}
 
Example 17
Source File: FilteredQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    deprecationLogger.deprecated("The [filtered] query is deprecated, please use a [bool] query instead with a [must] "
            + "clause for the query part and a [filter] clause for the filter part.");

    XContentParser parser = parseContext.parser();

    Query query = Queries.newMatchAllQuery();
    Query filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("query".equals(currentFieldName)) {
                query = parseContext.parseInnerQuery();
            } else if ("filter".equals(currentFieldName)) {
                filterFound = true;
                filter = parseContext.parseInnerFilter();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("strategy".equals(currentFieldName)) {
                // ignore
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        }
    }

    // parsed internally, but returned null during parsing...
    if (query == null) {
        return null;
    }

    BooleanQuery filteredQuery = Queries.filtered(query, filter);
    filteredQuery.setBoost(boost);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, filteredQuery);
    }
    return filteredQuery;
}
 
Example 18
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void parseSource(SearchContext context, BytesReference source) throws SearchParseException {
    // nothing to parse...
    if (source == null || source.length() == 0) {
        return;
    }
    String abc = source.toUtf8();
    XContentParser parser = null;
    try {
        parser = XContentFactory.xContent(source).createParser(source);
        XContentParser.Token token;
        token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchParseException("failed to parse search source. source must be an object, but found [{}] instead", token.name());
        }
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                String fieldName = parser.currentName();
                parser.nextToken();
                SearchParseElement element = elementParsers.get(fieldName);
                if (element == null) {
                    throw new SearchParseException(context, "failed to parse search source. unknown search element [" + fieldName + "]", parser.getTokenLocation());
                }
                element.parse(parser, context);
            } else {
                if (token == null) {
                    throw new ElasticsearchParseException("failed to parse search source. end of query source reached but query is not complete.");
                } else {
                    throw new ElasticsearchParseException("failed to parse search source. expected field name but got [{}]", token);
                }
            }
        }
    } catch (Throwable e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        XContentLocation location = parser != null ? parser.getTokenLocation() : null;
        throw new SearchParseException(context, "failed to parse search source [" + sSource + "]", location, e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
 
Example 19
Source File: ByteFieldMapper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
    byte value;
    float boost = fieldType().boost();
    if (context.externalValueSet()) {
        Object externalValue = context.externalValue();
        if (externalValue == null) {
            if (fieldType().nullValue() == null) {
                return;
            }
            value = fieldType().nullValue();
        } else if (externalValue instanceof String) {
            String sExternalValue = (String) externalValue;
            if (sExternalValue.length() == 0) {
                if (fieldType().nullValue() == null) {
                    return;
                }
                value = fieldType().nullValue();
            } else {
                value = Byte.parseByte(sExternalValue);
            }
        } else {
            value = ((Number) externalValue).byteValue();
        }
        if (context.includeInAll(includeInAll, this)) {
            context.allEntries().addText(fieldType().names().fullName(), Byte.toString(value), boost);
        }
    } else {
        XContentParser parser = context.parser();
        if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
                (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
            if (fieldType().nullValue() == null) {
                return;
            }
            value = fieldType().nullValue();
            if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
                context.allEntries().addText(fieldType().names().fullName(), fieldType().nullValueAsString(), boost);
            }
        } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
            XContentParser.Token token;
            String currentFieldName = null;
            Byte objValue = fieldType().nullValue();
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else {
                    if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) {
                        if (parser.currentToken() != XContentParser.Token.VALUE_NULL) {
                            objValue = (byte) parser.shortValue(coerce.value());
                        }
                    } else if ("boost".equals(currentFieldName) || "_boost".equals(currentFieldName)) {
                        boost = parser.floatValue();
                    } else {
                        throw new IllegalArgumentException("unknown property [" + currentFieldName + "]");
                    }
                }
            }
            if (objValue == null) {
                // no value
                return;
            }
            value = objValue;
        } else {
            value = (byte) parser.shortValue(coerce.value());
            if (context.includeInAll(includeInAll, this)) {
                context.allEntries().addText(fieldType().names().fullName(), parser.text(), boost);
            }
        }
    }
    if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) {
        CustomByteNumericField field = new CustomByteNumericField(value, fieldType());
        field.setBoost(boost);
        fields.add(field);
    }
    if (fieldType().hasDocValues()) {
        addDocValue(context, fields, value);
    }
}
 
Example 20
Source File: SamplerParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    XContentParser.Token token;
    String currentFieldName = null;
    String executionHint = null;
    int shardSize = DEFAULT_SHARD_SAMPLE_SIZE;
    int maxDocsPerValue = MAX_DOCS_PER_VALUE_DEFAULT;
    ValuesSourceParser vsParser = null;
    boolean diversityChoiceMade = false;

    vsParser = ValuesSourceParser.any(aggregationName, InternalSampler.TYPE, context).scriptable(true).formattable(false).build();

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
            if (context.parseFieldMatcher().match(currentFieldName, SHARD_SIZE_FIELD)) {
                shardSize = parser.intValue();
            } else if (context.parseFieldMatcher().match(currentFieldName, MAX_DOCS_PER_VALUE_FIELD)) {
                diversityChoiceMade = true;
                maxDocsPerValue = parser.intValue();
            } else {
                throw new SearchParseException(context, "Unsupported property \"" + currentFieldName + "\" for aggregation \""
                        + aggregationName, parser.getTokenLocation());
            }
        } else if (!vsParser.token(currentFieldName, token, parser)) {
            if (context.parseFieldMatcher().match(currentFieldName, EXECUTION_HINT_FIELD)) {
                executionHint = parser.text();
            } else {
                throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                        parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unsupported property \"" + currentFieldName + "\" for aggregation \""
                    + aggregationName, parser.getTokenLocation());
        }
    }

    ValuesSourceConfig vsConfig = vsParser.config();
    if (vsConfig.valid()) {
        return new SamplerAggregator.DiversifiedFactory(aggregationName, shardSize, executionHint, vsConfig, maxDocsPerValue);
    } else {
        if (diversityChoiceMade) {
            throw new SearchParseException(context, "Sampler aggregation has " + MAX_DOCS_PER_VALUE_FIELD.getPreferredName()
                    + " setting but no \"field\" or \"script\" setting to provide values for aggregation \"" + aggregationName + "\"",
                    parser.getTokenLocation());

        }
        return new SamplerAggregator.Factory(aggregationName, shardSize);
    }
}