use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class Settings method fromXContent.
private static Settings fromXContent(XContentParser parser, boolean allowNullValues, boolean validateEndOfStream) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Builder innerBuilder = Settings.builder();
StringBuilder currentKeyBuilder = new StringBuilder();
fromXContent(parser, currentKeyBuilder, innerBuilder, allowNullValues);
if (validateEndOfStream) {
// ensure we reached the end of the stream
XContentParser.Token lastToken = null;
try {
while (!parser.isClosed() && (lastToken = parser.nextToken()) == null) ;
} catch (Exception e) {
throw new OpenSearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", e, parser.getTokenLocation().lineNumber, parser.getTokenLocation().columnNumber);
}
if (lastToken != null) {
throw new OpenSearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", parser.getTokenLocation().lineNumber, parser.getTokenLocation().columnNumber);
}
}
return innerBuilder.build();
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class Fuzziness method parseCustomAuto.
private static Fuzziness parseCustomAuto(final String string) {
assert string.toUpperCase(Locale.ROOT).startsWith(AUTO.asString() + ":");
String[] fuzzinessLimit = string.substring(AUTO.asString().length() + 1).split(",");
if (fuzzinessLimit.length == 2) {
try {
int lowerLimit = Integer.parseInt(fuzzinessLimit[0]);
int highLimit = Integer.parseInt(fuzzinessLimit[1]);
return new Fuzziness("AUTO", lowerLimit, highLimit);
} catch (NumberFormatException e) {
throw new OpenSearchParseException("failed to parse [{}] as a \"auto:int,int\"", e, string);
}
} else {
throw new OpenSearchParseException("failed to find low and high distance values");
}
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class JavaDateMathParser method parse.
@Override
public Instant parse(String text, LongSupplier now, boolean roundUpProperty, ZoneId timeZone) {
Instant time;
String mathString;
if (text.startsWith("now")) {
try {
// TODO only millisecond granularity here!
time = Instant.ofEpochMilli(now.getAsLong());
} catch (Exception e) {
throw new OpenSearchParseException("could not read the current timestamp", e);
}
mathString = text.substring("now".length());
} else {
int index = text.indexOf("||");
if (index == -1) {
return parseDateTime(text, timeZone, roundUpProperty);
}
time = parseDateTime(text.substring(0, index), timeZone, false);
mathString = text.substring(index + 2);
}
return parseMath(mathString, time, roundUpProperty, timeZone);
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class JavaDateMathParser method parseDateTime.
private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNoTime) {
if (Strings.isNullOrEmpty(value)) {
throw new OpenSearchParseException("cannot parse empty date");
}
DateFormatter formatter = roundUpIfNoTime ? this.roundupParser : this.formatter;
try {
if (timeZone == null) {
return DateFormatters.from(formatter.parse(value)).toInstant();
} else {
TemporalAccessor accessor = formatter.parse(value);
ZoneId zoneId = TemporalQueries.zone().queryFrom(accessor);
if (zoneId != null) {
timeZone = zoneId;
}
return DateFormatters.from(accessor).withZoneSameLocal(timeZone).toInstant();
}
} catch (IllegalArgumentException | DateTimeParseException e) {
throw new OpenSearchParseException("failed to parse date field [{}] with format [{}]: [{}]", e, value, format, e.getMessage());
}
}
use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.
the class MultiTermVectorsRequest method add.
public void add(TermVectorsRequest template, @Nullable XContentParser parser) throws IOException {
XContentParser.Token token;
String currentFieldName = null;
if (parser != null) {
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 ("docs".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("docs array element should include an object");
}
TermVectorsRequest termVectorsRequest = new TermVectorsRequest(template);
TermVectorsRequest.parseRequest(termVectorsRequest, parser);
add(termVectorsRequest);
}
} else if ("ids".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (!token.isValue()) {
throw new IllegalArgumentException("ids array element should only contain ids");
}
ids.add(parser.text());
}
} else {
throw new OpenSearchParseException("no parameter named [{}] and type ARRAY", currentFieldName);
}
} else if (token == XContentParser.Token.START_OBJECT && currentFieldName != null) {
if ("parameters".equals(currentFieldName)) {
TermVectorsRequest.parseRequest(template, parser);
} else {
throw new OpenSearchParseException("no parameter named [{}] and type OBJECT", currentFieldName);
}
} else if (currentFieldName != null) {
throw new OpenSearchParseException("_mtermvectors: Parameter [{}] not supported", currentFieldName);
}
}
}
for (String id : ids) {
TermVectorsRequest curRequest = new TermVectorsRequest(template);
curRequest.id(id);
requests.add(curRequest);
}
}
Aggregations