Search in sources :

Example 76 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class JavaDateMathParser method parseMath.

private Instant parseMath(final String mathString, final Instant time, final boolean roundUpProperty, ZoneId timeZone) throws OpenSearchParseException {
    if (timeZone == null) {
        timeZone = ZoneOffset.UTC;
    }
    ZonedDateTime dateTime = ZonedDateTime.ofInstant(time, timeZone);
    for (int i = 0; i < mathString.length(); ) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new OpenSearchParseException("operator not supported for date math [{}]", mathString);
            }
        }
        if (i >= mathString.length()) {
            throw new OpenSearchParseException("truncated date math [{}]", mathString);
        }
        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new OpenSearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new OpenSearchParseException("rounding `/` can only be used on single unit types [{}]", mathString);
            }
        }
        char unit = mathString.charAt(i++);
        switch(unit) {
            case 'y':
                if (round) {
                    dateTime = dateTime.withDayOfYear(1).with(LocalTime.MIN);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusYears(1);
                    }
                } else {
                    dateTime = dateTime.plusYears(sign * num);
                }
                break;
            case 'M':
                if (round) {
                    dateTime = dateTime.withDayOfMonth(1).with(LocalTime.MIN);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusMonths(1);
                    }
                } else {
                    dateTime = dateTime.plusMonths(sign * num);
                }
                break;
            case 'w':
                if (round) {
                    dateTime = dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusWeeks(1);
                    }
                } else {
                    dateTime = dateTime.plusWeeks(sign * num);
                }
                break;
            case 'd':
                if (round) {
                    dateTime = dateTime.with(LocalTime.MIN);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusDays(1);
                    }
                } else {
                    dateTime = dateTime.plusDays(sign * num);
                }
                break;
            case 'h':
            case 'H':
                if (round) {
                    dateTime = dateTime.withMinute(0).withSecond(0).withNano(0);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusHours(1);
                    }
                } else {
                    dateTime = dateTime.plusHours(sign * num);
                }
                break;
            case 'm':
                if (round) {
                    dateTime = dateTime.withSecond(0).withNano(0);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusMinutes(1);
                    }
                } else {
                    dateTime = dateTime.plusMinutes(sign * num);
                }
                break;
            case 's':
                if (round) {
                    dateTime = dateTime.withNano(0);
                    if (roundUpProperty) {
                        dateTime = dateTime.plusSeconds(1);
                    }
                } else {
                    dateTime = dateTime.plusSeconds(sign * num);
                }
                break;
            default:
                throw new OpenSearchParseException("unit [{}] not supported for date math [{}]", unit, mathString);
        }
        if (round && roundUpProperty) {
            // subtract 1 millisecond to get the largest inclusive value
            dateTime = dateTime.minus(1, ChronoField.MILLI_OF_SECOND.getBaseUnit());
        }
    }
    return dateTime.toInstant();
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) ZonedDateTime(java.time.ZonedDateTime)

Example 77 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class DecayFunctionBuilder method parseDateVariable.

private AbstractDistanceScoreFunction parseDateVariable(XContentParser parser, QueryShardContext context, MappedFieldType dateFieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    String scaleString = null;
    String originString = null;
    String offsetString = "0d";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scaleString = parser.text();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            originString = parser.text();
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            offsetString = parser.text();
        } else {
            throw new OpenSearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    long origin;
    if (originString == null) {
        origin = context.nowInMillis();
    } else {
        origin = ((DateFieldMapper.DateFieldType) dateFieldType).parseToLong(originString, false, null, null, context::nowInMillis);
    }
    if (scaleString == null) {
        throw new OpenSearchParseException("[{}] must be set for date fields.", DecayFunctionBuilder.SCALE);
    }
    TimeValue val = TimeValue.parseTimeValue(scaleString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".scale");
    double scale = val.getMillis();
    val = TimeValue.parseTimeValue(offsetString, TimeValue.timeValueHours(24), DecayFunctionParser.class.getSimpleName() + ".offset");
    double offset = val.getMillis();
    IndexNumericFieldData numericFieldData = context.getForField(dateFieldType);
    return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode, getFunctionName());
}
Also used : DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) OpenSearchParseException(org.opensearch.OpenSearchParseException) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) XContentParser(org.opensearch.common.xcontent.XContentParser) TimeValue(org.opensearch.common.unit.TimeValue)

Example 78 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class DecayFunctionBuilder method parseGeoVariable.

