1、新增索引
/**
* 获取ES连接
*
* @return
*/
public RestHighLevelClient getEsClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(esConfig.getHostName(), esConfig.getPort(), "http")));
return client;
}
值得注意的是索引名称必须小写,设置分片和副本数量、定义该索引结构,最后就是执行创建索引
/**
* 索引创建
*
* @param indexName 索引名称
* @param shardNumber 分片数量
* @param replicaNumber 副本数量
*/
public boolean createIndex(String indexName, Integer shardNumber, Integer replicaNumber) throws Exception {
CreateIndexResponse createIndexResponse = null;
RestHighLevelClient client = this.getEsClient();
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 分片和副本数量
request.settings(Settings.builder()
.put("index.number_of_shards", shardNumber)
.put("index.number_of_replicas", replicaNumber)
)