Search in sources :

Example 1 with GetMappingsResponse

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

Example 2 with GetMappingsResponse

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

the class IndicesClientIT method testGetMapping.

public void testGetMapping() throws IOException {
    String indexName = "test";
    createIndex(indexName, Settings.EMPTY);
    PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
    XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
    mappingBuilder.startObject().startObject("properties").startObject("field");
    mappingBuilder.field("type", "text");
    mappingBuilder.endObject().endObject().endObject();
    putMappingRequest.source(mappingBuilder);
    AcknowledgedResponse putMappingResponse = execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
    assertTrue(putMappingResponse.isAcknowledged());
    Map<String, Object> getIndexResponse = getAsMap(indexName);
    assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
    GetMappingsRequest request = new GetMappingsRequest().indices(indexName);
    GetMappingsResponse getMappingsResponse = execute(request, highLevelClient().indices()::getMapping, highLevelClient().indices()::getMappingAsync);
    Map<String, Object> mappings = getMappingsResponse.mappings().get(indexName).sourceAsMap();
    Map<String, String> type = new HashMap<>();
    type.put("type", "text");
    Map<String, Object> field = new HashMap<>();
    field.put("field", type);
    Map<String, Object> expected = new HashMap<>();
    expected.put("properties", field);
    assertThat(mappings, equalTo(expected));
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.opensearch.client.indices.GetMappingsResponse)

Aggregations

HashMap (java.util.HashMap)2 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 GetMappingsRequest (org.opensearch.client.indices.GetMappingsRequest)2 GetMappingsResponse (org.opensearch.client.indices.GetMappingsResponse)2 PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)1 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)1 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1