Search in sources :

Example 1 with GetMappingsRequest

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

the class IndicesRequestConvertersTests method testGetMapping.

public void testGetMapping() {
    GetMappingsRequest getMappingRequest = new GetMappingsRequest();
    String[] indices = Strings.EMPTY_ARRAY;
    if (randomBoolean()) {
        indices = RequestConvertersTests.randomIndicesNames(0, 5);
        getMappingRequest.indices(indices);
    } else if (randomBoolean()) {
        getMappingRequest.indices((String[]) null);
    }
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomIndicesOptions(getMappingRequest::indicesOptions, getMappingRequest::indicesOptions, expectedParams);
    RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams);
    RequestConvertersTests.setRandomLocal(getMappingRequest::local, expectedParams);
    Request request = IndicesRequestConverters.getMappings(getMappingRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String index = String.join(",", indices);
    if (Strings.hasLength(index)) {
        endpoint.add(index);
    }
    endpoint.add("_mapping");
    Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
    Assert.assertThat(expectedParams, equalTo(request.getParameters()));
    Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}
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) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) StringJoiner(java.util.StringJoiner)

Example 2 with GetMappingsRequest

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

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

the class IndicesRequestConverters method getMappings.

static Request getMappings(GetMappingsRequest getMappingsRequest) {
    String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
    Request request = new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping"));
    RequestConverters.Params parameters = new RequestConverters.Params();
    parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
    parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
    parameters.withLocal(getMappingsRequest.local());
    request.addParameters(parameters.asMap());
    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 4 with GetMappingsRequest

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

GetMappingsRequest (org.opensearch.client.indices.GetMappingsRequest)4 PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)4 HashMap (java.util.HashMap)3 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)3 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 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)2 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)2 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)2 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)2 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)2 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)2 UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)2 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2 ValidateQueryRequest (org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest)2 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)2 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)2 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)2 CreateDataStreamRequest (org.opensearch.client.indices.CreateDataStreamRequest)2