Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ public IgnoreIndices ignoreIndices() {

@Override
public ActionRequestValidationException validate() {
if (aliases.length == 0) {
return addValidationError("No alias specified", null);
} else {
return null;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,15 @@ public ImmutableOpenMap<String, ImmutableList<AliasMetaData>> findAliases(final
return ImmutableOpenMap.of();
}

boolean matchAllAliases = aliases.length == 0;
ImmutableOpenMap.Builder<String, ImmutableList<AliasMetaData>> mapBuilder = ImmutableOpenMap.builder();
Iterable<String> intersection = HppcMaps.intersection(ObjectOpenHashSet.from(concreteIndices), indices.keys());
for (String index : intersection) {
IndexMetaData indexMetaData = indices.get(index);
List<AliasMetaData> filteredValues = Lists.newArrayList();
for (ObjectCursor<AliasMetaData> cursor : indexMetaData.getAliases().values()) {
AliasMetaData value = cursor.value;
if (Regex.simpleMatch(aliases, value.alias())) {
if (matchAllAliases || Regex.simpleMatch(aliases, value.alias())) {
filteredValues.add(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Table;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestChannel;
Expand All @@ -36,7 +35,7 @@
import org.elasticsearch.rest.action.support.RestTable;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import static org.elasticsearch.rest.RestRequest.Method.GET;

Expand All @@ -55,16 +54,13 @@ public RestAliasAction(Settings settings, Client client, RestController controll

@Override
void doRequest(final RestRequest request, final RestChannel channel) {
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
clusterStateRequest.filterMetaData(true);
clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
clusterStateRequest.filterAll().filterMetaData(false);

client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
final GetAliasesRequest getAliasesRequest = request.hasParam("alias") ?
new GetAliasesRequest(request.param("alias")) :
new GetAliasesRequest();

client.admin().indices().getAliases(getAliasesRequest, new ActionListener<GetAliasesResponse>() {
@Override
public void onResponse(ClusterStateResponse response) {
public void onResponse(GetAliasesResponse response) {
try {
Table tab = buildTable(request, response);
channel.sendResponse(RestTable.buildResponse(tab, request, channel));
Expand All @@ -86,8 +82,8 @@ public void onFailure(Throwable e) {

@Override
void documentation(StringBuilder sb) {
sb.append("/_cat_alias");
sb.append("/_cat_alias/{alias}");
sb.append("/_cat/aliases");
sb.append("/_cat/aliases/{alias}");
}

@Override
Expand All @@ -103,19 +99,14 @@ Table getTableWithHeader(RestRequest request) {
return table;
}

private Table buildTable(RestRequest request, ClusterStateResponse response) {
private Table buildTable(RestRequest request, GetAliasesResponse response) {
Table table = getTableWithHeader(request);

for (ObjectObjectCursor<String, ImmutableOpenMap<String, AliasMetaData>> cursor : response.getState().getMetaData().aliases()) {
String aliasName = cursor.key;
Iterator<ObjectObjectCursor<String,AliasMetaData>> iterator = cursor.value.iterator();
while (iterator.hasNext()) {
ObjectObjectCursor<String, AliasMetaData> iteratorCursor = iterator.next();
String indexName = iteratorCursor.key;
AliasMetaData aliasMetaData = iteratorCursor.value;

for (ObjectObjectCursor<String, List<AliasMetaData>> cursor : response.getAliases()) {
String indexName = cursor.key;
for (AliasMetaData aliasMetaData : cursor.value) {
table.startRow();
table.addCell(aliasName);
table.addCell(aliasMetaData.alias());
table.addCell(indexName);
table.addCell(aliasMetaData.filteringRequired() ? "*" : "-");
String indexRouting = Strings.hasLength(aliasMetaData.indexRouting()) ? aliasMetaData.indexRouting() : "-";
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/elasticsearch/aliases/IndexAliasesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.test.hamcrest.CollectionAssertions.hasKey;
import static org.hamcrest.Matchers.*;

/**
Expand Down Expand Up @@ -828,6 +829,20 @@ public void testRemoveAliasEmptyAliasEmptyIndex() {
}
}

@Test
public void testGetAllAliasesWorks() {
createIndex("index1");
createIndex("index2");

ensureYellow();

admin().indices().prepareAliases().addAlias("index1", "alias1").addAlias("index2", "alias2").get();

GetAliasesResponse response = admin().indices().prepareGetAliases().get();
assertThat(response.getAliases(), hasKey("index1"));
assertThat(response.getAliases(), hasKey("index1"));
}

private void assertHits(SearchHits hits, String... ids) {
assertThat(hits.totalHits(), equalTo((long) ids.length));
Set<String> hitIds = newHashSet();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.test.hamcrest;

import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.hamcrest.Matcher;

/**
* Assertions for easier handling of our custom collections,
* for example ImmutableOpenMap
*/
public class CollectionAssertions {

public static Matcher<ImmutableOpenMap> hasKey(final String key) {
return new CollectionMatchers.ImmutableOpenMapHasKeyMatcher(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.test.hamcrest;

import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
* Matchers for easier handling of our custom collections,
* for example ImmutableOpenMap
*/
public class CollectionMatchers {

public static class ImmutableOpenMapHasKeyMatcher extends TypeSafeMatcher<ImmutableOpenMap> {

private final String key;

public ImmutableOpenMapHasKeyMatcher(String key) {
this.key = key;
}

@Override
protected boolean matchesSafely(ImmutableOpenMap item) {
return item.containsKey(key);
}

@Override
public void describeMismatchSafely(final ImmutableOpenMap map, final Description mismatchDescription) {
if (map.size() == 0) {
mismatchDescription.appendText("was empty");
} else {
mismatchDescription.appendText(" was ").appendValue(map);
}
}

@Override
public void describeTo(Description description) {
description.appendText("ImmutableOpenMap should contain key " + key);
}
}

}