Akshay Bijawe Akshay Bijawe - 1 month ago 7
Java Question

Elasticsearch Transport Client connection

I’m building a Search Web Application using Servlet which connects to Elasticsearch. I have a question regarding the Transport module of Elasticsearch. I’m opening the connection to Elasticsearch using the TransportClient in the class that implements ServletContextListener. Below is the code for ElasticsearchServletContextListener class.

public class ElasticsearchContextListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Starting up!");

try {
Client client = TransportClient.builder().build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("IP-address"),9300));
//Storing the client connection in a static variable

} catch (UnknownHostException e) {
// TODO Auto-generated catch block


public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Shutting down!");

Now whenever the user searches for a query it uses the same ‘client’ connection that is initialized in the ServletContextListener class. Can the client connection handle multiple requests at the same time? Or does every user need a separate client connection to query the elasticsearch? Thank you for your assistance.


That Client instance is able to handle multiple calls and handle multiple threads. And you should have one Client instance only as it's expensive to create one.