Search in sources :

Example 1 with Alias

use of org.opensearch.action.admin.indices.alias.Alias in project OpenSearch by opensearch-project.

the class CreateIndexRequest method innerToXContent.

public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(SETTINGS.getPreferredName());
    settings.toXContent(builder, params);
    builder.endObject();
    if (mappings != null) {
        try (InputStream stream = mappings.streamInput()) {
            builder.rawField(MAPPINGS.getPreferredName(), stream, mappingsXContentType);
        }
    }
    builder.startObject(ALIASES.getPreferredName());
    for (Alias alias : aliases) {
        alias.toXContent(builder, params);
    }
    builder.endObject();
    return builder;
}
Also used : InputStream(java.io.InputStream) Alias(org.opensearch.action.admin.indices.alias.Alias)

Example 2 with Alias

use of org.opensearch.action.admin.indices.alias.Alias in project OpenSearch by opensearch-project.

the class ResizeRequest method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    {
        builder.startObject(CreateIndexRequest.SETTINGS.getPreferredName());
        {
            settings.toXContent(builder, params);
        }
        builder.endObject();
        builder.startObject(CreateIndexRequest.ALIASES.getPreferredName());
        {
            for (Alias alias : aliases) {
                alias.toXContent(builder, params);
            }
        }
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias)

Example 3 with Alias

use of org.opensearch.action.admin.indices.alias.Alias in project OpenSearch by opensearch-project.

the class IndicesClientIT method testSplit.

@SuppressWarnings("unchecked")
public void testSplit() throws IOException {
    createIndex("source", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).put("index.number_of_routing_shards", 4).build());
    updateIndexSettings("source", Settings.builder().put("index.blocks.write", true));
    ResizeRequest resizeRequest = new ResizeRequest("target", "source");
    resizeRequest.setResizeType(ResizeType.SPLIT);
    Settings targetSettings = Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build();
    resizeRequest.setTargetIndex(new org.opensearch.action.admin.indices.create.CreateIndexRequest("target").settings(targetSettings).alias(new Alias("alias")));
    ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::split, highLevelClient().indices()::splitAsync);
    assertTrue(resizeResponse.isAcknowledged());
    assertTrue(resizeResponse.isShardsAcknowledged());
    Map<String, Object> getIndexResponse = getAsMap("target");
    Map<String, Object> indexSettings = (Map<String, Object>) XContentMapValues.extractValue("target.settings.index", getIndexResponse);
    assertNotNull(indexSettings);
    assertEquals("4", indexSettings.get("number_of_shards"));
    assertEquals("0", indexSettings.get("number_of_replicas"));
    Map<String, Object> aliasData = (Map<String, Object>) XContentMapValues.extractValue("target.aliases.alias", getIndexResponse);
    assertNotNull(aliasData);
}
Also used : ResizeResponse(org.opensearch.action.admin.indices.shrink.ResizeResponse) Alias(org.opensearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 4 with Alias

use of org.opensearch.action.admin.indices.alias.Alias in project OpenSearch by opensearch-project.

the class IndicesClientIT method testRollover.

public void testRollover() throws IOException {
    highLevelClient().indices().create(new CreateIndexRequest("test").alias(new Alias("alias")), RequestOptions.DEFAULT);
    RolloverRequest rolloverRequest = new RolloverRequest("alias", "test_new");
    rolloverRequest.addMaxIndexDocsCondition(1);
    {
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(1, conditionStatus.size());
        assertFalse(conditionStatus.get("[max_docs: 1]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    highLevelClient().index(new IndexRequest("test").id("1").source("field", "value"), RequestOptions.DEFAULT);
    highLevelClient().index(new IndexRequest("test").id("2").source("field", "value").setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT);
    // without the refresh the rollover may not happen as the number of docs seen may be off
    {
        rolloverRequest.addMaxIndexAgeCondition(new TimeValue(1));
        rolloverRequest.dryRun(true);
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertFalse(rolloverResponse.isRolledOver());
        assertTrue(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(2, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
    {
        String mappings = "{\"properties\":{\"field2\":{\"type\":\"keyword\"}}}";
        rolloverRequest.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
        rolloverRequest.dryRun(false);
        rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB));
        RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, highLevelClient().indices()::rolloverAsync);
        assertTrue(rolloverResponse.isRolledOver());
        assertFalse(rolloverResponse.isDryRun());
        Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
        assertEquals(3, conditionStatus.size());
        assertTrue(conditionStatus.get("[max_docs: 1]"));
        assertTrue(conditionStatus.get("[max_age: 1ms]"));
        assertFalse(conditionStatus.get("[max_size: 1mb]"));
        assertEquals("test", rolloverResponse.getOldIndex());
        assertEquals("test_new", rolloverResponse.getNewIndex());
    }
}
Also used : RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) Alias(org.opensearch.action.admin.indices.alias.Alias) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RolloverResponse(org.opensearch.client.indices.rollover.RolloverResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) TimeValue(org.opensearch.common.unit.TimeValue)

Example 5 with Alias

use of org.opensearch.action.admin.indices.alias.Alias in project OpenSearch by opensearch-project.

the class IndicesClientIT method testPutTemplate.

@SuppressWarnings("unchecked")
public void testPutTemplate() throws Exception {
    PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{ \"properties\": { \"host_name\": { \"type\": \"keyword\" } } }", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
    AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
    assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));
    Map<String, Object> templates = getAsMap("/_template/my-template");
    assertThat(templates.keySet(), hasSize(1));
    assertThat(extractValue("my-template.order", templates), equalTo(10));
    assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*"));
    assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
    assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
    assertThat(extractValue("my-template.mappings.properties.host_name.type", templates), equalTo("keyword"));
    assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc"));
    assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

Alias (org.opensearch.action.admin.indices.alias.Alias)110 HashMap (java.util.HashMap)20 Map (java.util.Map)19 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ClusterState (org.opensearch.cluster.ClusterState)13 Settings (org.opensearch.common.settings.Settings)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)12 IOException (java.io.IOException)11 MultiGetResponse (org.opensearch.action.get.MultiGetResponse)10 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 OpenSearchException (org.opensearch.OpenSearchException)7 GetResponse (org.opensearch.action.get.GetResponse)7 IndexSettings (org.opensearch.index.IndexSettings)7 ResizeRequest (org.opensearch.action.admin.indices.shrink.ResizeRequest)6 ResizeResponse (org.opensearch.action.admin.indices.shrink.ResizeResponse)6 ExplainResponse (org.opensearch.action.explain.ExplainResponse)6 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)6 List (java.util.List)5