Refine search
public static ContextParser<Void, TaskId> parser() { return (p, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { return new TaskId(p.text()); } throw new ElasticsearchParseException("Expected a string but found [{}] instead", p.currentToken()); }; }
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()); } } }
private static List<AliasMetaData> parseAliases(XContentParser parser) throws IOException { List<AliasMetaData> indexAliases = new ArrayList<>(); // We start at START_OBJECT since parseIndexEntry ensures that while (parser.nextToken() != Token.END_OBJECT) { ensureExpectedToken(Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); indexAliases.add(AliasMetaData.Builder.fromXContent(parser)); } return indexAliases; }
String mask = null; if (parser.currentToken() != Token.START_OBJECT) { throw new ParsingException(parser.getTokenLocation(), "[ranges] must contain objects, but hit a " + parser.currentToken()); while (parser.nextToken() != Token.END_OBJECT) { if (parser.currentToken() == Token.FIELD_NAME) { continue; key = parser.text(); } else if (RangeAggregator.Range.FROM_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) { from = parser.textOrNull(); to = parser.textOrNull(); } else if (MASK_FIELD.match(parser.currentName(), parser.getDeprecationHandler())) { mask = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected ip range parameter: [" + parser.currentName() + "]");
private static Explanation parseExplanation(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation); XContentParser.Token token; Float value = null; String description = null; List<Explanation> details = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation); String currentFieldName = parser.currentName(); token = parser.nextToken(); if (Fields.VALUE.equals(currentFieldName)) { value = parser.floatValue(); } else if (Fields.DETAILS.equals(currentFieldName)) { ensureExpectedToken(XContentParser.Token.START_ARRAY, token, parser::getTokenLocation); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { details.add(parseExplanation(parser));
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()); } } }
public static MaxDocsCondition fromXContent(XContentParser parser) throws IOException { if (parser.nextToken() == XContentParser.Token.VALUE_NUMBER) { return new MaxDocsCondition(parser.longValue()); } else { throw new IllegalArgumentException("invalid token: " + parser.currentToken()); } } }
private static void parseLikeField(XContentParser parser, List<String> texts, List<Item> items) throws IOException { if (parser.currentToken().isValue()) { texts.add(parser.text()); } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { items.add(Item.parse(parser, new Item())); } else { throw new IllegalArgumentException("Content of 'like' parameter should either be a string or an object"); } }
private static Set<BytesRef> parseArrayToSet(XContentParser parser) throws IOException { final Set<BytesRef> set = new HashSet<>(); if (parser.currentToken() != XContentParser.Token.START_ARRAY) { throw new ElasticsearchParseException("Missing start of array in include/exclude clause"); } while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (!parser.currentToken().isValue()) { throw new ElasticsearchParseException("Array elements in include/exclude clauses should be string values"); } set.add(new BytesRef(parser.text())); } return set; }
public static Status fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; if (parser.currentToken() == Token.START_OBJECT) { token = parser.nextToken(); } else { token = parser.nextToken(); } ensureExpectedToken(Token.START_OBJECT, token, parser::getTokenLocation); token = parser.nextToken(); ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation); return innerFromXContent(parser); }
public static IncludeExclude parseExclude(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.VALUE_STRING) { return new IncludeExclude(null, parser.text()); } else if (token == XContentParser.Token.START_ARRAY) { return new IncludeExclude(null, new TreeSet<>(parseArrayToSet(parser))); } else { throw new IllegalArgumentException("Unrecognized token for an exclude [" + token + "]"); } }
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; }
public static AggregationProfileShardResult fromXContent(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); ensureExpectedToken(XContentParser.Token.START_ARRAY, token, parser::getTokenLocation); List<ProfileResult> aggProfileResults = new ArrayList<>(); while((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { aggProfileResults.add(ProfileResult.fromXContent(parser)); } return new AggregationProfileShardResult(aggProfileResults); } }
/** * Parse a {@link FieldAndFormat} from some {@link XContent}. */ public static FieldAndFormat fromXContent(XContentParser parser) throws IOException { Token token = parser.currentToken(); if (token.isValue()) { return new FieldAndFormat(parser.text(), null); } else { return PARSER.apply(parser, null); } }
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); } }
/** * Used for serialization testing, since pipeline aggs serialize themselves as a named object but are parsed * as a regular object with the name passed in. */ static MovFnPipelineAggregationBuilder parse(XContentParser parser) throws IOException { parser.nextToken(); if (parser.currentToken().equals(XContentParser.Token.START_OBJECT)) { parser.nextToken(); if (parser.currentToken().equals(XContentParser.Token.FIELD_NAME)) { String aggName = parser.currentName(); parser.nextToken(); // "moving_fn" parser.nextToken(); // start_object return PARSER.apply(aggName).apply(parser, null); } } throw new IllegalStateException("Expected aggregation name but none found"); }
/** * Since stored scripts can accept templates rather than just scripts, they must also be able * to handle template parsing, hence the need for custom parsing source. Templates can * consist of either an {@link String} or a JSON object. If a JSON object is discovered * then the content type option must also be saved as a compiler option. */ private void setSource(XContentParser parser) { try { if (parser.currentToken() == Token.START_OBJECT) { // this is really for search templates, that need to be converted to json format XContentBuilder builder = XContentFactory.jsonBuilder(); source = Strings.toString(builder.copyCurrentStructure(parser)); options.put(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()); } else { source = parser.text(); } } catch (IOException exception) { throw new UncheckedIOException(exception); } }
private static void fromXContent(XContentParser parser, StringBuilder keyBuilder, Settings.Builder builder, boolean allowNullValues) throws IOException { final int length = keyBuilder.length(); while (parser.nextToken() != XContentParser.Token.END_OBJECT) { if (parser.currentToken() == XContentParser.Token.FIELD_NAME) { keyBuilder.setLength(length); keyBuilder.append(parser.currentName()); } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { keyBuilder.append('.'); fromXContent(parser, keyBuilder, builder, allowNullValues); } else if (parser.currentToken() == XContentParser.Token.START_ARRAY) { List<String> list = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { list.add(parser.text()); } else if (parser.currentToken() == XContentParser.Token.VALUE_NUMBER) { list.add(parser.text()); // just use the string representation here } else if (parser.currentToken() == XContentParser.Token.VALUE_BOOLEAN) { list.add(String.valueOf(parser.text())); } else { throw new IllegalStateException("only value lists are allowed in serialized settings"); validateValue(key, list, builder, parser, allowNullValues); builder.putList(key, list); } else if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { String key = keyBuilder.toString(); validateValue(key, null, builder, parser, allowNullValues); builder.putNull(key); } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING || parser.currentToken() == XContentParser.Token.VALUE_NUMBER) {
public static GetFieldMappingsResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); final Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>(); if (parser.nextToken() == XContentParser.Token.FIELD_NAME) { while (parser.currentToken() == XContentParser.Token.FIELD_NAME) { final String index = parser.currentName(); final Map<String, Map<String, FieldMappingMetaData>> typeMappings = PARSER.parse(parser, index); mappings.put(index, typeMappings); parser.nextToken(); } } return new GetFieldMappingsResponse(mappings); }
public static Fuzziness parse(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); switch (token) { case VALUE_STRING: case VALUE_NUMBER: final String fuzziness = parser.text(); if (AUTO.asString().equalsIgnoreCase(fuzziness)) { return AUTO; } else if (fuzziness.toUpperCase(Locale.ROOT).startsWith(AUTO.asString() + ":")) { return parseCustomAuto(fuzziness); } try { final int minimumSimilarity = Integer.parseInt(fuzziness); switch (minimumSimilarity) { case 0: return ZERO; case 1: return ONE; case 2: return TWO; default: return build(fuzziness); } } catch (NumberFormatException ex) { return build(fuzziness); } default: throw new IllegalArgumentException("Can't parse fuzziness on token: [" + token + "]"); } }