Skip to main content

索引操作

创建索引

try (RestHighLevelClient client = client()) {
boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
if (exists == false) {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0)
);
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
log.info("create index[{}], result[{}]", createIndexResponse.index(), createIndexResponse.isAcknowledged());
}
} catch (Exception e) {
log.error("", e);
}

删除索引

try (RestHighLevelClient client = client()) {
boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
if (exists) {
DeleteIndexRequest request = new DeleteIndexRequest(index);
AcknowledgedResponse Response = client.indices().delete(request, RequestOptions.DEFAULT);
}
} catch (Exception e) {
log.error("", e);
}