Search in sources :

Example 16 with OpenSearchParseException

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();
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchGenerationException(org.opensearch.OpenSearchGenerationException) GeneralSecurityException(java.security.GeneralSecurityException) OpenSearchParseException(org.opensearch.OpenSearchParseException) UncheckedIOException(java.io.UncheckedIOException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 17 with OpenSearchParseException

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");
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException)

Example 18 with OpenSearchParseException

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);
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) Instant(java.time.Instant) OpenSearchParseException(org.opensearch.OpenSearchParseException) DateTimeParseException(java.time.format.DateTimeParseException)

Example 19 with OpenSearchParseException

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());
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) OpenSearchParseException(org.opensearch.OpenSearchParseException) ZoneId(java.time.ZoneId)

Example 20 with OpenSearchParseException

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);
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

OpenSearchParseException (org.opensearch.OpenSearchParseException)105 XContentParser (org.opensearch.common.xcontent.XContentParser)34 HashMap (java.util.HashMap)27 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 ArrayList (java.util.ArrayList)14 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Map (java.util.Map)11 IOException (java.io.IOException)10 List (java.util.List)7 ParsingException (org.opensearch.common.ParsingException)5 GeoPoint (org.opensearch.common.geo.GeoPoint)5 Token (org.opensearch.common.xcontent.XContentParser.Token)5 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)4 UncheckedIOException (java.io.UncheckedIOException)3 DateTimeParseException (java.time.format.DateTimeParseException)3 HashSet (java.util.HashSet)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)3