1

I want to connect my project to elastic-search. I am getting the following error:

Field repository in com.example.demo.elasticsearch.controller.Controller required a bean of type 'com.example.demo.elasticsearch.repository.CustomerRepository' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.example.demo.elasticsearch.repository.CustomerRepository' in your configuration.

2020-07-29 15:43:44.525  WARN 14432 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Unsatisfied dependency expressed through method 'springApplicationAdminRegistrar' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]

so I built some classes like the following:

Controller.java

package com.example.demo.elasticsearch.controller;

import com.example.demo.elasticsearch.model.Customer;
import com.example.demo.elasticsearch.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@org.springframework.stereotype.Controller
public class Controller {

    @Autowired
    private CustomerRepository repository;

    @PostMapping("/saveCustomer")
    public int saveCustomer(@RequestBody List<Customer> customers) {
        repository.saveAll(customers);
        return customers.size();
    }

    @GetMapping("/findAll")
    public Iterable<Customer> findAllCustomers() {
        return repository.findAll();
    }

    @GetMapping("/findByFName/{firstName}")
    public List<Customer> findByFirstName(@PathVariable String firstName) {
        return repository.findByFirstname(firstName);
    }
}

Customer.java

package com.example.demo.elasticsearch.model;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "javatechie", type = "customer", shards = 2)
@Data
@AllArgsConstructor
@NoArgsConstructor

public class Customer {

    @Id
    private String id;
    private String firstname;
    private String lastname;
    private int age;

}

CustomerRepository.java

package com.example.demo.elasticsearch.repository;


import com.example.demo.elasticsearch.model.Customer;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {

    List<Customer> findByFirstname(String firstName);

}

Build.gradle

// https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch
    compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '1.0.0.RELEASE'

 dependencies {
        classpath "org.elasticsearch.gradle:build-tools:6.5.4"
    }
Catalina
  • 663
  • 5
  • 20

1 Answers1

3

Do you have any configuration class for bean creation like following

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.elasticsearch.repository")
public class Config {
 
    @Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
 
        return RestClients.create(clientConfiguration).rest();
    }
 
    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }
}

If not, please try like this. And also check the version of spring-data-elasticsearch you are using(1.0.0.RELEASE is too old i think)

Midhun Mohan
  • 649
  • 1
  • 5
  • 13
  • Released in 2014 according to maven.org – szatkus Jul 29 '20 at 15:59
  • I have another error, if i use repository.saveAll(customers) to sotre data, i am getting the following error: `Elasticsearch exception [type=illegal_argument_exception,` any idea why? – Catalina Jul 30 '20 at 09:30
  • could you please provide the full log for that exception @Catalina – Midhun Mohan Jul 30 '20 at 09:43
  • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;]]] with root cause – Catalina Jul 30 '20 at 09:49
  • see https://stackoverflow.com/questions/63175882/trying-to-sotre-and-get-some-data-using-elasticsearch – Catalina Jul 30 '20 at 15:12