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);
}
}
});
}
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));
}
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();
}
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"));
}
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"));
}
Aggregations