Search in sources :

Example 1 with CreateIndexResponse

use of org.opensearch.client.indices.CreateIndexResponse 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 CreateIndexResponse

use of org.opensearch.client.indices.CreateIndexResponse 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)

Example 3 with CreateIndexResponse

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

the class IndicesClientDocumentationIT method testIndexPutSettings.

@SuppressWarnings("unused")
public void testIndexPutSettings() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    // tag::indices-put-settings-request
    // <1>
    UpdateSettingsRequest request = new UpdateSettingsRequest("index1");
    UpdateSettingsRequest requestMultiple = // <2>
    new UpdateSettingsRequest("index1", "index2");
    // <3>
    UpdateSettingsRequest requestAll = new UpdateSettingsRequest();
    // end::indices-put-settings-request
    // tag::indices-put-settings-create-settings
    String settingKey = "index.number_of_replicas";
    int settingValue = 0;
    Settings settings = Settings.builder().put(settingKey, settingValue).build();
    // end::indices-put-settings-create-settings
    // tag::indices-put-settings-request-index-settings
    request.settings(settings);
    // end::indices-put-settings-request-index-settings
    {
        // tag::indices-put-settings-settings-builder
        Settings.Builder settingsBuilder = Settings.builder().put(settingKey, settingValue);
        // <1>
        request.settings(settingsBuilder);
    // end::indices-put-settings-settings-builder
    }
    {
        // tag::indices-put-settings-settings-map
        Map<String, Object> map = new HashMap<>();
        map.put(settingKey, settingValue);
        // <1>
        request.settings(map);
    // end::indices-put-settings-settings-map
    }
    {
        // tag::indices-put-settings-settings-source
        request.settings("{\"index.number_of_replicas\": \"2\"}", // <1>
        XContentType.JSON);
    // end::indices-put-settings-settings-source
    }
    // tag::indices-put-settings-request-preserveExisting
    // <1>
    request.setPreserveExisting(false);
    // end::indices-put-settings-request-preserveExisting
    // tag::indices-put-settings-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(2));
    // <2>
    request.timeout("2m");
    // end::indices-put-settings-request-timeout
    // tag::indices-put-settings-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::indices-put-settings-request-masterTimeout
    // tag::indices-put-settings-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::indices-put-settings-request-indicesOptions
    // tag::indices-put-settings-execute
    AcknowledgedResponse updateSettingsResponse = client.indices().putSettings(request, RequestOptions.DEFAULT);
    // end::indices-put-settings-execute
    // tag::indices-put-settings-response
    // <1>
    boolean acknowledged = updateSettingsResponse.isAcknowledged();
    // end::indices-put-settings-response
    assertTrue(acknowledged);
    // tag::indices-put-settings-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

        @Override
        public void onResponse(AcknowledgedResponse updateSettingsResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::indices-put-settings-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::indices-put-settings-execute-async
    // <1>
    client.indices().putSettingsAsync(request, RequestOptions.DEFAULT, listener);
    // end::indices-put-settings-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 4 with CreateIndexResponse

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

the class IndicesClientDocumentationIT method testGetFieldMapping.

@SuppressWarnings("unused")
public void testGetFieldMapping() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
        PutMappingRequest request = new PutMappingRequest("twitter");
        request.source("{\n" + "  \"properties\": {\n" + "    \"message\": {\n" + "      \"type\": \"text\"\n" + "    },\n" + "    \"timestamp\": {\n" + "      \"type\": \"date\"\n" + "    }\n" + "  }\n" + // <1>
        "}", XContentType.JSON);
        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        assertTrue(putMappingResponse.isAcknowledged());
    }
    // tag::get-field-mappings-request
    // <1>
    GetFieldMappingsRequest request = new GetFieldMappingsRequest();
    // <2>
    request.indices("twitter");
    // <3>
    request.fields("message", "timestamp");
    // end::get-field-mappings-request
    // tag::get-field-mappings-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::get-field-mappings-request-indicesOptions
    // tag::get-field-mappings-request-local
    // <1>
    request.local(true);
    // end::get-field-mappings-request-local
    {
        // tag::get-field-mappings-execute
        GetFieldMappingsResponse response = client.indices().getFieldMapping(request, RequestOptions.DEFAULT);
        // end::get-field-mappings-execute
        // tag::get-field-mappings-response
        final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = // <1>
        response.mappings();
        final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = // <2>
        mappings.get("twitter");
        final GetFieldMappingsResponse.FieldMappingMetadata metadata = // <3>
        fieldMappings.get("message");
        // <4>
        final String fullName = metadata.fullName();
        // <5>
        final Map<String, Object> source = metadata.sourceAsMap();
    // end::get-field-mappings-response
    }
    {
        // tag::get-field-mappings-execute-listener
        ActionListener<GetFieldMappingsResponse> listener = new ActionListener<GetFieldMappingsResponse>() {

            @Override
            public void onResponse(GetFieldMappingsResponse putMappingResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-field-mappings-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        final ActionListener<GetFieldMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
        listener = ActionListener.wrap(r -> {
            final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = r.mappings();
            final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = mappings.get("twitter");
            final GetFieldMappingsResponse.FieldMappingMetadata metadata1 = fieldMappings.get("message");
            final String fullName = metadata1.fullName();
            final Map<String, Object> source = metadata1.sourceAsMap();
            latchListener.onResponse(r);
        }, e -> {
            latchListener.onFailure(e);
            fail("should not fail");
        });
        // tag::get-field-mappings-execute-async
        // <1>
        client.indices().getFieldMappingAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-field-mappings-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) GetFieldMappingsResponse(org.opensearch.client.indices.GetFieldMappingsResponse) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with CreateIndexResponse

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

the class IndicesClientDocumentationIT method testGetMappingAsync.

public void testGetMappingAsync() throws Exception {
    final RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
        PutMappingRequest request = new PutMappingRequest("twitter");
        request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON);
        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        assertTrue(putMappingResponse.isAcknowledged());
    }
    {
        GetMappingsRequest request = new GetMappingsRequest();
        request.indices("twitter");
        // tag::get-mappings-execute-listener
        ActionListener<GetMappingsResponse> listener = new ActionListener<GetMappingsResponse>() {

            @Override
            public void onResponse(GetMappingsResponse putMappingResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-mappings-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        final ActionListener<GetMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
        listener = ActionListener.wrap(r -> {
            Map<String, MappingMetadata> allMappings = r.mappings();
            MappingMetadata indexMapping = allMappings.get("twitter");
            Map<String, Object> mapping = indexMapping.sourceAsMap();
            Map<String, String> type = new HashMap<>();
            type.put("type", "text");
            Map<String, Object> field = new HashMap<>();
            field.put("message", type);
            Map<String, Object> expected = new HashMap<>();
            expected.put("properties", field);
            assertThat(mapping, equalTo(expected));
            latchListener.onResponse(r);
        }, e -> {
            latchListener.onFailure(e);
            fail("should not fail");
        });
        // tag::get-mappings-execute-async
        // <1>
        client.indices().getMappingAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-mappings-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) GetMappingsResponse(org.opensearch.client.indices.GetMappingsResponse)

Aggregations

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