Search in sources :

Example 1 with CreateIndexRequest

use of org.opensearch.client.indices.CreateIndexRequest in project bw-calendar-engine by Bedework.

the class BwIndexEsImpl method newIndex.

@Override
public String newIndex() throws CalFacadeException {
    try {
        final String newName = "bw" + docType.toLowerCase() + newIndexSuffix();
        targetIndex = newName;
        final CreateIndexRequest req = new CreateIndexRequest(newName);
        final String mappingStr = fileToString(Util.buildPath(false, idxpars.getIndexerConfig(), "/", docType.toLowerCase(), "/mappings.json"));
        req.source(mappingStr, XContentType.JSON);
        info("Attempt to create index " + newName);
        final CreateIndexResponse resp = getClient().indices().create(req, RequestOptions.DEFAULT);
        info("Index " + newName + " created");
        final DocBuilder db = getDocBuilder();
        indexDoc(db.makeUpdateInfoDoc(docType), true);
        return newName;
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    }
}
Also used : CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 2 with CreateIndexRequest

use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testRollover.

public void testRollover() throws IOException {
    highLevelClient().indices().create(new CreateIndexRequest("test").alias(new Alias("alias")), RequestOptions.DEFAULT);
    RolloverRequest rolloverRequest = new RolloverRequest("alias", "test_new");
    rolloverRequest.addMaxIndexDocsCondition(1);
    {
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(1, conditionStatus.size());
        assertFalse(conditionStatus.get("[max_docs: 1]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    highLevelClient().index(new IndexRequest("test").id("1").source("field", "value"), RequestOptions.DEFAULT);
    highLevelClient().index(new IndexRequest("test").id("2").source("field", "value").setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT);
    // without the refresh the rollover may not happen as the number of docs seen may be off
    {
        rolloverRequest.addMaxIndexAgeCondition(new TimeValue(1));
        rolloverRequest.dryRun(true);
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertTrue(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(2, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    {
        String mappings = "{\"properties\":{\"field2\":{\"type\":\"keyword\"}}}";
        rolloverRequest.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
        rolloverRequest.dryRun(false);
        rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB));
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertTrue(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(3, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertFalse(conditionStatus.get("[max_size: 1mb]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
}
Also used : RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) Alias(org.opensearch.action.admin.indices.alias.Alias) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RolloverResponse(org.opensearch.client.indices.rollover.RolloverResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) TimeValue(org.opensearch.common.unit.TimeValue)

Example 3 with CreateIndexRequest

use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testCreateIndex.

public void testCreateIndex() throws IOException {
    CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator.randomCreateIndexRequest();
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomTimeout(createIndexRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    RequestConvertersTests.setRandomMasterTimeout(createIndexRequest, expectedParams);
    RequestConvertersTests.setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams);
    Request request = IndicesRequestConverters.createIndex(createIndexRequest);
    Assert.assertEquals("/" + createIndexRequest.index(), request.getEndpoint());
    Assert.assertEquals(expectedParams, request.getParameters());
    Assert.assertEquals(HttpPut.METHOD_NAME, request.getMethod());
    RequestConvertersTests.assertToXContentBody(createIndexRequest, request.getEntity());
}
Also used : HashMap(java.util.HashMap) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest)

Example 4 with CreateIndexRequest

use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.

the class OpenSearchRestHighLevelClientTestCase method createIndexWithMultipleShards.

protected void createIndexWithMultipleShards(String index) throws IOException {
    CreateIndexRequest indexRequest = new CreateIndexRequest(index);
    int shards = randomIntBetween(8, 10);
    indexRequest.settings(Settings.builder().put("index.number_of_shards", shards).put("index.number_of_replicas", 0));
    highLevelClient().indices().create(indexRequest, RequestOptions.DEFAULT);
}
Also used : CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest)

Example 5 with CreateIndexRequest

use of org.opensearch.client.indices.CreateIndexRequest in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testMultiTermVectors.

// Not entirely sure if _mtermvectors belongs to CRUD, and in the absence of a better place, will have it here
public void testMultiTermVectors() throws Exception {
    RestHighLevelClient client = highLevelClient();
    CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("user").field("type", "keyword").endObject().endObject().endObject());
    CreateIndexResponse authorsResponse = client.indices().create(authorsRequest, RequestOptions.DEFAULT);
    assertTrue(authorsResponse.isAcknowledged());
    client.index(new IndexRequest("index").id("1").source("user", "foobar"), RequestOptions.DEFAULT);
    client.index(new IndexRequest("index").id("2").source("user", "baz"), RequestOptions.DEFAULT);
    Response refreshResponse = client().performRequest(new Request("POST", "/authors/_refresh"));
    assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
    {
        // tag::multi-term-vectors-request
        // <1>
        MultiTermVectorsRequest request = new MultiTermVectorsRequest();
        TermVectorsRequest tvrequest1 = new TermVectorsRequest("authors", "1");
        tvrequest1.setFields("user");
        // <2>
        request.add(tvrequest1);
        XContentBuilder docBuilder = XContentFactory.jsonBuilder();
        docBuilder.startObject().field("user", "guest-user").endObject();
        TermVectorsRequest tvrequest2 = new TermVectorsRequest("authors", docBuilder);
        // <3>
        request.add(tvrequest2);
    // end::multi-term-vectors-request
    }
    // tag::multi-term-vectors-request-template
    TermVectorsRequest tvrequestTemplate = // <1>
    new TermVectorsRequest("authors", "fake_id");
    tvrequestTemplate.setFields("user");
    String[] ids = { "1", "2" };
    MultiTermVectorsRequest request = // <2>
    new MultiTermVectorsRequest(ids, tvrequestTemplate);
    // end::multi-term-vectors-request-template
    // tag::multi-term-vectors-execute
    MultiTermVectorsResponse response = client.mtermvectors(request, RequestOptions.DEFAULT);
    // end::multi-term-vectors-execute
    // tag::multi-term-vectors-response
    List<TermVectorsResponse> tvresponseList = // <1>
    response.getTermVectorsResponses();
    if (tvresponseList != null) {
        for (TermVectorsResponse tvresponse : tvresponseList) {
        }
    }
    // end::multi-term-vectors-response
    ActionListener<MultiTermVectorsResponse> listener;
    // tag::multi-term-vectors-execute-listener
    listener = new ActionListener<MultiTermVectorsResponse>() {

        @Override
        public void onResponse(MultiTermVectorsResponse mtvResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::multi-term-vectors-execute-listener
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::multi-term-vectors-execute-async
    client.mtermvectorsAsync(request, RequestOptions.DEFAULT, // <1>
    listener);
    // end::multi-term-vectors-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) OpenSearchException(org.opensearch.OpenSearchException) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)24 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)15 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)15 HashMap (java.util.HashMap)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 OpenSearchException (org.opensearch.OpenSearchException)10 IOException (java.io.IOException)9 ActionListener (org.opensearch.action.ActionListener)9 LatchedActionListener (org.opensearch.action.LatchedActionListener)9 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)8 Map (java.util.Map)7 IndexRequest (org.opensearch.action.index.IndexRequest)7 PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)6 BulkRequest (org.opensearch.action.bulk.BulkRequest)5 BulkResponse (org.opensearch.action.bulk.BulkResponse)5 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)5 Settings (org.opensearch.common.settings.Settings)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)4 GetIndexRequest (org.opensearch.client.indices.GetIndexRequest)4