use of org.opensearch.OpenSearchStatusException in project OpenSearch by opensearch-project.
the class RestHighLevelClient method parseResponseException.
/**
* Converts a {@link ResponseException} obtained from the low level REST client into an {@link OpenSearchException}.
* If a response body was returned, tries to parse it as an error returned from OpenSearch.
* If no response body was returned or anything goes wrong while parsing the error, returns a new {@link OpenSearchStatusException}
* that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned
* exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing.
*/
protected final OpenSearchStatusException parseResponseException(ResponseException responseException) {
Response response = responseException.getResponse();
HttpEntity entity = response.getEntity();
OpenSearchStatusException opensearchException;
RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode());
if (entity == null) {
opensearchException = new OpenSearchStatusException(responseException.getMessage(), restStatus, responseException);
} else {
try {
opensearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
opensearchException.addSuppressed(responseException);
} catch (Exception e) {
opensearchException = new OpenSearchStatusException("Unable to parse response body", restStatus, responseException);
opensearchException.addSuppressed(e);
}
}
return opensearchException;
}
use of org.opensearch.OpenSearchStatusException in project OpenSearch by opensearch-project.
the class IndicesClientIT method testIndexTemplates.
public void testIndexTemplates() throws Exception {
String templateName = "my-template";
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
List<String> pattern = Collections.singletonList("pattern");
ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName);
boolean exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
assertTrue(exist);
GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName);
GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync);
assertThat(getResponse.getIndexTemplates().size(), equalTo(1));
assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true));
assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate));
DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName);
response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, highLevelClient().indices()::deleteIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
assertFalse(exist);
}
use of org.opensearch.OpenSearchStatusException in project OpenSearch by opensearch-project.
the class IndicesClientIT method testPutTemplateWithTypesUsingUntypedAPI.
public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{" + " \"my_doc_type\": {" + " \"properties\": {" + " \"host_name\": {" + " \"type\": \"keyword\"" + " }" + " }" + " }" + "}", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
OpenSearchStatusException badMappingError = expectThrows(OpenSearchStatusException.class, () -> execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync));
assertThat(badMappingError.getDetailedMessage(), containsString("Root mapping definition has unsupported parameters: [my_doc_type"));
}
use of org.opensearch.OpenSearchStatusException in project OpenSearch by opensearch-project.
the class ReindexIT method testReindex.
public void testReindex() throws IOException {
final String sourceIndex = "source1";
final String destinationIndex = "dest";
{
// Prepare
Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build();
createIndex(sourceIndex, settings);
createIndex(destinationIndex, settings);
BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)).add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status());
}
{
// reindex one document with id 1 from source to destination
ReindexRequest reindexRequest = new ReindexRequest();
reindexRequest.setSourceIndices(sourceIndex);
reindexRequest.setDestIndex(destinationIndex);
reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1"));
reindexRequest.setRefresh(true);
BulkByScrollResponse bulkResponse = execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync);
assertEquals(1, bulkResponse.getCreated());
assertEquals(1, bulkResponse.getTotal());
assertEquals(0, bulkResponse.getDeleted());
assertEquals(0, bulkResponse.getNoops());
assertEquals(0, bulkResponse.getVersionConflicts());
assertEquals(1, bulkResponse.getBatches());
assertTrue(bulkResponse.getTook().getMillis() > 0);
assertEquals(1, bulkResponse.getBatches());
assertEquals(0, bulkResponse.getBulkFailures().size());
assertEquals(0, bulkResponse.getSearchFailures().size());
}
{
// set require_alias=true, but there exists no alias
ReindexRequest reindexRequest = new ReindexRequest();
reindexRequest.setSourceIndices(sourceIndex);
reindexRequest.setDestIndex(destinationIndex);
reindexRequest.setSourceQuery(new IdsQueryBuilder().addIds("1"));
reindexRequest.setRefresh(true);
reindexRequest.setRequireAlias(true);
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync);
});
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [dest] and [require_alias] request flag is [true] and [dest] is not an alias]", exception.getMessage());
}
}
use of org.opensearch.OpenSearchStatusException in project OpenSearch by opensearch-project.
the class SearchIT method testMultiSearchTemplateAllBad.
public void testMultiSearchTemplateAllBad() throws Exception {
MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest();
SearchTemplateRequest badRequest1 = new SearchTemplateRequest();
badRequest1.setRequest(new SearchRequest("index"));
badRequest1.setScriptType(ScriptType.INLINE);
badRequest1.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("number", "BAD NUMBER");
badRequest1.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(badRequest1);
SearchTemplateRequest badRequest2 = new SearchTemplateRequest();
badRequest2.setRequest(new SearchRequest("index"));
badRequest2.setScriptType(ScriptType.INLINE);
badRequest2.setScript("BAD QUERY TEMPLATE");
scriptParams = new HashMap<>();
scriptParams.put("number", "BAD NUMBER");
badRequest2.setScriptParams(scriptParams);
multiSearchTemplateRequest.add(badRequest2);
// The whole HTTP request should fail if no nested search requests are valid
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, highLevelClient()::msearchTemplateAsync));
assertEquals(RestStatus.BAD_REQUEST, exception.status());
assertThat(exception.getMessage(), containsString("no requests added"));
}
Aggregations