Search in sources :

Example 6 with GetAliasesResponse

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

the class RestGetAliasesAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name");
    final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases);
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    getAliasesRequest.indices(indices);
    getAliasesRequest.indicesOptions(IndicesOptions.fromRequest(request, getAliasesRequest.indicesOptions()));
    getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local()));
    return channel -> client.admin().indices().getAliases(getAliasesRequest, new RestBuilderListener<GetAliasesResponse>(channel) {

        @Override
        public RestResponse buildResponse(GetAliasesResponse response, XContentBuilder builder) throws Exception {
            if (response.getAliases().isEmpty()) {
                if (indices.length > 0) {
                    return new BytesRestResponse(OK, builder.startObject().endObject());
                } else {
                    final String message = String.format(Locale.ROOT, "alias [%s] missing", toNamesString(getAliasesRequest.aliases()));
                    builder.startObject();
                    {
                        builder.field("error", message);
                        builder.field("status", RestStatus.NOT_FOUND.getStatus());
                    }
                    builder.endObject();
                    return new BytesRestResponse(RestStatus.NOT_FOUND, builder);
                }
            } else {
                builder.startObject();
                {
                    for (final ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) {
                        builder.startObject(entry.key);
                        {
                            builder.startObject("aliases");
                            {
                                for (final AliasMetaData alias : entry.value) {
                                    AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS);
                                }
                            }
                            builder.endObject();
                        }
                        builder.endObject();
                    }
                }
                builder.endObject();
                return new BytesRestResponse(OK, builder);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) Arrays(java.util.Arrays) GET(org.elasticsearch.rest.RestRequest.Method.GET) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Strings(org.elasticsearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponse(org.elasticsearch.rest.RestResponse) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) ToXContent(org.elasticsearch.common.xcontent.ToXContent) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Collectors(java.util.stream.Collectors) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) List(java.util.List) RestStatus(org.elasticsearch.rest.RestStatus) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) IOException(java.io.IOException) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) List(java.util.List) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 7 with GetAliasesResponse

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

the class IndexAliasesIT method testIndicesGetAliases.

