Skip to content

Commit f0052a5

Browse files
committed
Admin: show open and closed indices in _cat/indices
When asking for `GET /_cat/indices?v`, you can now retrieve closed indices in addition to opened ones. ``` health status index pri rep docs.count docs.deleted store.size pri.store.size yellow open .marvel-2014.05.21 1 1 8792 0 21.7mb 21.7mb close test yellow open .marvel-2014.05.22 1 1 3871 0 10.7mb 10.7mb red open .marvel-2014.05.27 1 1 ``` Closes elastic#7907. Closes elastic#7936.
1 parent 97816c1 commit f0052a5

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

rest-api-spec/test/cat.indices/10_basic.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- match:
2525
$body: |
2626
/^(green \s+
27+
(open|close) \s+
2728
index1 \s+
2829
1 \s+
2930
0 \s+

src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.elasticsearch.action.support.IndicesOptions;
3131
import org.elasticsearch.client.Client;
3232
import org.elasticsearch.client.Requests;
33+
import org.elasticsearch.cluster.metadata.IndexMetaData;
34+
import org.elasticsearch.cluster.metadata.MetaData;
3335
import org.elasticsearch.common.Strings;
3436
import org.elasticsearch.common.Table;
3537
import org.elasticsearch.common.inject.Inject;
@@ -69,8 +71,9 @@ public void doRequest(final RestRequest request, final RestChannel channel, fina
6971
client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
7072
@Override
7173
public void processResponse(final ClusterStateResponse clusterStateResponse) {
72-
final String[] concreteIndices = clusterStateResponse.getState().metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), indices);
73-
ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest(concreteIndices);
74+
final String[] concreteIndices = clusterStateResponse.getState().metaData().concreteIndices(IndicesOptions.fromOptions(false, true, true, true), indices);
75+
final String[] openIndices = clusterStateResponse.getState().metaData().concreteIndices(IndicesOptions.lenientExpandOpen(), indices);
76+
ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest(openIndices);
7477
clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
7578
client.admin().cluster().health(clusterHealthRequest, new RestActionListener<ClusterHealthResponse>(channel) {
7679
@Override
@@ -80,7 +83,7 @@ public void processResponse(final ClusterHealthResponse clusterHealthResponse) {
8083
client.admin().indices().stats(indicesStatsRequest, new RestResponseListener<IndicesStatsResponse>(channel) {
8184
@Override
8285
public RestResponse buildResponse(IndicesStatsResponse indicesStatsResponse) throws Exception {
83-
Table tab = buildTable(request, concreteIndices, clusterHealthResponse, indicesStatsResponse);
86+
Table tab = buildTable(request, concreteIndices, clusterHealthResponse, indicesStatsResponse, clusterStateResponse.getState().metaData());
8487
return RestTable.buildResponse(tab, channel);
8588
}
8689
});
@@ -96,6 +99,7 @@ Table getTableWithHeader(final RestRequest request) {
9699
Table table = new Table();
97100
table.startHeaders();
98101
table.addCell("health", "alias:h;desc:current health status");
102+
table.addCell("status", "alias:s;desc:open/close status");
99103
table.addCell("index", "alias:i,idx;desc:index name");
100104
table.addCell("pri", "alias:p,shards.primary,shardsPrimary;text-align:right;desc:number of primary shards");
101105
table.addCell("rep", "alias:r,shards.replica,shardsReplica;text-align:right;desc:number of replica shards");
@@ -284,15 +288,18 @@ Table getTableWithHeader(final RestRequest request) {
284288
return table;
285289
}
286290

287-
private Table buildTable(RestRequest request, String[] indices, ClusterHealthResponse health, IndicesStatsResponse stats) {
291+
private Table buildTable(RestRequest request, String[] indices, ClusterHealthResponse health, IndicesStatsResponse stats, MetaData indexMetaDatas) {
288292
Table table = getTableWithHeader(request);
289293

290294
for (String index : indices) {
291295
ClusterIndexHealth indexHealth = health.getIndices().get(index);
292296
IndexStats indexStats = stats.getIndices().get(index);
297+
IndexMetaData indexMetaData = indexMetaDatas.getIndices().get(index);
298+
IndexMetaData.State state = indexMetaData.getState();
293299

294300
table.startRow();
295-
table.addCell(indexHealth == null ? "red*" : indexHealth.getStatus().toString().toLowerCase(Locale.getDefault()));
301+
table.addCell(state == IndexMetaData.State.OPEN ? (indexHealth == null ? "red*" : indexHealth.getStatus().toString().toLowerCase(Locale.ROOT)) : null);
302+
table.addCell(state.toString().toLowerCase(Locale.ROOT));
296303
table.addCell(index);
297304
table.addCell(indexHealth == null ? null : indexHealth.getNumberOfShards());
298305
table.addCell(indexHealth == null ? null : indexHealth.getNumberOfReplicas());

0 commit comments

Comments
 (0)