Search in sources :

Example 1 with GetIndexResponse

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

the class IndicesClientDocumentationIT method testGetIndex.

public void testGetIndex() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Settings settings = Settings.builder().put("number_of_shards", 3).build();
        String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("index").settings(settings).mapping(mappings, XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    // tag::get-index-request
    // <1>
    GetIndexRequest request = new GetIndexRequest("index");
    // end::get-index-request
    // tag::get-index-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::get-index-request-indicesOptions
    // tag::get-index-request-includeDefaults
    // <1>
    request.includeDefaults(true);
    // end::get-index-request-includeDefaults
    // tag::get-index-execute
    GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
    // end::get-index-execute
    // tag::get-index-response
    // <1>
    MappingMetadata indexMappings = getIndexResponse.getMappings().get("index");
    // <2>
    Map<String, Object> indexTypeMappings = indexMappings.getSourceAsMap();
    // <3>
    List<AliasMetadata> indexAliases = getIndexResponse.getAliases().get("index");
    // <4>
    String numberOfShardsString = getIndexResponse.getSetting("index", "index.number_of_shards");
    // <5>
    Settings indexSettings = getIndexResponse.getSettings().get("index");
    // <6>
    Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
    TimeValue time = getIndexResponse.getDefaultSettings().get("index").getAsTime("index.refresh_interval", // <7>
    null);
    // end::get-index-response
    assertEquals(Collections.singletonMap("properties", Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))), indexTypeMappings);
    assertTrue(indexAliases.isEmpty());
    assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, time);
    assertEquals("3", numberOfShardsString);
    assertEquals(Integer.valueOf(3), numberOfShards);
    // tag::get-index-execute-listener
    ActionListener<GetIndexResponse> listener = new ActionListener<GetIndexResponse>() {

        @Override
        public void onResponse(GetIndexResponse getIndexResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-index-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::get-index-execute-async
    // <1>
    client.indices().getAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-index-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) 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) GetIndexResponse(org.opensearch.client.indices.GetIndexResponse) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) TimeValue(org.opensearch.common.unit.TimeValue)

Example 2 with GetIndexResponse

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

the class IndicesClientIT method testGetIndex.

@SuppressWarnings("unchecked")
public void testGetIndex() throws IOException {
    String indexName = "get_index_test";
    Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build();
    String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}";
    createIndex(indexName, basicSettings, mappings);
    GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(false);
    GetIndexResponse getIndexResponse = execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
    // default settings should be null
    assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
    assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
    assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
    assertNotNull(getIndexResponse.getMappings().get(indexName));
    assertNotNull(getIndexResponse.getMappings().get(indexName));
    MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName);
    assertNotNull(mappingMetadata);
    assertEquals("_doc", mappingMetadata.type());
    assertEquals("{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}", mappingMetadata.source().string());
    Object o = mappingMetadata.getSourceAsMap().get("properties");
    assertThat(o, instanceOf(Map.class));
    // noinspection unchecked
    assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
    // noinspection unchecked
    Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
    assertEquals("integer", fieldMapping.get("type"));
}
Also used : GetIndexResponse(org.opensearch.client.indices.GetIndexResponse) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Map(java.util.Map) HashMap(java.util.HashMap) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 3 with GetIndexResponse

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

the class IndicesClientIT method testGetIndexWithDefaults.

@SuppressWarnings("unchecked")
public void testGetIndexWithDefaults() throws IOException {
    String indexName = "get_index_test";
    Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build();
    String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}";
    createIndex(indexName, basicSettings, mappings);
    GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(true);
    GetIndexResponse getIndexResponse = execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
    assertNotNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
    assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, getIndexResponse.getDefaultSettings().get(indexName).getAsTime("index.refresh_interval", null));
    assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
    assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
    assertNotNull(getIndexResponse.getMappings().get(indexName));
    assertNotNull(getIndexResponse.getMappings().get(indexName));
    Object o = getIndexResponse.getMappings().get(indexName).getSourceAsMap().get("properties");
    assertThat(o, instanceOf(Map.class));
    assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
    Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
    assertEquals("integer", fieldMapping.get("type"));
}
Also used : GetIndexResponse(org.opensearch.client.indices.GetIndexResponse) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

GetIndexRequest (org.opensearch.client.indices.GetIndexRequest)3 GetIndexResponse (org.opensearch.client.indices.GetIndexResponse)3 Settings (org.opensearch.common.settings.Settings)3 IndexSettings (org.opensearch.index.IndexSettings)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 MappingMetadata (org.opensearch.cluster.metadata.MappingMetadata)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)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 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 TimeValue (org.opensearch.common.unit.TimeValue)1