use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.
the class CompletionFieldMapper method parse.
/**
* Parses and indexes inputs
*
* Parsing:
* Acceptable format:
* "STRING" - interpreted as field value (input)
* "ARRAY" - each element can be one of "OBJECT" (see below)
* "OBJECT" - { "input": STRING|ARRAY, "weight": STRING|INT, "contexts": ARRAY|OBJECT }
*
* Indexing:
* if context mappings are defined, delegates to {@link ContextMappings#addField(ParseContext.Document, String, String, int, Map)}
* else adds inputs as a {@link org.apache.lucene.search.suggest.document.SuggestField}
*/
@Override
public Mapper parse(ParseContext context) throws IOException {
// parse
XContentParser parser = context.parser();
Token token = parser.currentToken();
Map<String, CompletionInputMetaData> inputMap = new HashMap<>(1);
if (token == Token.VALUE_NULL) {
throw new MapperParsingException("completion field [" + fieldType().name() + "] does not support null values");
} else if (token == Token.START_ARRAY) {
while ((token = parser.nextToken()) != Token.END_ARRAY) {
parse(context, token, parser, inputMap);
}
} else {
parse(context, token, parser, inputMap);
}
// index
for (Map.Entry<String, CompletionInputMetaData> completionInput : inputMap.entrySet()) {
String input = completionInput.getKey();
// truncate input
if (input.length() > maxInputLength) {
int len = Math.min(maxInputLength, input.length());
if (Character.isHighSurrogate(input.charAt(len - 1))) {
assert input.length() >= len + 1 && Character.isLowSurrogate(input.charAt(len));
len += 1;
}
input = input.substring(0, len);
}
CompletionInputMetaData metaData = completionInput.getValue();
if (fieldType().hasContextMappings()) {
fieldType().getContextMappings().addField(context.doc(), fieldType().name(), input, metaData.weight, metaData.contexts);
} else {
context.doc().add(new SuggestField(fieldType().name(), input, metaData.weight));
}
}
multiFields.parse(this, context);
return null;
}
use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.
the class GeoDistanceSortBuilder method fromXContent.
/**
* Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link QueryParseContext} in
* {@link org.elasticsearch.common.xcontent.XContent} format.
*
* @param context the input parse context. The state on the parser contained in this context will be changed as a
* side effect of this method call
* @param elementName in some sort syntax variations the field name precedes the xContent object that specifies
* further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object,
* the field name can be passed in via this argument
*/
public static GeoDistanceSortBuilder fromXContent(QueryParseContext context, String elementName) throws IOException {
XContentParser parser = context.parser();
String fieldName = null;
List<GeoPoint> geoPoints = new ArrayList<>();
DistanceUnit unit = DistanceUnit.DEFAULT;
GeoDistance geoDistance = GeoDistance.ARC;
SortOrder order = SortOrder.ASC;
SortMode sortMode = null;
QueryBuilder nestedFilter = null;
String nestedPath = null;
GeoValidationMethod validation = null;
XContentParser.Token token;
String currentName = parser.currentName();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
parseGeoPoints(parser, geoPoints);
fieldName = currentName;
} else if (token == XContentParser.Token.START_OBJECT) {
if (NESTED_FILTER_FIELD.match(currentName)) {
nestedFilter = context.parseInnerQueryBuilder();
} else {
// the json in the format of -> field : { lat : 30, lon : 12 }
if (fieldName != null && fieldName.equals(currentName) == false) {
throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
}
fieldName = currentName;
GeoPoint point = new GeoPoint();
GeoUtils.parseGeoPoint(parser, point);
geoPoints.add(point);
}
} else if (token.isValue()) {
if (ORDER_FIELD.match(currentName)) {
order = SortOrder.fromString(parser.text());
} else if (UNIT_FIELD.match(currentName)) {
unit = DistanceUnit.fromString(parser.text());
} else if (DISTANCE_TYPE_FIELD.match(currentName)) {
geoDistance = GeoDistance.fromString(parser.text());
} else if (VALIDATION_METHOD_FIELD.match(currentName)) {
validation = GeoValidationMethod.fromString(parser.text());
} else if (SORTMODE_FIELD.match(currentName)) {
sortMode = SortMode.fromString(parser.text());
} else if (NESTED_PATH_FIELD.match(currentName)) {
nestedPath = parser.text();
} else if (token == Token.VALUE_STRING) {
if (fieldName != null && fieldName.equals(currentName) == false) {
throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
}
GeoPoint point = new GeoPoint();
point.resetFromString(parser.text());
geoPoints.add(point);
fieldName = currentName;
} else if (fieldName.equals(currentName)) {
throw new ParsingException(parser.getTokenLocation(), "Only geohashes of type string supported for field [{}]", currentName);
} else {
throw new ParsingException(parser.getTokenLocation(), "[{}] does not support [{}]", NAME, currentName);
}
}
}
GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, geoPoints.toArray(new GeoPoint[geoPoints.size()]));
result.geoDistance(geoDistance);
result.unit(unit);
result.order(order);
if (sortMode != null) {
result.sortMode(sortMode);
}
if (nestedFilter != null) {
result.setNestedFilter(nestedFilter);
}
result.setNestedPath(nestedPath);
if (validation != null) {
result.validation(validation);
}
return result;
}
use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method verifyShardInfo.
private void verifyShardInfo(XContentParser parser, boolean primary, boolean includeDiskInfo, ShardRoutingState state) throws IOException {
parser.nextToken();
assertEquals(Token.START_OBJECT, parser.currentToken());
parser.nextToken();
assertEquals("index", parser.currentName());
parser.nextToken();
assertEquals("idx", parser.text());
parser.nextToken();
assertEquals("shard", parser.currentName());
parser.nextToken();
assertEquals(0, parser.intValue());
parser.nextToken();
assertEquals("primary", parser.currentName());
parser.nextToken();
assertEquals(primary, parser.booleanValue());
parser.nextToken();
assertEquals("current_state", parser.currentName());
parser.nextToken();
assertEquals(state.toString().toLowerCase(Locale.ROOT), parser.text());
if (state == ShardRoutingState.UNASSIGNED) {
parser.nextToken();
assertEquals("unassigned_info", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
Token token;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
// until we reach end of unassigned_info
if (token == XContentParser.Token.FIELD_NAME) {
// we should never display "delayed" from unassigned info
assertNotEquals("delayed", parser.currentName());
if (parser.currentName().equals("last_allocation_status")) {
parser.nextToken();
assertThat(parser.text(), isOneOf(AllocationDecision.NO.toString(), AllocationDecision.NO_VALID_SHARD_COPY.toString(), AllocationDecision.AWAITING_INFO.toString(), AllocationDecision.NO_ATTEMPT.toString()));
}
}
}
} else {
assertEquals(ShardRoutingState.STARTED, state);
parser.nextToken();
assertEquals("current_node", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
Token token;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
// until we reach end of current_node
if (token == Token.FIELD_NAME) {
assertTrue(parser.currentName().equals("id") || parser.currentName().equals("name") || parser.currentName().equals("transport_address") || parser.currentName().equals("weight_ranking"));
} else {
assertTrue(token.isValue());
assertNotNull(parser.text());
}
}
}
if (includeDiskInfo) {
// disk info is included, just verify the object is there
parser.nextToken();
assertEquals("cluster_info", parser.currentName());
assertEquals(Token.START_OBJECT, parser.nextToken());
int numObjects = 1;
while (numObjects > 0) {
Token token = parser.nextToken();
if (token == Token.START_OBJECT) {
++numObjects;
} else if (token == Token.END_OBJECT) {
--numObjects;
}
}
}
}
use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.
the class GeoPolygonQueryBuilder method fromXContent.
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;
}
use of org.elasticsearch.common.xcontent.XContentParser.Token in project elasticsearch by elastic.
the class GeoDistanceAggregationBuilder method parseRange.
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);
}
}
Aggregations