文章目录
java api 创建索引
- java api 创建索引
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
/**
* 类名称:ESTest_Client
* 类描述: 创建索引
*
* @author:
* 创建时间:2022/1/11 21:05
*/
public class ESTest_Index_Create {
public static void main(String[] args) throws Exception {
// 创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
CreateIndexRequest request = new CreateIndexRequest("sysuser");
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建索引操作结果:" + acknowledged);
// 关闭ES客户端
esClient.close();
}
}
运行程序后, 有报错提示 : No log4j2 configuration file found
resources目录下, 增加配置
%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n
查询所有的索引: get请求 http://127.0.0.1:9200/_cat/indices?v
可以看到创建成功. 如果再次运行上面的程序, 可以看到提示报错: 索引已经存在.