0
votes

I am using ElasticsearchCrudRepository in my Spring project to insert record into elastic search. Single record insertion is working fine but when i am trying to insert List of records then i am facing some exception from elastic search. Below is stacktrace -

org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: no requests added; at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:29) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.action.bulk.BulkRequest.validate(BulkRequest.java:600) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.action.TransportActionNodeProxy.execute(TransportActionNodeProxy.java:46) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.client.transport.TransportProxyClient.lambda$execute$0(TransportProxyClient.java:59) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:250) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:366) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:408) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) ~[elasticsearch-5.6.11.jar:5.6.11] at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) ~[elasticsearch-5.6.11.jar:5.6.11] at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.bulkIndex(ElasticsearchTemplate.java:617) ~[spring-data-elasticsearch-3.0.10.RELEASE.jar:3.0.10.RELEASE] at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.saveAll(AbstractElasticsearchRepository.java:185) ~[spring-data-elasticsearch-3.0.10.RELEASE.jar:3.0.10.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151] at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:641) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:590) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.0.10.RELEASE.jar:2.0.10.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE] at com.sun.proxy.$Proxy104.saveAll(Unknown Source) ~[na]

Below is the code which i am using to insert the data into elastic search -

public interface DataModelElasticRepository extends ElasticsearchRepository<BaseDataModel,String> {

}


@Service
public class DataModelServiceImpl implements DataModelService, Serializable {

    @Autowired
    private DataModelElasticRepository dataModelRepository;

    @Override
    public BaseDataModel save(BaseDataModel datamodel) {
        // TODO Auto-generated method stub
        return dataModelRepository.save(datamodel);
    }

    @Override
    public Iterable<BaseDataModel> save(Iterable<BaseDataModel> dataModelList) {
        return dataModelRepository.saveAll(dataModelList);
    }

}


public interface DataModelService {

    BaseDataModel save(BaseDataModel datamodel);

    Iterable<BaseDataModel> save(Iterable<BaseDataModel> dataModelList);
}

Can anyone please let me know what i am doing wrong here? Thanks.

1

1 Answers

0
votes

I am using HighLevelRestClient now, not using ElasticSearchRepository. I was facing some issues with ElasticSerachRepository given below -

Handling org.elasticsearch.client.transport.NoNodeAvailableException

and through HighLevelClient its easy to insert request in Bulk.

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-document-bulk.html