public void testIndicesGetAliases() throws Exception {
    logger.info("--> creating indices [foobar, test, test123, foobarbaz, bazbar]");
    createIndex("foobar");
    createIndex("test");
    createIndex("test123");
    createIndex("foobarbaz");
    createIndex("bazbar");
    assertAcked(client().admin().indices().preparePutMapping("foobar", "test", "test123", "foobarbaz", "bazbar").setType("type").setSource("field", "type=text"));
    ensureGreen();
    logger.info("--> creating aliases [alias1, alias2]");
    assertAcked(admin().indices().prepareAliases().addAlias("foobar", "alias1").addAlias("foobar", "alias2"));
    logger.info("--> getting alias1");
    GetAliasesResponse getResponse = admin().indices().prepareGetAliases("alias1").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
    AliasesExistResponse existsResponse = admin().indices().prepareAliasesExist("alias1").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> getting all aliases that start with alias*");
    getResponse = admin().indices().prepareGetAliases("alias*").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").size(), equalTo(2));
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("alias1"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(1), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(1).alias(), equalTo("alias2"));
    assertThat(getResponse.getAliases().get("foobar").get(1).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(1).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(1).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("alias*").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> creating aliases [bar, baz, foo]");
    assertAcked(admin().indices().prepareAliases().addAlias("bazbar", "bar").addAlias("bazbar", "bac", termQuery("field", "value")).addAlias("foobar", "foo"));
    assertAcked(admin().indices().prepareAliases().addAliasAction(AliasActions.add().index("foobar").alias("bac").routing("bla")));
    logger.info("--> getting bar and baz for index bazbar");
    getResponse = admin().indices().prepareGetAliases("bar", "bac").addIndices("bazbar").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2));
    assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar"));
    assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("bar", "bac").addIndices("bazbar").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> getting *b* for index baz*");
    getResponse = admin().indices().prepareGetAliases("*b*").addIndices("baz*").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2));
    assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar"));
    assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("*b*").addIndices("baz*").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> getting *b* for index *bar");
    getResponse = admin().indices().prepareGetAliases("b*").addIndices("*bar").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(2));
    assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2));
    assertThat(getResponse.getAliases().get("bazbar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).alias(), equalTo("bac"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("term"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("field"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getFilter().string(), containsString("value"));
    assertThat(getResponse.getAliases().get("bazbar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(0).getSearchRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1), notNullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).alias(), equalTo("bar"));
    assertThat(getResponse.getAliases().get("bazbar").get(1).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("bazbar").get(1).getSearchRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("bac"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), equalTo("bla"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), equalTo("bla"));
    existsResponse = admin().indices().prepareAliasesExist("b*").addIndices("*bar").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> getting f* for index *bar");
    getResponse = admin().indices().prepareGetAliases("f*").addIndices("*bar").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("f*").addIndices("*bar").get();
    assertThat(existsResponse.exists(), equalTo(true));
    // alias at work
    logger.info("--> getting f* for index *bac");
    getResponse = admin().indices().prepareGetAliases("foo").addIndices("*bac").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("*bac").get();
    assertThat(existsResponse.exists(), equalTo(true));
    logger.info("--> getting foo for index foobar");
    getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(1));
    assertThat(getResponse.getAliases().get("foobar").get(0), notNullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).alias(), equalTo("foo"));
    assertThat(getResponse.getAliases().get("foobar").get(0).getFilter(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getIndexRouting(), nullValue());
    assertThat(getResponse.getAliases().get("foobar").get(0).getSearchRouting(), nullValue());
    existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get();
    assertThat(existsResponse.exists(), equalTo(true));
    // alias at work again
    logger.info("--> getting * for index *bac");
    getResponse = admin().indices().prepareGetAliases("*").addIndices("*bac").get();
    assertThat(getResponse, notNullValue());
    assertThat(getResponse.getAliases().size(), equalTo(2));
    assertThat(getResponse.getAliases().get("foobar").size(), equalTo(4));
    assertThat(getResponse.getAliases().get("bazbar").size(), equalTo(2));
    existsResponse = admin().indices().prepareAliasesExist("*").addIndices("*bac").get();
    assertThat(existsResponse.exists(), equalTo(true));
    assertAcked(admin().indices().prepareAliases().removeAlias("foobar", "foo"));
    getResponse = admin().indices().prepareGetAliases("foo").addIndices("foobar").get();
    assertThat(getResponse.getAliases().isEmpty(), equalTo(true));
    existsResponse = admin().indices().prepareAliasesExist("foo").addIndices("foobar").get();
    assertThat(existsResponse.exists(), equalTo(false));
}
Also used : AliasesExistResponse(org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse)

Example 8 with GetAliasesResponse

use of org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse in project graylog2-server by Graylog2.

the class Indices method aliasTarget.

@Nullable
public String aliasTarget(String alias) throws TooManyAliasesException {
    final IndicesAdminClient indicesAdminClient = c.admin().indices();
    final GetAliasesRequest request = indicesAdminClient.prepareGetAliases(alias).request();
    final GetAliasesResponse response = indicesAdminClient.getAliases(request).actionGet();
    // The ES return value of this has an awkward format: The first key of the hash is the target index. Thanks.
    final ImmutableOpenMap<String, List<AliasMetaData>> aliases = response.getAliases();
    if (aliases.size() > 1) {
        throw new TooManyAliasesException(Sets.newHashSet(aliases.keysIt()));
    }
    return aliases.isEmpty() ? null : aliases.keysIt().next();
}
Also used : GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) GetAliasesRequest(org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable)

Example 9 with GetAliasesResponse

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

the class IndexAliasesIT method testGetAllAliasesWorks.

public void testGetAllAliasesWorks() {
    createIndex("index1");
    createIndex("index2");
    assertAcked(admin().indices().prepareAliases().addAlias("index1", "alias1").addAlias("index2", "alias2"));
    GetAliasesResponse response = admin().indices().prepareGetAliases().get();
    assertThat(response.getAliases(), hasKey("index1"));
    assertThat(response.getAliases(), hasKey("index1"));
}
Also used : GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse)

Example 10 with GetAliasesResponse

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

the class IndexAliasesIT method checkAliases.

private void checkAliases() {
    GetAliasesResponse getAliasesResponse = admin().indices().prepareGetAliases("alias1").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(1));
    AliasMetaData aliasMetaData = getAliasesResponse.getAliases().get("test").get(0);
    assertThat(aliasMetaData.alias(), equalTo("alias1"));
    assertThat(aliasMetaData.filter(), nullValue());
    assertThat(aliasMetaData.indexRouting(), nullValue());
    assertThat(aliasMetaData.searchRouting(), nullValue());
    getAliasesResponse = admin().indices().prepareGetAliases("alias2").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(1));
    aliasMetaData = getAliasesResponse.getAliases().get("test").get(0);
    assertThat(aliasMetaData.alias(), equalTo("alias2"));
    assertThat(aliasMetaData.filter(), notNullValue());
    assertThat(aliasMetaData.indexRouting(), nullValue());
    assertThat(aliasMetaData.searchRouting(), nullValue());
    getAliasesResponse = admin().indices().prepareGetAliases("alias3").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(1));
    aliasMetaData = getAliasesResponse.getAliases().get("test").get(0);
    assertThat(aliasMetaData.alias(), equalTo("alias3"));
    assertThat(aliasMetaData.filter(), nullValue());
    assertThat(aliasMetaData.indexRouting(), equalTo("index"));
    assertThat(aliasMetaData.searchRouting(), equalTo("search"));
}
Also used : AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) GetAliasesResponse(org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse)

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