Integration of Java API Elasticsearch

Hello everyone,

I'm currently integrating the elasticsearch java api in my api-rest project. About it, Im' using the dependency JAX-RS. All is working fine but there is a little problem which bothers me. I will explain it.
I don't know if I'm doing this in the good way but there is the scenari of a call to my api endpoint using the elasticsarch java api :

  • Calling the ressource
  • Checking the securityContext (specific to my api rest app)
  • Opening the elasticsearch client
  • Doing my query by using SearchResponse
  • Closing my client
  • Return the response

The little issue is located when I'm closing the client, indeed this step takes between 1 and 2 seconds. It's not a big deal but for what I'm using it,e.g datas vizualisation on a front-end dashboard, it's a little bit slow.
So what I'm aksing to you it's if my scenari is good , if I'm doing right to close the client at this time and if it's normal that it's take a while to close it ?

Thanks in advance and sorry for the english level, forgive the frenchy I am.


you should reuse the Elasticsearch client and not create a new one all the time. The client is also thread safe, so you can share it among all your resources.

The reason why closing takes some time is, that it uses a threadpool internally that needs to be shut down (also one of the reasons why you do not want to start a new one all the time).


Ok thanks for your quick response but there is a problem for me with your solution :

When do I decide to close the client ? Cause the dashboard I'm designing will be a part of a web interface which will be used by several clients. It won't cause any problems ?


You can close it, when the application closes.

This is a REST API, it's never completely closed cause it has to be always reachable.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.