private static String convertToYaml(String type, BytesReference bytes, boolean prettyPrint) throws IOException { try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, bytes.streamInput())) { parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { return null; } parser.nextToken(); XContentBuilder builder = XContentFactory.yamlBuilder(); if (prettyPrint) { builder.prettyPrint(); } builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML); return Strings.toString(builder); } }
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); }
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; }
private static Map<String, Set<String>> parseContexts(XContentParser parser) throws IOException { Map<String, Set<String>> contexts = new HashMap<>(); while((parser.nextToken()) != XContentParser.Token.END_OBJECT) { ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); String key = parser.currentName(); ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser::getTokenLocation); Set<String> values = new HashSet<>(); while((parser.nextToken()) != XContentParser.Token.END_ARRAY) { ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.currentToken(), parser::getTokenLocation); values.add(parser.text()); } contexts.put(key, values); } return contexts; }
@Override public SignificanceHeuristic parse(XContentParser parser) throws IOException, QueryShardException { String givenName = parser.currentName(); boolean backgroundIsSuperset = true; XContentParser.Token token = parser.nextToken(); while (!token.equals(XContentParser.Token.END_OBJECT)) { if (BACKGROUND_IS_SUPERSET.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); backgroundIsSuperset = parser.booleanValue(); } else { throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. unknown field [{}]", givenName, parser.currentName()); } token = parser.nextToken(); } return newHeuristic(true, backgroundIsSuperset); } };
private static List<String> parseableStringToList(String parsableString) { // fromXContent doesn't use named xcontent or deprecation. try (XContentParser xContentParser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parsableString)) { XContentParser.Token token = xContentParser.nextToken(); if (token != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("expected START_ARRAY but got " + token); } ArrayList<String> list = new ArrayList<>(); while ((token = xContentParser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token != XContentParser.Token.VALUE_STRING) { throw new IllegalArgumentException("expected VALUE_STRING but got " + token); } list.add(xContentParser.text()); } return list; } catch (IOException e) { throw new IllegalArgumentException("failed to parse array", e); } }
long time = -1; List<CollectorResult> children = new ArrayList<>(); while((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if (NAME.match(currentFieldName, parser.getDeprecationHandler())) { name = parser.text(); } else if (REASON.match(currentFieldName, parser.getDeprecationHandler())) { reason = parser.text(); } else if (TIME.match(currentFieldName, parser.getDeprecationHandler())) { parser.text(); } else if (TIME_NANOS.match(currentFieldName, parser.getDeprecationHandler())) { time = parser.longValue(); if (CHILDREN.match(currentFieldName, parser.getDeprecationHandler())) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { children.add(CollectorResult.fromXContent(parser));
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)) { took = parser.longValue(); 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 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); }
public static SnapshotId fromXContent(XContentParser parser) throws IOException { // the new format from 5.0 which contains the snapshot name and uuid if (parser.currentToken() == XContentParser.Token.START_OBJECT) { String name = null; String uuid = null; while (parser.nextToken() != XContentParser.Token.END_OBJECT) { String currentFieldName = parser.currentName(); parser.nextToken(); if (NAME.equals(currentFieldName)) { name = parser.text(); } else if (UUID.equals(currentFieldName)) { uuid = parser.text(); } } return new SnapshotId(name, uuid); } else { // the old format pre 5.0 that only contains the snapshot name, use the name as the uuid too final String name = parser.text(); return new SnapshotId(name, name); } }
parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { log.error("Cannot parse config for type {} because {}!={}", type, type, parser.currentName()); return null; parser.nextToken();
@Override public SignificanceHeuristic parse(XContentParser parser) throws IOException, QueryShardException { String givenName = parser.currentName(); boolean includeNegatives = false; boolean backgroundIsSuperset = true; XContentParser.Token token = parser.nextToken(); while (!token.equals(XContentParser.Token.END_OBJECT)) { if (INCLUDE_NEGATIVES_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); includeNegatives = parser.booleanValue(); } else if (BACKGROUND_IS_SUPERSET.match(parser.currentName(), parser.getDeprecationHandler())) { parser.nextToken(); backgroundIsSuperset = parser.booleanValue(); } else { throw new ElasticsearchParseException("failed to parse [{}] significance heuristic. unknown field [{}]", givenName, parser.currentName()); } token = parser.nextToken(); } return newHeuristic(includeNegatives, backgroundIsSuperset); }
public static MaxSizeCondition fromXContent(XContentParser parser) throws IOException { if (parser.nextToken() == XContentParser.Token.VALUE_STRING) { return new MaxSizeCondition(ByteSizeValue.parseBytesSizeValue(parser.text(), NAME)); } else { throw new IllegalArgumentException("invalid token: " + parser.currentToken()); } } }
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; }
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 HighlightField fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); String fieldName = parser.currentName(); Text[] fragments = null; XContentParser.Token token = parser.nextToken(); if (token == XContentParser.Token.START_ARRAY) { List<Text> values = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { values.add(new Text(parser.text())); } fragments = values.toArray(new Text[values.size()]); } else if (token == XContentParser.Token.VALUE_NULL) { fragments = null; } else { throw new ParsingException(parser.getTokenLocation(), "unexpected token type [" + token + "]"); } return new HighlightField(fieldName, fragments); }
public void fromXContent(XContentParser parser) throws IOException { scrollIds = null; if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new IllegalArgumentException("Malformed content, must start with an object"); } else { 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 ("scroll_id".equals(currentFieldName)){ if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token.isValue() == false) { throw new IllegalArgumentException("scroll_id array element should only contain scroll_id"); addScrollId(parser.text()); if (token.isValue() == false) { throw new IllegalArgumentException("scroll_id element should only contain scroll_id"); addScrollId(parser.text());
parser.nextToken(); parser.nextToken(); if(!id.equals((parser.currentName()))) { log.error("Cannot parse config for type {} because {}!={}", id, id, parser.currentName()); return null; parser.nextToken();
public static SmoothingModel fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; String fieldName = null; double alpha = DEFAULT_LAPLACE_ALPHA; while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } if (token.isValue() && ALPHA_FIELD.match(fieldName, parser.getDeprecationHandler())) { alpha = parser.doubleValue(); } } return new Laplace(alpha); }
public static MaxAgeCondition fromXContent(XContentParser parser) throws IOException { if (parser.nextToken() == XContentParser.Token.VALUE_STRING) { return new MaxAgeCondition(TimeValue.parseTimeValue(parser.text(), NAME)); } else { throw new IllegalArgumentException("invalid token: " + parser.currentToken()); } } }