Reputation: 1
Problem in Using Opensearch with Spring Boot Application
I am trying to use OpenSearch with my SpringBoot Application. As there is no dedicated Spring Boot Starter Open Search Dependency for Open Search. I am Using Spring Boot Starter data elastic search dependency and maven dependecy for opensearch rest high level client and open search.
Later in the config class, I am Configuring my Open Search like Port and hostName using Instance of Rest High Level Client.
So Now, To perform CRUD operations in OpenSearch Using Java, We can use the In Built Methods like save, findById, findAll, deleteById etc by making our Repository Interface Extend ElasticSearchRepository.
But My Issue is while Adding Data into DB using save(T t) method like Cricketer cricketer = repo.save(Cricketer) the data is getting saved in the DB but I am getting an Exception with message "java.lang.RuntimeException: Unable to parse response body for Response{requestLine=PUT /game-index1/_doc/2009?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}"
This Issue is only with save method, for the Get Methods like findById and findAll are also working fine. Only save method has this problem.
Also while starting the spring boot application, although the application is getting started, its is stating there is an version mismatch between Elasticsearch Client and Cluster: 7.12.1 - 2.7.0, But here the problem is the cluster is of openserach and there is no 7.x version of it. Please Help me in rectifying this issue.
-> My Dependencies in pom.xml are `
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.opensearch.client</groupId>
<artifactId>opensearch-rest-high-level-client</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.opensearch</groupId>
<artifactId>opensearch</artifactId>
<version>1.1.0</version>
</dependency>`
-> -> My OpenSearch Config Class is : `
@Configuration
@EnableElasticsearchRepositories(basePackageClasses = CricketerRepo.class)
@ComponentScan(basePackageClasses = OpenSearchConfig.class)
public class OpenSearchConfig {
@Bean
public RestHighLevelClient client() {
return new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
}
}`
Note : Here I disabled the Login Part for OpenSearch for Posting purpose.
-> My Document class is :
@Document(indexName = "game-index1")
public class Cricketer {
@Id
private String cID;
private String cName;
private String cRole;
private String cCountry;
}
-> My Repository Class is :
@Repository
public interface CricketerRepo extends ElasticsearchRepository<Cricketer, String> {
}
-> My Add Method in Service Class is :
@Autowired
CricketerRepo repository;
public Cricketer addCricketer(Cricketer cricketer) {
return repository.save(cricketer); // Exception occuring at this line
}
-> Here the date I wanted to save in DB (Opensearch is getting save successfully, But as that save method should return something, Cricketer in this case. I am getting an Exception stating unable to parse Responce)
-> Exception I am getting is : java.lang.RuntimeException: Unable to parse response body for Response{requestLine=PUT /game-index1/_doc/2009?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 201 Created}
-> The Version mismatch message I am getting while starting the spring boot application is : Version mismatch in between Elasticsearch Client and Cluster: 7.12.1 - 2.7.0
Note : Even Though It is saying there is a version mismatch the spring boot application is starting fine and Also the Get Methods like findById and findAll are also working fine. Only save method has this problem.
Upvotes: 0
Views: 3334
Reputation: 31
Upgrading to 2.6.0 worked for me. Seems like _type missing from xcontentparser while reading response.
DocWriteResponse class needs _type to be non null but using 1.1.0 was leading this property to be null and it was internally leading to NPE.
Upvotes: 0