spring-boot 集成 elasticsearch 启动报错 Validation Failed: 1: no requests added; 解决办法

avatar

错误信息如下

Caused by: org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: no requests added;
	at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:29)
	at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:600)
	at org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:46)
	at org.elasticsearch.client.transport.TransportProxyClient.lambda$execute$0(TransportProxyClient.java:59)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:250)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:366)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
	at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.bulkIndex(ElasticsearchTemplate.java:594)
	at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.saveAll(AbstractElasticsearchRepository.java:184)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
	at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy94.saveAll(Unknown Source)
	at co.yiiu.module.es.service.TopicSearchService.indexedAll(TopicSearchService.java:55)
	at co.yiiu.config.ServerRunner.run(ServerRunner.java:35)
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790)
	... 10 common frames omitted

原因是项目里代码写的问题,把要索引的内容查出来没有判断是否有数据,就直接调用es的保存方法了,原始代码如下

/**
 * 索引全部话题
 */
public void indexedAll() {
  List<TopicIndex> topicIndices = new ArrayList<>();
  List<TopicWithBLOBs> topics = topicMapper.findAll();
  topics.forEach(topic -> {
    TopicIndex topicIndex = new TopicIndex();
    topicIndex.setId(topic.getId());
    topicIndex.setTitle(topic.getTitle());
    topicIndex.setTag(topic.getTag());
    topicIndex.setContent(topic.getContent());
    topicIndex.setInTime(topic.getInTime());

    User user = userService.findById(topic.getUserId());
    topicIndex.setUsername(user.getUsername());
    topicIndices.add(topicIndex);
  });
  // 保存前先删除索引
  this.clearAll();
  topicIndexRepository.saveAll(topicIndices);
}

修改成下面这样就不报错了

/**
 * 索引全部话题
 */
public void indexedAll() {
  List<TopicIndex> topicIndices = new ArrayList<>();
  List<TopicWithBLOBs> topics = topicMapper.findAll();
  if (topics.size() > 0) { // 这里做一下判断,查出来要索引的数据有没有,有的话再调用es的save方法进行索引
    topics.forEach(topic -> {
      TopicIndex topicIndex = new TopicIndex();
      topicIndex.setId(topic.getId());
      topicIndex.setTitle(topic.getTitle());
      topicIndex.setTag(topic.getTag());
      topicIndex.setContent(topic.getContent());
      topicIndex.setInTime(topic.getInTime());

      User user = userService.findById(topic.getUserId());
      topicIndex.setUsername(user.getUsername());
      topicIndices.add(topicIndex);
    });
    // 保存前先删除索引
    this.clearAll();
    topicIndexRepository.saveAll(topicIndices);
  }
}
0 条评论

目前还没有回答,快来帮帮TA吧!

添加一条评论 请尽量发布对他人有帮助的评论

登录后可发布评论

登录 | Github登录