use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class WildcardQueryBuilder method fromXContent.
public static WildcardQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
XContentParser parser = parseContext.parser();
String fieldName = null;
String rewrite = null;
String value = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
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) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else {
if (WILDCARD_FIELD.match(currentFieldName)) {
value = parser.text();
} else if (VALUE_FIELD.match(currentFieldName)) {
value = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
boost = parser.floatValue();
} else if (REWRITE_FIELD.match(currentFieldName)) {
rewrite = parser.textOrNull();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
queryName = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "[wildcard] query does not support [" + currentFieldName + "]");
}
}
}
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
value = parser.text();
}
}
return new WildcardQueryBuilder(fieldName, value).rewrite(rewrite).boost(boost).queryName(queryName);
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class WrapperQueryBuilder method fromXContent.
public static WrapperQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
XContentParser parser = parseContext.parser();
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)) {
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);
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class RestValidateQueryAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
validateQueryRequest.explain(request.paramAsBoolean("explain", false));
validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
validateQueryRequest.rewrite(request.paramAsBoolean("rewrite", false));
Exception bodyParsingException = null;
try {
request.withContentOrSourceParamParserOrNull(parser -> {
if (parser != null) {
validateQueryRequest.query(RestActions.getQueryContent(parser));
} else if (request.hasParam("q")) {
validateQueryRequest.query(RestActions.urlParamsToQueryBuilder(request));
}
});
} catch (Exception e) {
bodyParsingException = e;
}
final Exception finalBodyParsingException = bodyParsingException;
return channel -> {
if (finalBodyParsingException != null) {
if (finalBodyParsingException instanceof ParsingException) {
handleException(validateQueryRequest, ((ParsingException) finalBodyParsingException).getDetailedMessage(), channel);
} else {
handleException(validateQueryRequest, finalBodyParsingException.getMessage(), channel);
}
} else {
client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {
@Override
public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.field(VALID_FIELD, response.isValid());
buildBroadcastShardsHeader(builder, request, response);
if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
builder.startArray(EXPLANATIONS_FIELD);
for (QueryExplanation explanation : response.getQueryExplanation()) {
builder.startObject();
if (explanation.getIndex() != null) {
builder.field(INDEX_FIELD, explanation.getIndex());
}
builder.field(VALID_FIELD, explanation.isValid());
if (explanation.getError() != null) {
builder.field(ERROR_FIELD, explanation.getError());
}
if (explanation.getExplanation() != null) {
builder.field(EXPLANATION_FIELD, explanation.getExplanation());
}
builder.endObject();
}
builder.endArray();
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
}
};
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class BucketSelectorPipelineAggregationBuilder method parse.
public static BucketSelectorPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
XContentParser parser = context.parser();
XContentParser.Token token;
Script script = null;
String currentFieldName = null;
Map<String, String> bucketsPathsMap = null;
GapPolicy gapPolicy = 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 (BUCKETS_PATH.match(currentFieldName)) {
bucketsPathsMap = new HashMap<>();
bucketsPathsMap.put("_value", parser.text());
} else if (GAP_POLICY.match(currentFieldName)) {
gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
} else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
script = Script.parse(parser);
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (BUCKETS_PATH.match(currentFieldName)) {
List<String> paths = new ArrayList<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
String path = parser.text();
paths.add(path);
}
bucketsPathsMap = new HashMap<>();
for (int i = 0; i < paths.size(); i++) {
bucketsPathsMap.put("_value" + i, paths.get(i));
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
script = Script.parse(parser);
} else if (BUCKETS_PATH.match(currentFieldName)) {
Map<String, Object> map = parser.map();
bucketsPathsMap = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].");
}
}
if (bucketsPathsMap == null) {
throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
}
if (script == null) {
throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + Script.SCRIPT_PARSE_FIELD.getPreferredName() + "] for bucket_selector aggregation [" + reducerName + "]");
}
BucketSelectorPipelineAggregationBuilder factory = new BucketSelectorPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
if (gapPolicy != null) {
factory.gapPolicy(gapPolicy);
}
return factory;
}
use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.
the class IpRangeAggregationBuilder method parseRange.
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);
}
}
Aggregations