Search in sources :

Example 1 with PutMappingRequest

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

the class IndicesClientIT method testGetFieldMapping.

public void testGetFieldMapping() 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());
    GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest().indices(indexName).fields("field");
    GetFieldMappingsResponse getFieldMappingsResponse = execute(getFieldMappingsRequest, highLevelClient().indices()::getFieldMapping, highLevelClient().indices()::getFieldMappingAsync);
    final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappingMap = getFieldMappingsResponse.mappings().get(indexName);
    final GetFieldMappingsResponse.FieldMappingMetadata metadata = new GetFieldMappingsResponse.FieldMappingMetadata("field", new BytesArray("{\"field\":{\"type\":\"text\"}}"));
    assertThat(fieldMappingMap, equalTo(Collections.singletonMap("field", metadata)));
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) GetFieldMappingsResponse(org.opensearch.client.indices.GetFieldMappingsResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest 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 3 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest 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 4 with PutMappingRequest

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

the class IndicesRequestConverters method putMapping.

static Request putMapping(PutMappingRequest putMappingRequest) throws IOException {
    Request request = new Request(HttpPut.METHOD_NAME, RequestConverters.endpoint(putMappingRequest.indices(), "_mapping"));
    RequestConverters.Params parameters = new RequestConverters.Params();
    parameters.withTimeout(putMappingRequest.timeout());
    parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout());
    parameters.withIndicesOptions(putMappingRequest.indicesOptions());
    request.addParameters(parameters.asMap());
    request.setEntity(RequestConverters.createEntity(putMappingRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) 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) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest)

Example 5 with PutMappingRequest

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

the class IndicesClientDocumentationIT method testPutMapping.

public void testPutMapping() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    {
        // tag::put-mapping-request
        // <1>
        PutMappingRequest request = new PutMappingRequest("twitter");
        // end::put-mapping-request
        {
            // tag::put-mapping-request-source
            request.source("{\n" + "  \"properties\": {\n" + "    \"message\": {\n" + "      \"type\": \"text\"\n" + "    }\n" + "  }\n" + // <1>
            "}", XContentType.JSON);
            // end::put-mapping-request-source
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        {
            // tag::put-mapping-map
            Map<String, Object> jsonMap = new HashMap<>();
            Map<String, Object> message = new HashMap<>();
            message.put("type", "text");
            Map<String, Object> properties = new HashMap<>();
            properties.put("message", message);
            jsonMap.put("properties", properties);
            // <1>
            request.source(jsonMap);
            // end::put-mapping-map
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        {
            // tag::put-mapping-xcontent
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.startObject("properties");
                {
                    builder.startObject("message");
                    {
                        builder.field("type", "text");
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            // <1>
            request.source(builder);
            // end::put-mapping-xcontent
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        // tag::put-mapping-request-timeout
        // <1>
        request.setTimeout(TimeValue.timeValueMinutes(2));
        // end::put-mapping-request-timeout
        // tag::put-mapping-request-masterTimeout
        // <1>
        request.setMasterTimeout(TimeValue.timeValueMinutes(1));
        // end::put-mapping-request-masterTimeout
        // tag::put-mapping-execute
        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        // end::put-mapping-execute
        // tag::put-mapping-response
        // <1>
        boolean acknowledged = putMappingResponse.isAcknowledged();
        // end::put-mapping-response
        assertTrue(acknowledged);
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)9 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)7 HashMap (java.util.HashMap)6 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)6 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)4 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)4 GetFieldMappingsRequest (org.opensearch.client.indices.GetFieldMappingsRequest)4 GetMappingsRequest (org.opensearch.client.indices.GetMappingsRequest)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 OpenSearchException (org.opensearch.OpenSearchException)3 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)3 Map (java.util.Map)2 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)2 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)2