private AbstractDistanceScoreFunction parseGeoVariable(XContentParser parser, QueryShardContext context, MappedFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    GeoPoint origin = new GeoPoint();
    String scaleString = null;
    String offsetString = "0km";
    double decay = 0.5;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scaleString = parser.text();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            origin = GeoUtils.parseGeoPoint(parser);
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            offsetString = parser.text();
        } else {
            throw new OpenSearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (origin == null || scaleString == null) {
        throw new OpenSearchParseException("[{}] and [{}] must be set for geo fields.", DecayFunctionBuilder.ORIGIN, DecayFunctionBuilder.SCALE);
    }
    double scale = DistanceUnit.DEFAULT.parse(scaleString, DistanceUnit.DEFAULT);
    double offset = DistanceUnit.DEFAULT.parse(offsetString, DistanceUnit.DEFAULT);
    IndexGeoPointFieldData indexFieldData = context.getForField(fieldType);
    return new GeoFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), indexFieldData, mode, getFunctionName());
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchParseException(org.opensearch.OpenSearchParseException) IndexGeoPointFieldData(org.opensearch.index.fielddata.IndexGeoPointFieldData) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 79 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class DecayFunctionBuilder method parseNumberVariable.

private AbstractDistanceScoreFunction parseNumberVariable(XContentParser parser, QueryShardContext context, MappedFieldType fieldType, MultiValueMode mode) throws IOException {
    XContentParser.Token token;
    String parameterName = null;
    double scale = 0;
    double origin = 0;
    double decay = 0.5;
    double offset = 0.0d;
    boolean scaleFound = false;
    boolean refFound = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            parameterName = parser.currentName();
        } else if (DecayFunctionBuilder.SCALE.equals(parameterName)) {
            scale = parser.doubleValue();
            scaleFound = true;
        } else if (DecayFunctionBuilder.DECAY.equals(parameterName)) {
            decay = parser.doubleValue();
        } else if (DecayFunctionBuilder.ORIGIN.equals(parameterName)) {
            origin = parser.doubleValue();
            refFound = true;
        } else if (DecayFunctionBuilder.OFFSET.equals(parameterName)) {
            offset = parser.doubleValue();
        } else {
            throw new OpenSearchParseException("parameter [{}] not supported!", parameterName);
        }
    }
    if (!scaleFound || !refFound) {
        throw new OpenSearchParseException("both [{}] and [{}] must be set for numeric fields.", DecayFunctionBuilder.SCALE, DecayFunctionBuilder.ORIGIN);
    }
    IndexNumericFieldData numericFieldData = context.getForField(fieldType);
    return new NumericFieldDataScoreFunction(origin, scale, decay, offset, getDecayFunction(), numericFieldData, mode, getFunctionName());
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 80 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class BlobStoreIndexShardSnapshots method fromXContent.

public static BlobStoreIndexShardSnapshots fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    if (token == null) {
        // New parser
        token = parser.nextToken();
    }
    Map<String, List<String>> snapshotsMap = new HashMap<>();
    Map<String, String> historyUUIDs = new HashMap<>();
    Map<String, FileInfo> files = new HashMap<>();
    if (token == XContentParser.Token.START_OBJECT) {
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
            String currentFieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_ARRAY) {
                if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) == false) {
                    throw new OpenSearchParseException("unknown array [{}]", currentFieldName);
                }
                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                    FileInfo fileInfo = FileInfo.fromXContent(parser);
                    files.put(fileInfo.name(), fileInfo);
                }
            } else if (token == XContentParser.Token.START_OBJECT) {
                if (ParseFields.SNAPSHOTS.match(currentFieldName, parser.getDeprecationHandler()) == false) {
                    throw new OpenSearchParseException("unknown object [{}]", currentFieldName);
                }
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
                    String snapshot = parser.currentName();
                    XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                            if (ParseFields.FILES.match(currentFieldName, parser.getDeprecationHandler()) && parser.nextToken() == XContentParser.Token.START_ARRAY) {
                                List<String> fileNames = new ArrayList<>();
                                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                    fileNames.add(parser.text());
                                }
                                snapshotsMap.put(snapshot, fileNames);
                            } else if (ParseFields.SHARD_STATE_ID.match(currentFieldName, parser.getDeprecationHandler())) {
                                parser.nextToken();
                                historyUUIDs.put(snapshot, parser.text());
                            }
                        }
                    }
                }
            } else {
                throw new OpenSearchParseException("unexpected token [{}]", token);
            }
        }
    }
    List<SnapshotFiles> snapshots = new ArrayList<>(snapshotsMap.size());
    for (Map.Entry<String, List<String>> entry : snapshotsMap.entrySet()) {
        List<FileInfo> fileInfosBuilder = new ArrayList<>();
        for (String file : entry.getValue()) {
            FileInfo fileInfo = files.get(file);
            assert fileInfo != null;
            fileInfosBuilder.add(fileInfo);
        }
        snapshots.add(new SnapshotFiles(entry.getKey(), Collections.unmodifiableList(fileInfosBuilder), historyUUIDs.get(entry.getKey())));
    }
    return new BlobStoreIndexShardSnapshots(files, Collections.unmodifiableList(snapshots));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileInfo(org.opensearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo) OpenSearchParseException(org.opensearch.OpenSearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) 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