elasticsearch-5.x JAVA API 第二部分:集群健康与段合并
1. 集群健康
首先定义一个类来存储集群健康信息
package com.zw.elasticsearch.cluster;
public class ClusterHealth {
// 集群名
private String clusterName;
// 集群中节点数目
private int number_of_node;
// 活跃总分片数目
private int number_of_activeShards;
// 活跃主分片数目
private int number_of_activePrimaryShards;
// 初始化分片数目
private int number_of_initingShards;
// 未分配分片数目
private int number_of_unassignedShards;
// 迁移中分片数目
private int number_of_relocatingShards;
// 集群健康状态值
private String clusterState;
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
public int getNumber_of_node() {
return number_of_node;
}
public void setNumber_of_node(int number_of_node) {
this.number_of_node = number_of_node;
}
public int getNumber_of_activeShards() {
return number_of_activeShards;
}
public void setNumber_of_activeShards(int number_of_activeShards) {
this.number_of_activeShards = number_of_activeShards;
}
public int getNumber_of_activePrimaryShards() {
return number_of_activePrimaryShards;
}
public void setNumber_of_activePrimaryShards(int number_of_activePrimaryShards) {
this.number_of_activePrimaryShards = number_of_activePrimaryShards;
}
public int getNumber_of_initingShards() {
return number_of_initingShards;
}
public void setNumber_of_initingShards(int number_of_initingShards) {
this.number_of_initingShards = number_of_initingShards;
}
public int getNumber_of_unassignedShards() {
return number_of_unassignedShards;
}
public void setNumber_of_unassignedShards(int number_of_unassignedShards) {
this.number_of_unassignedShards = number_of_unassignedShards;
}
public int getNumber_of_relocatingShards() {
return number_of_relocatingShards;
}
public void setNumber_of_relocatingShards(int number_of_relocatingShards) {
this.number_of_relocatingShards = number_of_relocatingShards;
}
public String getClusterState() {
return clusterState;
}
public void setClusterState(String clusterState) {
this.clusterState = clusterState;
}
public ClusterHealth(String clusterName, int number_of_node, int number_of_activeShards,
int number_of_activePrimaryShards, int number_of_initingShards, int number_of_unassignedShards,
int number_of_relocatingShards, String clusterState) {
super();
this.clusterName = clusterName;
this.number_of_node = number_of_node;
this.number_of_activeShards = number_of_activeShards;
this.number_of_activePrimaryShards = number_of_activePrimaryShards;
this.number_of_initingShards = number_of_initingShards;
this.number_of_unassignedShards = number_of_unassignedShards;
this.number_of_relocatingShards = number_of_relocatingShards;
this.clusterState = clusterState;
}
public ClusterHealth() {
super();
}
@Override
public String toString() {
return "ClusterHealth [clusterName=" + clusterName + ", number_of_node=" + number_of_node
+ ", number_of_activeShards=" + number_of_activeShards + ", number_of_activePrimaryShards="
+ number_of_activePrimaryShards + ", number_of_initingShards=" + number_of_initingShards
+ ", number_of_unassignedShards=" + number_of_unassignedShards + ", number_of_relocatingShards="
+ number_of_relocatingShards + ", clusterState=" + clusterState + "]";
}
}
下面是获取集群健康信息的代码:

2. 强制段合并
首先定义一个类来存放索引状态信息(包括索引名,有效文档数目,标记删除的文档数目,段数目等)
package com.zw.elasticsearch.cluster;
public class IndexStateInfo {
// 有效文档数目
private long documentCounts;
// 标记删除文档数目
private long deletingDocCounts;
// 段数目
private long segmentCounts;
// 索引名称
private String indexName;
public long getDocumentCounts() {
return documentCounts;
}
public void setDocumentCounts(long documentCounts) {
this.documentCounts = documentCounts;
}
public long getDeletingDocCounts() {
return deletingDocCounts;
}
public void setDeletingDocCounts(long deletingDocCounts) {
this.deletingDocCounts = deletingDocCounts;
}
public long getSegmentCounts() {
return segmentCounts;
}
public void setSegmentCounts(long segmentCounts) {
this.segmentCounts = segmentCounts;
}
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public IndexStateInfo(long documentCounts, long deletingDocCounts, long segmentCounts, String indexName) {
super();
this.documentCounts = documentCounts;
this.deletingDocCounts = deletingDocCounts;
this.segmentCounts = segmentCounts;
this.indexName = indexName;
}
public IndexStateInfo() {
super();
}
@Override
public String toString() {
return "IndexStateInfo [documentCounts=" + documentCounts + ", deletingDocCounts=" + deletingDocCounts
+ ", segmentCounts=" + segmentCounts + ", indexName=" + indexName + "]";
}
}
下面是获取索引状态信息以及强制段合并代码
获取索引信息:

强制段合并:(其中参数segmentNum为最终合并成的段数目)
