0

I have a interface which has function used to query ElasticSearch. It extends the ElasticsearchRepository for doing it.

public interface HouseholdRepository extends ElasticsearchRepository<SearchHouseholdESBean, String> {
    List<SearchHouseholdESBean> findByPhoneNumberAndActiveInd(String phoneNumber, String activeInd);

The problem is how do i call this in my business class where i need to get the results. This being an interface , i can't create an object of this to call the methods. Also, the implementation is implicit to the jars in the Elastic Search.

2
  • You cannot instantiate an object but you can autowire it. Commented Oct 31, 2018 at 12:57
  • @georges van I tried something like :- "@Autowired private HouseholdRepository householdRepository;" in the implementation class but the object is null. What can be the reason for this ? Commented Oct 31, 2018 at 13:07

1 Answer 1

1

To use elastichsearch repositories you must follow the next steps: 1. add annotation @EnableElasticsearchRepositories on your SpringBootApplication

@SpringBootApplication
@EnableElasticsearchRepositories
public class Application {
//...

2. Make sure that the interface HouseholdRepository is scanned by the spring-boot application. You can simple achieve this by placing it under the same root package as your Application class.

3.You will just @Autowire HouseholdRepository in your service without further changes. The idea behind spring boot data is that the code will be generated based on that interface.

OBS: make sure that you have the proper project dependencies. You should depend on spring-boot-starter-data-elasticsearch to avoid extra configuration effort.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I have following in the configuration file where i instatiate my transport client "@ EnableElasticsearchRepositories @ Configuration @ ComponentScan(basePackages = {"com.amdocs.bda.ccr"}) public class CCRServicesConfig { @ Bean public ElasticsearchOperations elasticsearchTemplate() {" Will this suffice or i have to add "@ SpringBootApplication" annotation to the same file as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.