常用的elastic search java API:
获取索引列表:
public static List<String> listIndex() {
List<String> list = new ArrayList<>();
GetIndexRequestBuilder builder = client.admin().indices().prepareGetIndex();
GetIndexResponse response = builder.get();
Collections.addAll(list, response.getIndices());
return list;
}
获取索引状态列表:
public Map<String, Object> getIndexState(TransportClient client) {
Map<String, Object> indexStateMap = new HashedMap();
List<String> indexList = new ArrayList<>();
List<String> openIndexList = new ArrayList<>();
List<String> closeIndexList = new ArrayList<>();
ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
Iterator<ObjectObjectCursor<String, IndexMetaData>> indexIterator = response.getState().getMetaData().getIndices().iterator();
while (indexIterator.hasNext()) {
ObjectObjectCursor objectIndex = indexIterator.next();
String index = objectIndex.key.toString();
if (!index.contains(".")) {
IndexMetaData indexMetaData = (IndexMetaData) objectIndex.value;
String indexState = indexMetaData.getState().name();
if (indexState.contains("OPEN")) {
openIndexList.add(index);
} else closeIndexList.add(index);
indexList.add(index);
}
}
indexStateMap.put("indexList", indexList);
indexStateMap.put("openIndexList", openIndexList);
indexStateMap.put("closeIndexList", closeIndexList);
return indexStateMap;
}
注:关闭的索引,无法获取health状态。