IndexBoost(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); if (token == XContentParser.Token.FIELD_NAME) { index = parser.currentName(); } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.FIELD_NAME + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } token = parser.nextToken(); if (token == XContentParser.Token.VALUE_NUMBER) { boost = parser.floatValue(); } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_NUMBER + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.END_OBJECT + "] in [" + INDICES_BOOST_FIELD + "] but found [" + token + "]", parser.getTokenLocation()); } } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] in [" + parser.currentName() + "] but found [" + token + "]", parser.getTokenLocation()); } }
/** * Makes sure that current token is of type {@link Token#FIELD_NAME} and the field name is equal to the provided one * @throws ParsingException if the token is not of type {@link Token#FIELD_NAME} or is not equal to the given field name */ public static void ensureFieldName(XContentParser parser, Token token, String fieldName) throws IOException { ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation); String currentName = parser.currentName(); if (currentName.equals(fieldName) == false) { String message = "Failed to parse object: expecting field with name [%s] but found [%s]"; throw new ParsingException(parser.getTokenLocation(), String.format(Locale.ROOT, message, fieldName, currentName)); } }
public static MatchAllQueryBuilder fromXContent(XContentParser parser) { try { return PARSER.apply(parser, null); } catch (IllegalArgumentException e) { throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e); } }
public static BulkResponse fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation); List<BulkItemResponse> items = new ArrayList<>(); String currentFieldName = parser.currentName(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (TOOK.equals(currentFieldName)) { ingestTook = parser.longValue(); } else if (ERRORS.equals(currentFieldName) == false) { throwUnknownField(currentFieldName, parser.getTokenLocation()); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { items.add(BulkItemResponse.fromXContent(parser, items.size())); throwUnknownField(currentFieldName, parser.getTokenLocation()); throwUnknownToken(token, parser.getTokenLocation());
public static <VS extends ValuesSource, T> void declareCommon( AbstractObjectParser<? extends MultiValuesSourceAggregationBuilder<VS, ?>, T> objectParser, boolean formattable, ValueType targetValueType) { objectParser.declareField(MultiValuesSourceAggregationBuilder::valueType, p -> { ValueType valueType = ValueType.resolveForScript(p.text()); if (targetValueType != null && valueType.isNotA(targetValueType)) { throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" + valueType + "]. It can only work on value of type [" + targetValueType + "]"); } return valueType; }, ValueType.VALUE_TYPE, ObjectParser.ValueType.STRING); if (formattable) { objectParser.declareField(MultiValuesSourceAggregationBuilder::format, XContentParser::text, ParseField.CommonFields.FORMAT, ObjectParser.ValueType.STRING); } }
static List<Object> parseValues(XContentParser parser) throws IOException { List<Object> values = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { Object value = maybeConvertToBytesRef(parser.objectBytes()); if (value == null) { throw new ParsingException(parser.getTokenLocation(), "No value specified for terms query"); } values.add(value); } return values; }
public static WrapperQueryBuilder fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed"); } String fieldName = parser.currentName(); if (! QUERY_FIELD.match(fieldName, parser.getDeprecationHandler())) { throw new ParsingException(parser.getTokenLocation(), "[wrapper] query malformed, expected `query` but was " + fieldName); } parser.nextToken(); byte[] source = parser.binaryValue(); parser.nextToken(); if (source == null) { throw new ParsingException(parser.getTokenLocation(), "wrapper query has no [query] specified"); } return new WrapperQueryBuilder(source); }
public static SpanGapQueryBuilder fromXContent(XContentParser parser) throws IOException { String fieldName = null; int width = 0; String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; } else if (token.isValue()) { width = parser.intValue(); } } SpanGapQueryBuilder result = new SpanGapQueryBuilder(fieldName, width); return result; }
ValueType valueType = ValueType.resolveForScript(p.text()); if (targetValueType != null && valueType.isNotA(targetValueType)) { throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" + valueType + "]. It can only work on value of type [" objectParser.declareField(ValuesSourceAggregationBuilder::timeZone, p -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { return DateTimeZone.forID(p.text()); } else { return DateTimeZone.forOffsetHours(p.intValue());
private AbstractDistanceScoreFunction parseVariable(String fieldName, XContentParser parser, QueryShardContext context, MultiValueMode mode) throws IOException { //the field must exist, else we cannot read the value for the doc later MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType == null) { throw new ParsingException(parser.getTokenLocation(), "unknown field [{}]", fieldName); } // dates and time and geo need special handling parser.nextToken(); if (fieldType instanceof DateFieldMapper.DateFieldType) { return parseDateVariable(parser, context, fieldType, mode); } else if (fieldType instanceof GeoPointFieldType) { return parseGeoVariable(parser, context, fieldType, mode); } else if (fieldType instanceof NumberFieldMapper.NumberFieldType) { return parseNumberVariable(parser, context, fieldType, mode); } else { throw new ParsingException(parser.getTokenLocation(), "field [{}] is of type [{}], but only numeric types are supported.", fieldName, fieldType); } }
public static IdsQueryBuilder fromXContent(XContentParser parser) { try { return PARSER.apply(parser, null); } catch (IllegalArgumentException e) { throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e); } }
public static NestedAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { String path = 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 (NestedAggregator.PATH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { path = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); } } if (path == null) { // "field" doesn't exist, so we fall back to the context of the ancestors throw new ParsingException(parser.getTokenLocation(), "Missing [path] field for nested aggregation [" + aggregationName + "]"); } return new NestedAggregationBuilder(aggregationName, path); }
static <VB extends CompositeValuesSourceBuilder<VB>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> objectParser, ValueType targetValueType) { objectParser.declareField(VB::field, XContentParser::text, new ParseField("field"), ObjectParser.ValueType.STRING); objectParser.declareField(VB::missing, XContentParser::objectText, new ParseField("missing"), ObjectParser.ValueType.VALUE); objectParser.declareBoolean(VB::missingBucket, new ParseField("missing_bucket")); objectParser.declareField(VB::valueType, p -> { ValueType valueType = ValueType.resolveForScript(p.text()); if (targetValueType != null && valueType.isNotA(targetValueType)) { throw new ParsingException(p.getTokenLocation(), "Aggregation [" + objectParser.getName() + "] was configured with an incompatible value type [" + valueType + "]. It can only work on value of type [" + targetValueType + "]"); } return valueType; }, new ParseField("value_type"), ObjectParser.ValueType.STRING); objectParser.declareField(VB::script, (parser, context) -> Script.parse(parser), Script.SCRIPT_PARSE_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING); objectParser.declareField(VB::order, XContentParser::text, new ParseField("order"), ObjectParser.ValueType.STRING); }
private void parseToken(String aggregationName, XContentParser parser, String currentFieldName, XContentParser.Token currentToken, Map<String, Object> params) throws IOException { if (token(parser, currentFieldName, currentToken, params) == false) { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + currentToken + " [" + currentFieldName + "] in [" + aggregationName + "]"); } } }
try { QueryBuilder queryBuilder = null; XContentParser.Token first = parser.nextToken(); if (first == null) { return null; } else if (first != XContentParser.Token.START_OBJECT) { throw new ParsingException( parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + first + "]", parser.getTokenLocation() ); for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { if (token == XContentParser.Token.FIELD_NAME) { String fieldName = parser.currentName(); if ("query".equals(fieldName)) { queryBuilder = parseInnerQueryBuilder(parser); } else { throw new ParsingException(parser.getTokenLocation(), "request does not support [" + parser.currentName() + "]"); throw e; } catch (Exception e) { throw new ParsingException(parser == null ? null : parser.getTokenLocation(), "Failed to parse", e);
protected static QueryBuilder parseNestedFilter(XContentParser parser) { try { return parseInnerQueryBuilder(parser); } catch (Exception e) { throw new ParsingException(parser.getTokenLocation(), "Expected " + NESTED_FILTER_FIELD.getPreferredName() + " element.", e); } }
public static ReverseNestedAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { String path = 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 ("path".equals(currentFieldName)) { path = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); } } ReverseNestedAggregationBuilder factory = new ReverseNestedAggregationBuilder( aggregationName); if (path != null) { factory.path(path); } return factory; }
/** * This method can be used to parse a {@link GetResponse} object when it has been printed out * as a xcontent using the {@link #toXContent(XContentBuilder, Params)} method. * <p> * For forward compatibility reason this method might not fail if it tries to parse a field it * doesn't know. But before returning the result it will check that enough information were * parsed to return a valid {@link GetResponse} instance and throws a {@link ParsingException} * otherwise. This is the case when we get a 404 back, which can be parsed as a normal * {@link GetResponse} with found set to false, or as an elasticsearch exception. The caller * of this method needs a way to figure out whether we got back a valid get response, which * can be done by catching ParsingException. * * @param parser {@link XContentParser} to parse the response from * @return a {@link GetResponse} * @throws IOException is an I/O exception occurs during the parsing */ public static GetResponse fromXContent(XContentParser parser) throws IOException { GetResult getResult = GetResult.fromXContent(parser); // At this stage we ensure that we parsed enough information to return // a valid GetResponse instance. If it's not the case, we throw an // exception so that callers know it and can handle it correctly. if (getResult.getIndex() == null && getResult.getType() == null && getResult.getId() == null) { throw new ParsingException(parser.getTokenLocation(), String.format(Locale.ROOT, "Missing required fields [%s,%s,%s]", GetResult._INDEX, GetResult._TYPE, GetResult._ID)); } return new GetResponse(getResult); }
public static StoredFieldsContext fromXContent(String fieldName, XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.VALUE_STRING) { return fromList(Collections.singletonList(parser.text())); } else if (token == XContentParser.Token.START_ARRAY) { ArrayList<String> list = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { list.add(parser.text()); } return fromList(list); } else { throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.VALUE_STRING + "] or [" + XContentParser.Token.START_ARRAY + "] in [" + fieldName + "] but found [" + token + "]", parser.getTokenLocation()); } }
public static ScriptScoreFunctionBuilder fromXContent(XContentParser parser) throws IOException, ParsingException { Script script = 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 (Script.SCRIPT_PARSE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { script = Script.parse(parser); } else { throw new ParsingException(parser.getTokenLocation(), NAME + " query does not support [" + currentFieldName + "]"); } } } if (script == null) { throw new ParsingException(parser.getTokenLocation(), NAME + " requires 'script' field"); } return new ScriptScoreFunctionBuilder(script); } }