测试es添加数据方法
public void test() throws IOException {
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("test");
client.indices().create(request, RequestOptions.DEFAULT);
// 创建对象
IndexVo indexVo = new IndexVo("666");
// test为上面创建的索引名
IndexRequest zhf_index = new IndexRequest().index("test");
// 文档编号
zhf_index.id(UUID.randomUUID().toString());
// 过期规则
zhf_index.timeout(TimeValue.timeValueSeconds(30));
zhf_index.timeout("30s");
// 将数据放入请求中(json数据)
String jsonString = JSON.toJSONString(indexVo);
System.out.println(jsonString);
IndexRequest source = zhf_index.source(JSON.toJSONString(indexVo), XContentType.JSON);
System.out.println(source);
// 客户端发送请求
client.index(zhf_index, RequestOptions.DEFAULT);
return null;
}
报错如下,可以看到第一行打印source的结果,[test][null]这里中间类型为空。以前是默认会有的类型,现在默认是为空,需要手动去指定添加的数据类型
index {[test][null][19e360d1-17c0-4b10-a600-1a8438d94280], source[{"noteName":"666"}]} --> 这一行
elAdmin- 2021-11-01 14:17:28 [http-nio-9001-exec-2] ERROR m.z.e.handler.GlobalExceptionHandler - org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: type is missing;
at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:26)
at org.elasticsearch.action.index.IndexRequest.validate(IndexRequest.java:152)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1730)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1696)
at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:928)
at me.zhengjie.rest.IndexController.test(IndexController.java:56)
at me.zhengjie.rest.IndexController$$FastClassBySpringCGLIB$$8b96d2d6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
解决方法,很简单,既然报缺少类型,那就设置一个类型给它
public void test() throws IOException {
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("test");
client.indices().create(request, RequestOptions.DEFAULT);
// 创建对象
IndexVo indexVo = new IndexVo("666");
// test为上面创建的索引名
IndexRequest zhf_index = new IndexRequest().index("test");
// 设置类型 ===================》 解决方法,看自己传入的是什么类型,我这里是json串
zhf_index.type("json");
// 文档编号
zhf_index.id(UUID.randomUUID().toString());
// 过期规则
zhf_index.timeout(TimeValue.timeValueSeconds(30));
zhf_index.timeout("30s");
// 将数据放入请求中(json数据)
String jsonString = JSON.toJSONString(indexVo);
System.out.println(jsonString);
IndexRequest source = zhf_index.source(JSON.toJSONString(indexVo), XContentType.JSON);
System.out.println(source);
// 客户端发送请求
client.index(zhf_index, RequestOptions.DEFAULT);
return null;
}