Springboot+ES6.8.x的mapping设置

这段代码展示了如何在Elasticsearch中检查并创建索引,包括设置分片和副本数量,定义数据刷新间隔以及映射字段类型。如果索引不存在,它将创建一个新的索引,并特别提到`CreateIndexRequest`和`GetIndexRequest`的使用。
摘要由CSDN通过智能技术生成

上代码:

 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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值