上代码:
GetIndexRequest request = new GetIndexRequest(esIndex);
//判断索引(index)是否存在
boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
if(!exists){
CreateIndexRequest createIndexRequest = new CreateIndexRequest(esIndex);
createIndexRequest.settings(Settings.builder()
.put("index.number_of_shards", 5)//分片配置
.put("index.number_of_replicas", 1)//副本配置
.put("refresh_interval","1s")//数据刷新间隔
);
//索引的mapping配置,可以先在txt里写好在复制,注意一定要 properties开头,其他属性打头会报错
createIndexRequest.mapping("{\n" +
" \"properties\": {\n" +
" \"traceId\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"platformName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"applicationName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"capacityName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"quereTime\": {\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"url\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"code\": {\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"request\": {\n" +
" \"type\": \"object\"\n" +
" },\n" +
" \"sysType\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"allTime\": {\n" +
" \"type\": \"long\"\n" +
" }\n" +
" }\n" +
"}",XContentType.JSON);
//创建索引(index)
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
重点:上述代码不能用,一般是导包导错了,
记录下包:
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;