Search in sources :

Example 6 with OpenSearchStatusException

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);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) CompressedXContent(org.opensearch.common.compress.CompressedXContent) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 7 with OpenSearchStatusException

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"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 8 with OpenSearchStatusException

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());
    }
}
Also used : ReindexRequest(org.opensearch.index.reindex.ReindexRequest) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) BulkRequest(org.opensearch.action.bulk.BulkRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexRequest(org.opensearch.action.index.IndexRequest) Settings(org.opensearch.common.settings.Settings) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 9 with OpenSearchStatusException

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"));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) HashMap(java.util.HashMap) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 10 with OpenSearchStatusException

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

the class SearchIT method testNonExistentSearchTemplate.

public void testNonExistentSearchTemplate() {
    SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
    searchTemplateRequest.setRequest(new SearchRequest("index"));
    searchTemplateRequest.setScriptType(ScriptType.STORED);
    searchTemplateRequest.setScript("non-existent");
    searchTemplateRequest.setScriptParams(Collections.emptyMap());
    OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(searchTemplateRequest, highLevelClient()::searchTemplate, highLevelClient()::searchTemplateAsync));
    assertEquals(RestStatus.NOT_FOUND, exception.status());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Aggregations

OpenSearchStatusException (org.opensearch.OpenSearchStatusException)60 IOException (java.io.IOException)29 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)19 XContentParser (org.opensearch.common.xcontent.XContentParser)18 RestStatus (org.opensearch.rest.RestStatus)17 ActionListener (org.opensearch.action.ActionListener)16 LogManager (org.apache.logging.log4j.LogManager)14 Logger (org.apache.logging.log4j.Logger)14 IndexRequest (org.opensearch.action.index.IndexRequest)14 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)14 Client (org.opensearch.client.Client)13 List (java.util.List)12 GetRequest (org.opensearch.action.get.GetRequest)12 SearchRequest (org.opensearch.action.search.SearchRequest)12 SearchResponse (org.opensearch.action.search.SearchResponse)12 Settings (org.opensearch.common.settings.Settings)12 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)12 TransportService (org.opensearch.transport.TransportService)12 Map (java.util.Map)11 IndexResponse (org.opensearch.action.index.IndexResponse)11