/** * Since inline scripts can accept code rather than just an id, they must also be able * to handle template parsing, hence the need for custom parsing code. 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 setInline(XContentParser parser) { try { if (type != null) { throwOnlyOneOfType(); } type = ScriptType.INLINE; if (parser.currentToken() == Token.START_OBJECT) { //this is really for search templates, that need to be converted to json format XContentBuilder builder = XContentFactory.jsonBuilder(); idOrCode = builder.copyCurrentStructure(parser).string(); options.put(CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()); } else { idOrCode = parser.text(); } } catch (IOException exception) { throw new UncheckedIOException(exception); } }
private static InternalOrder parseOrder(XContentParser parser, QueryParseContext context) throws IOException { InternalOrder order = null; 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) { String dir = parser.text(); boolean asc = "asc".equals(dir); if (!asc && !"desc".equals(dir)) { throw new ParsingException(parser.getTokenLocation(), "Unknown order direction: [" + dir + "]. Should be either [asc] or [desc]"); } order = resolveOrder(currentFieldName, asc); } } return order; }
public void testParseGeoPointGeohash() throws IOException { for (int i = 0; i < 100; i++) { int geoHashLength = randomIntBetween(1, GeoHashUtils.PRECISION); StringBuilder geohashBuilder = new StringBuilder(geoHashLength); for (int j = 0; j < geoHashLength; j++) { geohashBuilder.append(BASE_32[randomInt(BASE_32.length - 1)]); } XContentBuilder json = jsonBuilder().startObject().field("geohash", geohashBuilder.toString()).endObject(); XContentParser parser = createParser(json); parser.nextToken(); GeoPoint point = GeoUtils.parseGeoPoint(parser); assertThat(point.lat(), allOf(lessThanOrEqualTo(90.0), greaterThanOrEqualTo(-90.0))); assertThat(point.lon(), allOf(lessThanOrEqualTo(180.0), greaterThanOrEqualTo(-180.0))); json = jsonBuilder().startObject().field("geohash", geohashBuilder.toString()).endObject(); parser = createParser(json); while (parser.currentToken() != Token.VALUE_STRING) { parser.nextToken(); } point = GeoUtils.parseGeoPoint(parser); assertThat(point.lat(), allOf(lessThanOrEqualTo(90.0), greaterThanOrEqualTo(-90.0))); assertThat(point.lon(), allOf(lessThanOrEqualTo(180.0), greaterThanOrEqualTo(-180.0))); } }
private String verifyNodeDecisionPrologue(XContentParser parser) throws IOException { assertEquals(Token.START_OBJECT, parser.nextToken()); parser.nextToken(); assertEquals("node_id", parser.currentName()); parser.nextToken(); assertNotNull(parser.text()); parser.nextToken(); assertEquals("node_name", parser.currentName()); parser.nextToken(); String nodeName = parser.text(); assertNotNull(nodeName); parser.nextToken(); assertEquals("transport_address", parser.currentName()); parser.nextToken(); assertNotNull(parser.text()); parser.nextToken(); assertEquals("node_decision", parser.currentName()); parser.nextToken(); return nodeName; }
private boolean verifyDeciders(XContentParser parser, AllocationDecision allocationDecision) throws IOException { assertEquals(Token.START_ARRAY, parser.nextToken()); boolean atLeastOneMatchingDecisionFound = false; while (parser.nextToken() != Token.END_ARRAY) { assertEquals(Token.START_OBJECT, parser.currentToken()); parser.nextToken(); assertEquals("decider", parser.currentName()); parser.nextToken(); assertNotNull(parser.text()); parser.nextToken(); assertEquals("decision", parser.currentName()); parser.nextToken(); String decisionText = parser.text(); if ((allocationDecision == AllocationDecision.NO && decisionText.equals("NO") || (allocationDecision == AllocationDecision.THROTTLED && decisionText.equals("THROTTLE")))) { atLeastOneMatchingDecisionFound = true; } assertNotNull(decisionText); parser.nextToken(); assertEquals("explanation", parser.currentName()); parser.nextToken(); assertNotNull(parser.text()); assertEquals(Token.END_OBJECT, parser.nextToken()); } return atLeastOneMatchingDecisionFound; }
@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); }
@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); }
public static Term parse(XContentParser parser, String name) { try { Term completion = new Term(name); Token token; String currentFieldName = null; while ((token = parser.nextToken()) != END_OBJECT) { if (token == FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if("text".equals(currentFieldName)) { completion.text = parser.text(); } else if("offset".equals(currentFieldName)) { completion.offset = parser.intValue(); } else if("length".equals(currentFieldName)) { completion.length = parser.intValue(); } } else if (token == START_ARRAY && "options".equals(currentFieldName)) { completion.options = parseOptions(parser); } } return completion; } catch (IOException e) { throw new RuntimeException(e); } }
protected static Option parseOption(XContentParser parser) throws IOException { Token token; String currentFieldName = null; Option option = new Option(); while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("text".equals(currentFieldName)) { option.text = parser.text(); } else if ("freq".equals(currentFieldName)) { option.freq = parser.longValue(); } else if ("score".equals(currentFieldName)) { option.score = parser.floatValue(); } } } return option; }
public static Completion parse(XContentParser parser, String name) { try { Completion completion = new Completion(name); Token token; String currentFieldName = null; while ((token = parser.nextToken()) != END_OBJECT) { if (token == FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if("text".equals(currentFieldName)) { completion.text = parser.text(); } else if("offset".equals(currentFieldName)) { completion.offset = parser.intValue(); } else if("length".equals(currentFieldName)) { completion.length = parser.intValue(); } } else if (token == START_ARRAY && "options".equals(currentFieldName)) { completion.options = parseOptions(parser); } } return completion; } catch (IOException e) { throw new RuntimeException(e); } }
protected static Option parseOption(XContentParser parser) throws IOException { Token token; String currentFieldName = null; Option option = new Option(); while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("text".equals(currentFieldName)) { option.text = parser.text(); } else if ("score".equals(currentFieldName)) { option.score = parser.floatValue(); } } else if (token == Token.START_OBJECT && "payload".equals(currentFieldName)) { try (XContentBuilder docBuilder = XContentFactory.contentBuilder(XContentType.JSON)) { docBuilder.copyCurrentStructure(parser); option.payload = docBuilder.bytes().toUtf8(); } } } return option; }
public static Phrase parse(XContentParser parser, String name) { try { Phrase completion = new Phrase(name); Token token; String currentFieldName = null; while ((token = parser.nextToken()) != END_OBJECT) { if (token == FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if("text".equals(currentFieldName)) { completion.text = parser.text(); } else if("offset".equals(currentFieldName)) { completion.offset = parser.intValue(); } else if("length".equals(currentFieldName)) { completion.length = parser.intValue(); } } else if (token == START_ARRAY && "options".equals(currentFieldName)) { completion.options = parseOptions(parser); } } return completion; } catch (IOException e) { throw new RuntimeException(e); } }
protected static Option parseOption(XContentParser parser) throws IOException { Token token; String currentFieldName = null; Option option = new Option(); while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("text".equals(currentFieldName)) { option.text = parser.text(); } else if ("collate".equals(currentFieldName)) { option.collate = parser.booleanValue(); } else if ("score".equals(currentFieldName)) { option.score = parser.floatValue(); } else if ("highlighted".equals(currentFieldName)) { option.highlighted = parser.text(); } } } return option; }
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); }
/** * 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 code. 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 setCode(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(); code = builder.copyCurrentStructure(parser).string(); options.put(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()); } else { code = parser.text(); } } catch (IOException exception) { throw new UncheckedIOException(exception); } }
private static Range parseRange(XContentParser parser, QueryParseContext context) throws IOException { String fromAsStr = null; String toAsStr = null; double from = 0.0; double to = Double.POSITIVE_INFINITY; String key = null; String toOrFromOrKey = null; Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { toOrFromOrKey = parser.currentName(); } else if (token == XContentParser.Token.VALUE_NUMBER) { if (Range.FROM_FIELD.match(toOrFromOrKey)) { from = parser.doubleValue(); } else if (Range.TO_FIELD.match(toOrFromOrKey)) { to = parser.doubleValue(); } } else if (token == XContentParser.Token.VALUE_STRING) { if (Range.KEY_FIELD.match(toOrFromOrKey)) { key = parser.text(); } else if (Range.FROM_FIELD.match(toOrFromOrKey)) { fromAsStr = parser.text(); } else if (Range.TO_FIELD.match(toOrFromOrKey)) { toAsStr = parser.text(); } } } if (fromAsStr != null || toAsStr != null) { return new Range(key, Double.parseDouble(fromAsStr), Double.parseDouble(toAsStr)); } else { return new Range(key, from, to); } }
private static Range parseRange(XContentParser parser, QueryParseContext context) throws IOException { String key = null; String from = null; String to = null; 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; } if (RangeAggregator.Range.KEY_FIELD.match(parser.currentName())) { key = parser.text(); } else if (RangeAggregator.Range.FROM_FIELD.match(parser.currentName())) { from = parser.textOrNull(); } else if (RangeAggregator.Range.TO_FIELD.match(parser.currentName())) { to = parser.textOrNull(); } else if (MASK_FIELD.match(parser.currentName())) { mask = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected ip range parameter: [" + parser.currentName() + "]"); } } if (mask != null) { if (key == null) { key = mask; } return new Range(key, mask); } else { return new Range(key, from, to); } }
/** * Parses query contexts for this mapper */ public final List<InternalQueryContext> parseQueryContext(QueryParseContext context) throws IOException, ElasticsearchParseException { List<T> queryContexts = new ArrayList<>(); XContentParser parser = context.parser(); Token token = parser.nextToken(); if (token == Token.START_OBJECT || token == Token.VALUE_STRING) { queryContexts.add(fromXContent(context)); } else if (token == Token.START_ARRAY) { while (parser.nextToken() != Token.END_ARRAY) { queryContexts.add(fromXContent(context)); } } return toInternalQueryContexts(queryContexts); }
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)) { alpha = parser.doubleValue(); } } return new Laplace(alpha); }
public static LinearInterpolation fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; String fieldName = null; double trigramLambda = 0.0; double bigramLambda = 0.0; double unigramLambda = 0.0; while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } else if (token.isValue()) { if (TRIGRAM_FIELD.match(fieldName)) { trigramLambda = parser.doubleValue(); if (trigramLambda < 0) { throw new IllegalArgumentException("trigram_lambda must be positive"); } } else if (BIGRAM_FIELD.match(fieldName)) { bigramLambda = parser.doubleValue(); if (bigramLambda < 0) { throw new IllegalArgumentException("bigram_lambda must be positive"); } } else if (UNIGRAM_FIELD.match(fieldName)) { unigramLambda = parser.doubleValue(); if (unigramLambda < 0) { throw new IllegalArgumentException("unigram_lambda must be positive"); } } else { throw new IllegalArgumentException( "suggester[phrase][smoothing][linear] doesn't support field [" + fieldName + "]"); } } else { throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + fieldName + "]"); } } return new LinearInterpolation(trigramLambda, bigramLambda, unigramLambda); }
public static SmoothingModel fromXContent(XContentParser parser) throws IOException { XContentParser.Token token; String fieldName = null; double discount = DEFAULT_BACKOFF_DISCOUNT; while ((token = parser.nextToken()) != Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); } if (token.isValue() && DISCOUNT_FIELD.match(fieldName)) { discount = parser.doubleValue(); } } return new StupidBackoff(discount); }
/** * Makes sure that current token is of type {@link XContentParser.Token#FIELD_NAME} and the the field name is equal to the provided one * @throws ParsingException if the token is not of type {@link XContentParser.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)); } }
/** * 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)); } }
public void testParseGeoPoint() throws IOException { for (int i = 0; i < 100; i++) { double lat = randomDouble() * 180 - 90 + randomIntBetween(-1000, 1000) * 180; double lon = randomDouble() * 360 - 180 + randomIntBetween(-1000, 1000) * 360; XContentBuilder json = jsonBuilder().startObject().field("lat", lat).field("lon", lon).endObject(); XContentParser parser = createParser(json); parser.nextToken(); GeoPoint point = GeoUtils.parseGeoPoint(parser); assertThat(point, equalTo(new GeoPoint(lat, lon))); json = jsonBuilder().startObject().field("lat", String.valueOf(lat)).field("lon", String.valueOf(lon)).endObject(); parser = createParser(json); parser.nextToken(); point = GeoUtils.parseGeoPoint(parser); assertThat(point, equalTo(new GeoPoint(lat, lon))); json = jsonBuilder().startObject().startArray("foo").value(lon).value(lat).endArray().endObject(); parser = createParser(json); while (parser.currentToken() != Token.START_ARRAY) { parser.nextToken(); } point = GeoUtils.parseGeoPoint(parser); assertThat(point, equalTo(new GeoPoint(lat, lon))); json = jsonBuilder().startObject().field("foo", lat + "," + lon).endObject(); parser = createParser(json); while (parser.currentToken() != Token.VALUE_STRING) { parser.nextToken(); } point = GeoUtils.parseGeoPoint(parser); assertThat(point, equalTo(new GeoPoint(lat, lon))); } }
public void testParseGeoPointArrayTooManyValues() throws IOException { double lat = 0.0; double lon = 0.0; double elev = 0.0; XContentBuilder json = jsonBuilder().startObject().startArray("foo").value(lon).value(lat).value(elev).endArray().endObject(); XContentParser parser = createParser(json); while (parser.currentToken() != Token.START_ARRAY) { parser.nextToken(); } Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); assertThat(e.getMessage(), is("only two values allowed")); }
public void testParseGeoPointArrayWrongType() throws IOException { double lat = 0.0; boolean lon = false; XContentBuilder json = jsonBuilder().startObject().startArray("foo").value(lon).value(lat).endArray().endObject(); XContentParser parser = createParser(json); while (parser.currentToken() != Token.START_ARRAY) { parser.nextToken(); } Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); assertThat(e.getMessage(), is("numeric value expected")); }
public void testParseGeoPointInvalidType() throws IOException { XContentBuilder json = jsonBuilder().startObject().field("foo", 5).endObject(); XContentParser parser = createParser(json); while (parser.currentToken() != Token.VALUE_NUMBER) { parser.nextToken(); } Exception e = expectThrows(ElasticsearchParseException.class, () -> GeoUtils.parseGeoPoint(parser)); assertThat(e.getMessage(), is("geo_point expected")); }
public void testBinaryField() throws Exception { assertResult("{'binary':null}", () -> builder().startObject().field("binary", (byte[]) null).endObject()); final byte[] randomBytes = randomBytes(); BytesReference bytes = builder().startObject().field("binary", randomBytes).endObject().bytes(); XContentParser parser = createParser(xcontentType().xContent(), bytes); assertSame(parser.nextToken(), Token.START_OBJECT); assertSame(parser.nextToken(), Token.FIELD_NAME); assertEquals(parser.currentName(), "binary"); assertTrue(parser.nextToken().isValue()); assertArrayEquals(randomBytes, parser.binaryValue()); assertSame(parser.nextToken(), Token.END_OBJECT); assertNull(parser.nextToken()); }
public void testBinaryValue() throws Exception { assertResult("{'binary':null}", () -> builder().startObject().field("binary").value((byte[]) null).endObject()); final byte[] randomBytes = randomBytes(); BytesReference bytes = builder().startObject().field("binary").value(randomBytes).endObject().bytes(); XContentParser parser = createParser(xcontentType().xContent(), bytes); assertSame(parser.nextToken(), Token.START_OBJECT); assertSame(parser.nextToken(), Token.FIELD_NAME); assertEquals(parser.currentName(), "binary"); assertTrue(parser.nextToken().isValue()); assertArrayEquals(randomBytes, parser.binaryValue()); assertSame(parser.nextToken(), Token.END_OBJECT); assertNull(parser.nextToken()); }
private void verifyStaleShardCopyNodeDecisions(XContentParser parser, int numNodes, Set<String> foundStores) throws IOException { parser.nextToken(); assertEquals("node_allocation_decisions", parser.currentName()); assertEquals(Token.START_ARRAY, parser.nextToken()); for (int i = 0; i < numNodes; i++) { String nodeName = verifyNodeDecisionPrologue(parser); assertEquals(AllocationDecision.NO.toString(), parser.text()); parser.nextToken(); assertEquals("store", parser.currentName()); assertEquals(Token.START_OBJECT, parser.nextToken()); parser.nextToken(); if (foundStores.contains(nodeName)) { // shard data was found on the node, but it is stale assertEquals("in_sync", parser.currentName()); parser.nextToken(); assertFalse(parser.booleanValue()); parser.nextToken(); assertEquals("allocation_id", parser.currentName()); parser.nextToken(); assertNotNull(parser.text()); } else { // no shard data was found on the node assertEquals("found", parser.currentName()); parser.nextToken(); assertFalse(parser.booleanValue()); } assertEquals(Token.END_OBJECT, parser.nextToken()); parser.nextToken(); assertEquals(Token.END_OBJECT, parser.currentToken()); } assertEquals(Token.END_ARRAY, parser.nextToken()); }
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()); } }
@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; }
protected static List<Option> parseOptions(XContentParser parser) throws IOException { Token token; List<Option> result = new ArrayList<>(); while ((token = parser.nextToken()) != Token.END_ARRAY) { if (token == Token.START_OBJECT) { result.add(parseOption(parser)); } } return result; }
public static GeoPolygonQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { XContentParser parser = parseContext.parser(); String fieldName = null; List<GeoPoint> shell = null; Float boost = null; GeoValidationMethod validationMethod = null; String queryName = null; String currentFieldName = null; XContentParser.Token token; boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED; 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) { fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { if (POINTS_FIELD.match(currentFieldName)) { shell = new ArrayList<GeoPoint>(); while ((token = parser.nextToken()) != Token.END_ARRAY) { shell.add(GeoUtils.parseGeoPoint(parser)); } } else { throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]"); } } else { throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support token type [" + token.name() + "] under [" + currentFieldName + "]"); } } } else if (token.isValue()) { if ("_name".equals(currentFieldName)) { queryName = parser.text(); } else if ("boost".equals(currentFieldName)) { boost = parser.floatValue(); } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName)) { ignoreUnmapped = parser.booleanValue(); } else if (VALIDATION_METHOD.match(currentFieldName)) { validationMethod = GeoValidationMethod.fromString(parser.text()); } else { throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] query does not support [" + currentFieldName + "]"); } } else { throw new ParsingException(parser.getTokenLocation(), "[geo_polygon] unexpected token type [" + token.name() + "]"); } } GeoPolygonQueryBuilder builder = new GeoPolygonQueryBuilder(fieldName, shell); if (validationMethod != null) { // if GeoValidationMethod was explicitly set ignore deprecated coerce and ignoreMalformed settings builder.setValidationMethod(validationMethod); } if (queryName != null) { builder.queryName(queryName); } if (boost != null) { builder.boost(boost); } builder.ignoreUnmapped(ignoreUnmapped); return builder; }
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { final boolean includeInAll = context.includeInAll(this.includeInAll, this); 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()) { return; } else { throw e; } } if (includeInAll) { value = parser.textOrNull(); // preserve formatting } else { value = numericValue; } } if (value == null) { value = fieldType().nullValue(); } if (value == null) { return; } if (numericValue == null) { numericValue = fieldType().type.parse(value, coerce.value()); } if (includeInAll) { context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost()); } 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)); }
@Override protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException { final boolean includeInAll = context.includeInAll(this.includeInAll, this); 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 = NumberFieldMapper.NumberType.DOUBLE.parse(parser, coerce.value()); } catch (IllegalArgumentException e) { if (ignoreMalformed.value()) { return; } else { throw e; } } if (includeInAll) { value = parser.textOrNull(); // preserve formatting } else { value = numericValue; } } if (value == null) { value = fieldType().nullValue(); } if (value == null) { return; } if (numericValue == null) { numericValue = NumberFieldMapper.NumberType.DOUBLE.parse(value, false); } if (includeInAll) { context.allEntries().addText(fieldType().name(), value.toString(), fieldType().boost()); } double doubleValue = numericValue.doubleValue(); if (Double.isFinite(doubleValue) == false) { // since we encode to a long, we have no way to carry NaNs and infinities throw new IllegalArgumentException("[scaled_float] only supports finite values, but got [" + doubleValue + "]"); } long scaledValue = Math.round(doubleValue * fieldType().getScalingFactor()); boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; boolean docValued = fieldType().hasDocValues(); boolean stored = fieldType().stored(); fields.addAll(NumberFieldMapper.NumberType.LONG.createFields(fieldType().name(), scaledValue, indexed, docValued, stored)); }
/** * @throws ParsingException with a "unknown token found" reason */ public static void throwUnknownToken(XContentParser.Token token, XContentLocation location) { String message = "Failed to parse object: unexpected token [%s] found"; throw new ParsingException(location, String.format(Locale.ROOT, message, token)); }