Search in sources :

Example 81 with OpenSearchParseException

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

the class RepositoryData method parseIndices.

/**
 * Parses information about all indices tracked in the repository and populates {@code indexSnapshots}, {@code indexLookup} and
 * {@code shardGenerations}.
 *
 * @param parser              x-content parser
 * @param fixBrokenShardGens  whether or not to fix broken shard generation (see {@link #snapshotsFromXContent} for details)
 * @param snapshots           map of snapshot uuid to {@link SnapshotId} that was populated by {@link #parseSnapshots}
 * @param indexSnapshots      map of {@link IndexId} to list of {@link SnapshotId} that contain the given index
 * @param indexLookup         map of index uuid (as returned by {@link IndexId#getId}) to {@link IndexId}
 * @param shardGenerations    shard generations builder that is populated index by this method
 */
private static void parseIndices(XContentParser parser, boolean fixBrokenShardGens, Map<String, SnapshotId> snapshots, Map<IndexId, List<SnapshotId>> indexSnapshots, Map<String, IndexId> indexLookup, ShardGenerations.Builder shardGenerations) throws IOException {
    XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        final String indexName = parser.currentName();
        final List<SnapshotId> snapshotIds = new ArrayList<>();
        final List<String> gens = new ArrayList<>();
        IndexId indexId = null;
        XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
        while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
            final String indexMetaFieldName = parser.currentName();
            final XContentParser.Token currentToken = parser.nextToken();
            switch(indexMetaFieldName) {
                case INDEX_ID:
                    indexId = new IndexId(indexName, parser.text());
                    break;
                case SNAPSHOTS:
                    XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, currentToken, parser);
                    XContentParser.Token currToken;
                    while ((currToken = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        final String uuid;
                        // the old format pre 5.4.1 which contains the snapshot name and uuid
                        if (currToken == XContentParser.Token.START_OBJECT) {
                            uuid = parseLegacySnapshotUUID(parser);
                        } else {
                            // the new format post 5.4.1 that only contains the snapshot uuid,
                            // since we already have the name/uuid combo in the snapshots array
                            uuid = parser.text();
                        }
                        final SnapshotId snapshotId = snapshots.get(uuid);
                        if (snapshotId == null) {
                            // different versions create or delete snapshot in the same repository.
                            throw new OpenSearchParseException("Detected a corrupted repository, index " + indexId + " references an unknown snapshot uuid [" + uuid + "]");
                        }
                        snapshotIds.add(snapshotId);
                    }
                    break;
                case SHARD_GENERATIONS:
                    XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, currentToken, parser);
                    while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                        gens.add(parser.textOrNull());
                    }
                    break;
            }
        }
        assert indexId != null;
        indexSnapshots.put(indexId, Collections.unmodifiableList(snapshotIds));
        indexLookup.put(indexId.getId(), indexId);
        for (int i = 0; i < gens.size(); i++) {
            String parsedGen = gens.get(i);
            if (fixBrokenShardGens) {
                parsedGen = ShardGenerations.fixShardGeneration(parsedGen);
            }
            if (parsedGen != null) {
                shardGenerations.put(indexId, i, parsedGen);
            }
        }
    }
}
Also used : SnapshotId(org.opensearch.snapshots.SnapshotId) OpenSearchParseException(org.opensearch.OpenSearchParseException) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 82 with OpenSearchParseException

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

the class TermSuggestionBuilder method fromXContent.

public static TermSuggestionBuilder fromXContent(XContentParser parser) throws IOException {
    TermSuggestionBuilder tmpSuggestion = new TermSuggestionBuilder("_na_");
    XContentParser.Token token;
    String currentFieldName = null;
    String fieldname = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (SuggestionBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.analyzer(parser.text());
            } else if (SuggestionBuilder.FIELDNAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                fieldname = parser.text();
            } else if (SuggestionBuilder.SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.size(parser.intValue());
            } else if (SuggestionBuilder.SHARDSIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.shardSize(parser.intValue());
            } else if (SUGGESTMODE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.suggestMode(SuggestMode.resolve(parser.text()));
            } else if (ACCURACY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.accuracy(parser.floatValue());
            } else if (SORT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.sort(SortBy.resolve(parser.text()));
            } else if (STRING_DISTANCE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.stringDistance(StringDistanceImpl.resolve(parser.text()));
            } else if (MAX_EDITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.maxEdits(parser.intValue());
            } else if (MAX_INSPECTIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.maxInspections(parser.intValue());
            } else if (MAX_TERM_FREQ_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.maxTermFreq(parser.floatValue());
            } else if (PREFIX_LENGTH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.prefixLength(parser.intValue());
            } else if (MIN_WORD_LENGTH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.minWordLength(parser.intValue());
            } else if (MIN_DOC_FREQ_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                tmpSuggestion.minDocFreq(parser.floatValue());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggester[term] doesn't support field [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "suggester[term] parsing failed on [" + currentFieldName + "]");
        }
    }
    // now we should have field name, check and copy fields over to the suggestion builder we return
    if (fieldname == null) {
        throw new OpenSearchParseException("the required field option [" + FIELDNAME_FIELD.getPreferredName() + "] is missing");
    }
    return new TermSuggestionBuilder(fieldname, tmpSuggestion);
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 83 with OpenSearchParseException

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

