Search in sources :

Example 1 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testIndexTemplateWithAliasesSource.

public void testIndexTemplateWithAliasesSource() {
    client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).setAliases("    {\n" + "        \"alias1\" : {},\n" + "        \"alias2\" : {\n" + "            \"filter\" : {\n" + "                \"type\" : {\n" + "                    \"value\" : \"type2\"\n" + "                }\n" + "            }\n" + "         },\n" + "        \"alias3\" : { \"routing\" : \"1\" }" + "    }\n").get();
    assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2"));
    ensureGreen();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(3));
    client().prepareIndex("test_index", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "value2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch("alias1").get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch("alias2").get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getType(), equalTo("type2"));
}
Also used : GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testIndexTemplateWithAliasesInSource.

public void testIndexTemplateWithAliasesInSource() {
    client().admin().indices().preparePutTemplate("template_1").setSource(new BytesArray("{\n" + "    \"template\" : \"*\",\n" + "    \"aliases\" : {\n" + "        \"my_alias\" : {\n" + "            \"filter\" : {\n" + "                \"type\" : {\n" + "                    \"value\" : \"type2\"\n" + "                }\n" + "            }\n" + "        }\n" + "    }\n" + "}"), XContentType.JSON).get();
    assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2"));
    ensureGreen();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(1));
    client().prepareIndex("test_index", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "value2").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 2L);
    searchResponse = client().prepareSearch("my_alias").get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getType(), equalTo("type2"));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testMultipleAliasesPrecedence.

public void testMultipleAliasesPrecedence() throws Exception {
    client().admin().indices().preparePutTemplate("template1").setPatterns(Collections.singletonList("*")).setOrder(0).addAlias(new Alias("alias1")).addAlias(new Alias("{index}-alias")).addAlias(new Alias("alias3").filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("test")))).addAlias(new Alias("alias4")).get();
    client().admin().indices().preparePutTemplate("template2").setPatterns(Collections.singletonList("te*")).setOrder(1).addAlias(new Alias("alias1").routing("test")).addAlias(new Alias("alias3")).get();
    assertAcked(prepareCreate("test").addAlias(new Alias("test-alias").searchRouting("test-routing")));
    ensureGreen();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().addIndices("test").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(4));
    for (AliasMetaData aliasMetaData : getAliasesResponse.getAliases().get("test")) {
        assertThat(aliasMetaData.alias(), anyOf(equalTo("alias1"), equalTo("test-alias"), equalTo("alias3"), equalTo("alias4")));
        if ("alias1".equals(aliasMetaData.alias())) {
            assertThat(aliasMetaData.indexRouting(), equalTo("test"));
            assertThat(aliasMetaData.searchRouting(), equalTo("test"));
        } else if ("alias3".equals(aliasMetaData.alias())) {
            assertThat(aliasMetaData.filter(), nullValue());
        } else if ("test-alias".equals(aliasMetaData.alias())) {
            assertThat(aliasMetaData.indexRouting(), nullValue());
            assertThat(aliasMetaData.searchRouting(), equalTo("test-routing"));
        }
    }
}
Also used : AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias)

Example 4 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project elasticsearch by elastic.

the class SimpleIndexTemplateIT method testIndexTemplateWithAliases.

public void testIndexTemplateWithAliases() throws Exception {
    client().admin().indices().preparePutTemplate("template_with_aliases").setPatterns(Collections.singletonList("te*")).addMapping("type1", "{\"type1\" : {\"properties\" : {\"value\" : {\"type\" : \"text\"}}}}", XContentType.JSON).addAlias(new Alias("simple_alias")).addAlias(new Alias("templated_alias-{index}")).addAlias(new Alias("filtered_alias").filter("{\"type\":{\"value\":\"type2\"}}")).addAlias(new Alias("complex_filtered_alias").filter(QueryBuilders.termsQuery("_type", "typeX", "typeY", "typeZ"))).get();
    assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2").addMapping("typeX").addMapping("typeY").addMapping("typeZ"));
    ensureGreen();
    client().prepareIndex("test_index", "type1", "1").setSource("field", "A value").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "B value").get();
    client().prepareIndex("test_index", "typeX", "3").setSource("field", "C value").get();
    client().prepareIndex("test_index", "typeY", "4").setSource("field", "D value").get();
    client().prepareIndex("test_index", "typeZ", "5").setSource("field", "E value").get();
    GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(4));
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 5L);
    searchResponse = client().prepareSearch("simple_alias").get();
    assertHitCount(searchResponse, 5L);
    searchResponse = client().prepareSearch("templated_alias-test_index").get();
    assertHitCount(searchResponse, 5L);
    searchResponse = client().prepareSearch("filtered_alias").get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getType(), equalTo("type2"));
    // Search the complex filter alias
    searchResponse = client().prepareSearch("complex_filtered_alias").get();
    assertHitCount(searchResponse, 3L);
    Set<String> types = new HashSet<>();
    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        types.add(searchHit.getType());
    }
    assertThat(types.size(), equalTo(3));
    assertThat(types, containsInAnyOrder("typeX", "typeY", "typeZ"));
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) HashSet(java.util.HashSet)

Example 5 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project elasticsearch by elastic.

the class RestAliasAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final GetAliasesRequest getAliasesRequest = request.hasParam("alias") ? new GetAliasesRequest(request.param("alias")) : new GetAliasesRequest();
    getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local()));
    return channel -> client.admin().indices().getAliases(getAliasesRequest, new RestResponseListener<GetAliasesResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetAliasesResponse response) throws Exception {
            Table tab = buildTable(request, response);
            return RestTable.buildResponse(tab, channel);
        }
    });
}
Also used : GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) Table(org.elasticsearch.common.Table) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) Table(org.elasticsearch.common.Table) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) RestResponse(org.elasticsearch.rest.RestResponse) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)

Aggregations

GetAliasesResponse (org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse)10 AliasMetaData (org.elasticsearch.cluster.metadata.AliasMetaData)4 List (java.util.List)3 GetAliasesRequest (org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)2 Alias (org.elasticsearch.action.admin.indices.alias.Alias)2 NodeClient (org.elasticsearch.client.node.NodeClient)2 Strings (org.elasticsearch.common.Strings)2 Settings (org.elasticsearch.common.settings.Settings)2 RestController (org.elasticsearch.rest.RestController)2 RestRequest (org.elasticsearch.rest.RestRequest)2 GET (org.elasticsearch.rest.RestRequest.Method.GET)2 RestResponse (org.elasticsearch.rest.RestResponse)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Collectors (java.util.stream.Collectors)1