SpringBoot操作ElasticSsearch添加数据时报错

测试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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值