the class CreateIndexRequestTests method testSettingsType.

public void testSettingsType() throws IOException {
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    builder.startObject().startArray("settings").endArray().endObject();
    CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest();
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> parsedCreateIndexRequest.source(builder));
    assertThat(e.getMessage(), equalTo("key [settings] must be an object"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 84 with OpenSearchParseException

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

the class GeoBoundingBox method parseBoundingBox.

/**
 * Parses the bounding box and returns bottom, top, left, right coordinates
 */
public static GeoBoundingBox parseBoundingBox(XContentParser parser) throws IOException, OpenSearchParseException {
    XContentParser.Token token = parser.currentToken();
    if (token != XContentParser.Token.START_OBJECT) {
        throw new OpenSearchParseException("failed to parse bounding box. Expected start object but found [{}]", token);
    }
    double top = Double.NaN;
    double bottom = Double.NaN;
    double left = Double.NaN;
    double right = Double.NaN;
    String currentFieldName;
    GeoPoint sparse = new GeoPoint();
    Rectangle envelope = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
            token = parser.nextToken();
            if (WKT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                try {
                    Geometry geometry = WKT_PARSER.fromWKT(parser.text());
                    if (ShapeType.ENVELOPE.equals(geometry.type()) == false) {
                        throw new OpenSearchParseException("failed to parse WKT bounding box. [" + geometry.type() + "] found. expected [" + ShapeType.ENVELOPE + "]");
                    }
                    envelope = (Rectangle) geometry;
                } catch (ParseException | IllegalArgumentException e) {
                    throw new OpenSearchParseException("failed to parse WKT bounding box", e);
                }
            } else if (TOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                top = parser.doubleValue();
            } else if (BOTTOM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                bottom = parser.doubleValue();
            } else if (LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                left = parser.doubleValue();
            } else if (RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                right = parser.doubleValue();
            } else {
                if (TOP_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_LEFT);
                    top = sparse.getLat();
                    left = sparse.getLon();
                } else if (BOTTOM_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_RIGHT);
                    bottom = sparse.getLat();
                    right = sparse.getLon();
                } else if (TOP_RIGHT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.TOP_RIGHT);
                    top = sparse.getLat();
                    right = sparse.getLon();
                } else if (BOTTOM_LEFT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                    GeoUtils.parseGeoPoint(parser, sparse, false, GeoUtils.EffectivePoint.BOTTOM_LEFT);
                    bottom = sparse.getLat();
                    left = sparse.getLon();
                } else {
                    throw new OpenSearchParseException("failed to parse bounding box. unexpected field [{}]", currentFieldName);
                }
            }
        } else {
            throw new OpenSearchParseException("failed to parse bounding box. field name expected but [{}] found", token);
        }
    }
    if (envelope != null) {
        if (Double.isNaN(top) == false || Double.isNaN(bottom) == false || Double.isNaN(left) == false || Double.isNaN(right) == false) {
            throw new OpenSearchParseException("failed to parse bounding box. Conflicting definition found " + "using well-known text and explicit corners.");
        }
        GeoPoint topLeft = new GeoPoint(envelope.getMaxLat(), envelope.getMinLon());
        GeoPoint bottomRight = new GeoPoint(envelope.getMinLat(), envelope.getMaxLon());
        return new GeoBoundingBox(topLeft, bottomRight);
    }
    GeoPoint topLeft = new GeoPoint(top, left);
    GeoPoint bottomRight = new GeoPoint(bottom, right);
    return new GeoBoundingBox(topLeft, bottomRight);
}
Also used : Geometry(org.opensearch.geometry.Geometry) OpenSearchParseException(org.opensearch.OpenSearchParseException) Rectangle(org.opensearch.geometry.Rectangle) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParseException(java.text.ParseException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 85 with OpenSearchParseException

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

the class GeoWKTParser method parseLinearRing.

// A LinearRing is closed LineString with 4 or more positions. The first and last positions
// are equivalent (they represent equivalent points).
private static LineStringBuilder parseLinearRing(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
    String token = nextEmptyOrOpen(stream);
    if (token.equals(EMPTY)) {
        return null;
    }
    List<Coordinate> coordinates = parseCoordinateList(stream, ignoreZValue, coerce);
    int coordinatesNeeded = coerce ? 3 : 4;
    if (coordinates.size() >= coordinatesNeeded) {
        if (!coordinates.get(0).equals(coordinates.get(coordinates.size() - 1))) {
            if (coerce) {
                coordinates.add(coordinates.get(0));
            } else {
                throw new OpenSearchParseException("invalid LinearRing found (coordinates are not closed)");
            }
        }
    }
    if (coordinates.size() < 4) {
        throw new OpenSearchParseException("invalid number of points in LinearRing (found [{}] - must be >= 4)", coordinates.size());
    }
    return new LineStringBuilder(coordinates);
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) Coordinate(org.locationtech.jts.geom.Coordinate) GeoPoint(org.opensearch.common.geo.GeoPoint) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder)

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