org.elasticsearch.common.xcontent.XContentParser.Token Java Examples
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: CategoryContextMapping.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public FieldQuery parseQuery(String name, XContentParser parser) throws IOException, ElasticsearchParseException { Iterable<? extends CharSequence> values; Token token = parser.currentToken(); if (token == Token.START_ARRAY) { ArrayList<String> list = new ArrayList<>(); while ((token = parser.nextToken()) != Token.END_ARRAY) { list.add(parser.text()); } values = list; } else if (token == Token.VALUE_NULL) { values = defaultValues; } else { values = Collections.singleton(parser.text()); } return new FieldQuery(name, values); }
Example #2
Source File: SpanMultiTermQueryParser.java From Elasticsearch with Apache License 2.0 | 6 votes |
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); Token token = parser.nextToken(); if (!MATCH_NAME.equals(parser.currentName()) || token != XContentParser.Token.FIELD_NAME) { throw new QueryParsingException(parseContext, "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause"); } token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { throw new QueryParsingException(parseContext, "spanMultiTerm must have [" + MATCH_NAME + "] multi term query clause"); } Query subQuery = parseContext.parseInnerQuery(); if (!(subQuery instanceof MultiTermQuery)) { throw new QueryParsingException(parseContext, "spanMultiTerm [" + MATCH_NAME + "] must be of type multi term query"); } parser.nextToken(); return new SpanMultiTermQueryWrapper<>((MultiTermQuery) subQuery); }
Example #3
Source File: JobEvent.java From elasticsearch-gatherer with Apache License 2.0 | 6 votes |
public JobEvent fromXContent(XContentParser parser) throws IOException { //DateMathParser dateParser = new DateMathParser(Joda.forPattern("dateOptionalTime"), TimeUnit.MILLISECONDS); Long startTimestamp = null; String currentFieldName = null; Token token; while ((token = parser.nextToken()) != END_OBJECT) { if (token == FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue() || token == VALUE_NULL) { if ("started".equals(currentFieldName)) { startTimestamp = Long.parseLong(parser.text()); } } else if (token == START_ARRAY) { List<String> values = newArrayList(); while ((parser.nextToken()) != END_ARRAY) { values.add(parser.text()); } } } return new JobEvent().timestamp(startTimestamp); }
Example #4
Source File: ESTestCase.java From crate with Apache License 2.0 | 6 votes |
/** * Randomly shuffles the fields inside objects parsed using the {@link XContentParser} passed in. * Recursively goes through inner objects and also shuffles them. Exceptions for this * recursive shuffling behavior can be made by passing in the names of fields which * internally should stay untouched. */ public static XContentBuilder shuffleXContent(XContentParser parser, boolean prettyPrint, String... exceptFieldNames) throws IOException { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType()); if (prettyPrint) { xContentBuilder.prettyPrint(); } Token token = parser.currentToken() == null ? parser.nextToken() : parser.currentToken(); if (token == Token.START_ARRAY) { List<Object> shuffledList = shuffleList(parser.listOrderedMap(), new HashSet<>(Arrays.asList(exceptFieldNames))); return xContentBuilder.value(shuffledList); } //we need a sorted map for reproducibility, as we are going to shuffle its keys and write XContent back Map<String, Object> shuffledMap = shuffleMap((LinkedHashMap<String, Object>)parser.mapOrdered(), new HashSet<>(Arrays.asList(exceptFieldNames))); return xContentBuilder.map(shuffledMap); }
Example #5
Source File: GeolocationContextMapping.java From Elasticsearch with Apache License 2.0 | 5 votes |
protected static Collection<String> parseSinglePointOrList(XContentParser parser) throws IOException { Token token = parser.currentToken(); if(token == Token.START_ARRAY) { token = parser.nextToken(); // Test if value is a single point in <code>[lon, lat]</code> format if(token == Token.VALUE_NUMBER) { double lon = parser.doubleValue(); if(parser.nextToken() == Token.VALUE_NUMBER) { double lat = parser.doubleValue(); if(parser.nextToken() == Token.END_ARRAY) { return Collections.singleton(GeoHashUtils.stringEncode(lon, lat)); } else { throw new ElasticsearchParseException("only two values expected"); } } else { throw new ElasticsearchParseException("latitue must be a numeric value"); } } else { // otherwise it's a list of locations ArrayList<String> result = new ArrayList<>(); while (token != Token.END_ARRAY) { result.add(GeoUtils.parseGeoPoint(parser).geohash()); token = parser.nextToken(); //infinite loop without this line } return result; } } else { // or a single location return Collections.singleton(GeoUtils.parseGeoPoint(parser).geohash()); } }
Example #6
Source File: QueryRescorer.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public RescoreSearchContext parse(XContentParser parser, SearchContext context) throws IOException { Token token; String fieldName = null; QueryRescoreContext rescoreContext = new QueryRescoreContext(this); while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); if ("rescore_query".equals(fieldName)) { ParsedQuery parsedQuery = context.queryParserService().parse(parser); rescoreContext.setParsedQuery(parsedQuery); } } else if (token.isValue()) { if ("query_weight".equals(fieldName)) { rescoreContext.setQueryWeight(parser.floatValue()); } else if ("rescore_query_weight".equals(fieldName)) { rescoreContext.setRescoreQueryWeight(parser.floatValue()); } else if ("score_mode".equals(fieldName)) { String sScoreMode = parser.text(); if ("avg".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Avg); } else if ("max".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Max); } else if ("min".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Min); } else if ("total".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Total); } else if ("multiply".equals(sScoreMode)) { rescoreContext.setScoreMode(ScoreMode.Multiply); } else { throw new IllegalArgumentException("[rescore] illegal score_mode [" + sScoreMode + "]"); } } else { throw new IllegalArgumentException("rescore doesn't support [" + fieldName + "]"); } } } return rescoreContext; }
Example #7
Source File: XContentParserUtils.java From crate with Apache License 2.0 | 5 votes |
/** * 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)); } }
Example #8
Source File: XContentParserUtils.java From crate with Apache License 2.0 | 5 votes |
/** * Makes sure that provided token is of the expected type * * @throws ParsingException if the token is not equal to the expected type */ public static void ensureExpectedToken(Token expected, Token actual, Supplier<XContentLocation> location) { if (actual != expected) { String message = "Failed to parse object: expecting token of type [%s] but found [%s]"; throw new ParsingException(location.get(), String.format(Locale.ROOT, message, expected, actual)); } }
Example #9
Source File: BucketMetricsParser.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public final PipelineAggregatorFactory parse(String pipelineAggregatorName, XContentParser parser, SearchContext context) throws IOException { XContentParser.Token token; String currentFieldName = null; String[] bucketsPaths = null; String format = null; GapPolicy gapPolicy = GapPolicy.SKIP; Map<String, Object> leftover = new HashMap<>(5); 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 (context.parseFieldMatcher().match(currentFieldName, FORMAT)) { format = parser.text(); } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) { bucketsPaths = new String[] { parser.text() }; } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) { gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation()); } else { leftover.put(currentFieldName, parser.text()); } } else if (token == XContentParser.Token.START_ARRAY) { if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) { List<String> paths = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { String path = parser.text(); paths.add(path); } bucketsPaths = paths.toArray(new String[paths.size()]); } else { leftover.put(currentFieldName, parser.list()); } } else { leftover.put(currentFieldName, parser.objectText()); } } if (bucketsPaths == null) { throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for aggregation [" + pipelineAggregatorName + "]", parser.getTokenLocation()); } ValueFormatter formatter = null; if (format != null) { formatter = ValueFormat.Patternable.Number.format(format).formatter(); } else { formatter = ValueFormatter.RAW; } PipelineAggregatorFactory factory = null; try { factory = buildFactory(pipelineAggregatorName, bucketsPaths, gapPolicy, formatter, leftover); } catch (ParseException exception) { throw new SearchParseException(context, "Could not parse settings for aggregation [" + pipelineAggregatorName + "].", null, exception); } if (leftover.size() > 0) { throw new SearchParseException(context, "Unexpected tokens " + leftover.keySet() + " in [" + pipelineAggregatorName + "].", null); } assert(factory != null); return factory; }
Example #10
Source File: GeohashCellQuery.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); String fieldName = null; String geohash = null; int levels = -1; boolean neighbors = false; XContentParser.Token token; if ((token = parser.currentToken()) != Token.START_OBJECT) { throw new ElasticsearchParseException("failed to parse [{}] query. expected an object but found [{}] instead", NAME, token); } while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == Token.FIELD_NAME) { String field = parser.currentName(); if (parseContext.isDeprecatedSetting(field)) { // skip } else if (PRECISION.equals(field)) { token = parser.nextToken(); if(token == Token.VALUE_NUMBER) { levels = parser.intValue(); } else if(token == Token.VALUE_STRING) { double meters = DistanceUnit.parse(parser.text(), DistanceUnit.DEFAULT, DistanceUnit.METERS); levels = GeoUtils.geoHashLevelsForPrecision(meters); } } else if (NEIGHBORS.equals(field)) { parser.nextToken(); neighbors = parser.booleanValue(); } else { fieldName = field; token = parser.nextToken(); if(token == Token.VALUE_STRING) { // A string indicates either a gehash or a lat/lon string String location = parser.text(); if(location.indexOf(",")>0) { geohash = GeoUtils.parseGeoPoint(parser).geohash(); } else { geohash = location; } } else { geohash = GeoUtils.parseGeoPoint(parser).geohash(); } } } else { throw new ElasticsearchParseException("failed to parse [{}] query. unexpected token [{}]", NAME, token); } } if (geohash == null) { throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing geohash value", NAME); } MappedFieldType fieldType = parseContext.fieldMapper(fieldName); if (fieldType == null) { throw new QueryParsingException(parseContext, "failed to parse [{}] query. missing [{}] field [{}]", NAME, BaseGeoPointFieldMapper.CONTENT_TYPE, fieldName); } if (!(fieldType instanceof BaseGeoPointFieldMapper.GeoPointFieldType)) { throw new QueryParsingException(parseContext, "failed to parse [{}] query. field [{}] is not a geo_point field", NAME, fieldName); } BaseGeoPointFieldMapper.GeoPointFieldType geoFieldType = ((BaseGeoPointFieldMapper.GeoPointFieldType) fieldType); if (!geoFieldType.isGeoHashPrefixEnabled()) { throw new QueryParsingException(parseContext, "failed to parse [{}] query. [geohash_prefix] is not enabled for field [{}]", NAME, fieldName); } if(levels > 0) { int len = Math.min(levels, geohash.length()); geohash = geohash.substring(0, len); } Query filter; if (neighbors) { filter = create(parseContext, geoFieldType, geohash, GeoHashUtils.addNeighbors(geohash, new ArrayList<CharSequence>(8))); } else { filter = create(parseContext, geoFieldType, geohash, null); } return filter; }
Example #11
Source File: GeoUtils.java From Elasticsearch with Apache License 2.0 | 4 votes |
/** * Parse a {@link GeoPoint} with a {@link XContentParser}. A geopoint has one of the following forms: * * <ul> * <li>Object: <pre>{"lat": <i><latitude></i>, "lon": <i><longitude></i>}</pre></li> * <li>String: <pre>"<i><latitude></i>,<i><longitude></i>"</pre></li> * <li>Geohash: <pre>"<i><geohash></i>"</pre></li> * <li>Array: <pre>[<i><longitude></i>,<i><latitude></i>]</pre></li> * </ul> * * @param parser {@link XContentParser} to parse the value from * @param point A {@link GeoPoint} that will be reset by the values parsed * @return new {@link GeoPoint} parsed from the parse */ public static GeoPoint parseGeoPoint(XContentParser parser, GeoPoint point) throws IOException, ElasticsearchParseException { double lat = Double.NaN; double lon = Double.NaN; String geohash = null; if(parser.currentToken() == Token.START_OBJECT) { while(parser.nextToken() != Token.END_OBJECT) { if(parser.currentToken() == Token.FIELD_NAME) { String field = parser.currentName(); if(LATITUDE.equals(field)) { parser.nextToken(); switch (parser.currentToken()) { case VALUE_NUMBER: case VALUE_STRING: lat = parser.doubleValue(true); break; default: throw new ElasticsearchParseException("latitude must be a number"); } } else if (LONGITUDE.equals(field)) { parser.nextToken(); switch (parser.currentToken()) { case VALUE_NUMBER: case VALUE_STRING: lon = parser.doubleValue(true); break; default: throw new ElasticsearchParseException("longitude must be a number"); } } else if (GEOHASH.equals(field)) { if(parser.nextToken() == Token.VALUE_STRING) { geohash = parser.text(); } else { throw new ElasticsearchParseException("geohash must be a string"); } } else { throw new ElasticsearchParseException("field must be either [{}], [{}] or [{}]", LATITUDE, LONGITUDE, GEOHASH); } } else { throw new ElasticsearchParseException("token [{}] not allowed", parser.currentToken()); } } if (geohash != null) { if(!Double.isNaN(lat) || !Double.isNaN(lon)) { throw new ElasticsearchParseException("field must be either lat/lon or geohash"); } else { return point.resetFromGeoHash(geohash); } } else if (Double.isNaN(lat)) { throw new ElasticsearchParseException("field [{}] missing", LATITUDE); } else if (Double.isNaN(lon)) { throw new ElasticsearchParseException("field [{}] missing", LONGITUDE); } else { return point.reset(lat, lon); } } else if(parser.currentToken() == Token.START_ARRAY) { int element = 0; while(parser.nextToken() != Token.END_ARRAY) { if(parser.currentToken() == Token.VALUE_NUMBER) { element++; if(element == 1) { lon = parser.doubleValue(); } else if(element == 2) { lat = parser.doubleValue(); } else { throw new ElasticsearchParseException("only two values allowed"); } } else { throw new ElasticsearchParseException("numeric value expected"); } } return point.reset(lat, lon); } else if(parser.currentToken() == Token.VALUE_STRING) { String data = parser.text(); return parseGeoPoint(data, point); } else { throw new ElasticsearchParseException("geo_point expected"); } }
Example #12
Source File: NumberFieldMapper.java From crate with Apache License 2.0 | 4 votes |
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { XContentParser parser = context.parser(); Object value; Number numericValue = null; if (context.externalValueSet()) { value = context.externalValue(); } else if (parser.currentToken() == Token.VALUE_NULL) { value = null; } else if (coerce.value() && parser.currentToken() == Token.VALUE_STRING && parser.textLength() == 0) { value = null; } else { try { numericValue = fieldType().type.parse(parser, coerce.value()); } catch (IllegalArgumentException e) { if (ignoreMalformed.value()) { context.addIgnoredField(fieldType.name()); return; } else { throw e; } } value = numericValue; } if (value == null) { value = fieldType().nullValue(); } if (value == null) { return; } if (numericValue == null) { numericValue = fieldType().type.parse(value, coerce.value()); } boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; boolean docValued = fieldType().hasDocValues(); boolean stored = fieldType().stored(); fields.addAll(fieldType().type.createFields(fieldType().name(), numericValue, indexed, docValued, stored)); if (docValued == false && (stored || indexed)) { createFieldNamesField(context, fields); } }
Example #13
Source File: XContentParserUtils.java From crate with Apache License 2.0 | 4 votes |
/** * @throws ParsingException with a "unknown token found" reason */ public static void throwUnknownToken(Token token, XContentLocation location) { String message = "Failed to parse object: unexpected token [%s] found"; throw new ParsingException(location, String.format(Locale.ROOT, message, token)); }