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)
);
//定义该索引结构
Map<String, Object> properties = new HashMap<>();
Map<String, Object> mpName = new HashMap<>();
mpName.put("type", "text");
properties.put("name", mpName);
Map<String, Object> mpAge = new HashMap<>();
mpAge.put("type", "integer");
properties.put("age", mpAge);
Map<String, Object> mpEmail = new HashMap<>();
mpEmail.put("type", "keyword");
properties.put("email", mpEmail);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
// 创建索引
createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
// 执行结果
boolean result = createIndexResponse.isAcknowledged();
return result;
}
Es数据类型和数据库类型的对应关系如下:
Elasticsearch type | SQL type |
---|---|
null | null |
boolean | BOOLEAN |
byte | TINYINT |
short | SMALLINT |
integer | INTEGER |
long | BIGINT |
double | DOUBLE |
float | REAL |
half_float | FLOAT |
scaled_float | DOUBLE |
keyword | VARCHAR |
text | VARCHAR |
binary | VARBINARY |
date | TIMESTAMP |
ip | VARCHAR |
2、查询指定索引的信息
线程同步获取索引的信息会抛出一个IOException
/**
* 获取指定索引的信息
*
* @param indexName 索引名称
* @return
* @throws IOException
*/
public Map<String, Object> getIndex(String indexName) throws IOException {
Map<String, Object> mpResult = new HashMap<>();
RestHighLevelClient client = this.getEsClient();
GetIndexRequest request = new GetIndexRequest(indexName);
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
// 获取索引的mapping结构
MappingMetaData indexMappings = getIndexResponse.getMappings().get(indexName);
Map<String, Object> indexTypeMappings = indexMappings.getSourceAsMap();
// 获取索引设置的参数
Settings indexSettings = getIndexResponse.getSettings().get(indexName);
// 索引分片个数
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
mpResult.put("indexMapping", indexTypeMappings);
mpResult.put("number_of_shards", numberOfShards);
return mpResult;
}
3、删除指定索引
public void deleteIndex(String indexName) {
RestHighLevelClient client = this.getEsClient();
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
// 等待所有节点将索引删除的超时时间——2分钟
request.timeout(TimeValue.timeValueMinutes(2));
client.indices().deleteAsync(request, RequestOptions.DEFAULT, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse deleteIndexResponse) {
System.out.println("删除索引成功");
}
@Override
public void onFailure(Exception e) {
System.out.println("删除索引失败");
}
});
}
以上就是High Level REST Client操作ES索引的总结