Search in sources :

Example 71 with OpenSearchParseException

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

the class ConfigurationUtils method newConfigurationException.

public static OpenSearchException newConfigurationException(String processorType, String processorTag, String propertyName, String reason) {
    String msg;
    if (propertyName == null) {
        msg = reason;
    } else {
        msg = "[" + propertyName + "] " + reason;
    }
    OpenSearchParseException exception = new OpenSearchParseException(msg);
    addMetadataToException(exception, processorType, processorTag, propertyName);
    return exception;
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException)

Example 72 with OpenSearchParseException

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

the class GeoQueryContext method fromXContent.

public static GeoQueryContext fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    GeoQueryContext.Builder builder = new Builder();
    if (token == XContentParser.Token.START_OBJECT) {
        GEO_CONTEXT_PARSER.parse(parser, builder, null);
    } else if (token == XContentParser.Token.VALUE_STRING) {
        builder.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
    } else {
        throw new OpenSearchParseException("geo context must be an object or string");
    }
    return builder.build();
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 73 with OpenSearchParseException

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

the class SuggestionBuilder method fromXContent.

static SuggestionBuilder<?> fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    String suggestText = null;
    String prefix = null;
    String regex = null;
    SuggestionBuilder<?> suggestionBuilder = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TEXT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                suggestText = parser.text();
            } else if (PREFIX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                prefix = parser.text();
            } else if (REGEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                regex = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggestion does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            suggestionBuilder = parser.namedObject(SuggestionBuilder.class, currentFieldName, null);
        }
    }
    if (suggestionBuilder == null) {
        throw new OpenSearchParseException("missing suggestion object");
    }
    if (suggestText != null) {
        suggestionBuilder.text(suggestText);
    }
    if (prefix != null) {
        suggestionBuilder.prefix(prefix);
    }
    if (regex != null) {
        suggestionBuilder.regex(regex);
    }
    return suggestionBuilder;
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 74 with OpenSearchParseException

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

the class GeoBoundingBoxTests method testInvalidParsePoint.

public void testInvalidParsePoint() throws IOException {
    XContentBuilder bboxBuilder = XContentFactory.jsonBuilder().startObject().field("wkt", "POINT (100.0 100.0)").endObject();
    XContentParser parser = createParser(bboxBuilder);
    parser.nextToken();
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> GeoBoundingBox.parseBoundingBox(parser));
    assertThat(e.getMessage(), equalTo("failed to parse WKT bounding box. [POINT] found. expected [ENVELOPE]"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 75 with OpenSearchParseException

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

the class AllocationCommands method fromXContent.

/**
 * Reads {@link AllocationCommands} from a {@link XContentParser}
 * <pre>
 *     {
 *         "commands" : [
 *              {"allocate" : {"index" : "test", "shard" : 0, "node" : "test"}}
 *         ]
 *     }
 * </pre>
 * @param parser {@link XContentParser} to read the commands from
 * @return {@link AllocationCommands} read
 * @throws IOException if something bad happens while reading the stream
 */
public static AllocationCommands fromXContent(XContentParser parser) throws IOException {
    AllocationCommands commands = new AllocationCommands();
    XContentParser.Token token = parser.currentToken();
    if (token == null) {
        throw new OpenSearchParseException("No commands");
    }
    if (token == XContentParser.Token.FIELD_NAME) {
        if (!parser.currentName().equals("commands")) {
            throw new OpenSearchParseException("expected field name to be named [commands], got [{}] instead", parser.currentName());
        }
        token = parser.nextToken();
        if (token != XContentParser.Token.START_ARRAY) {
            throw new OpenSearchParseException("commands should follow with an array element");
        }
    } else if (token == XContentParser.Token.START_ARRAY) {
    // ok...
    } else {
        throw new OpenSearchParseException("expected either field name [commands], or start array, got [{}] instead", token);
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        if (token == XContentParser.Token.START_OBJECT) {
            // move to the command name
            token = parser.nextToken();
            String commandName = parser.currentName();
            token = parser.nextToken();
            commands.add(parser.namedObject(AllocationCommand.class, commandName, null));
            // move to the end object one
            if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                throw new OpenSearchParseException("allocation command is malformed, done parsing a command," + " but didn't get END_OBJECT, got [{}] instead", token);
            }
        } else {
            throw new OpenSearchParseException("allocation command is malformed, got [{}] instead", token);
        }
    }
    return commands;
}
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