How to connect to ElasticSearch via Spring Boot App Startup?

Am using Spring Boot along with ElasticSearch's Java Client
to connect to a local ElasticSearch 5.5.0 instance by using the
following code inside:

public void (String... args)

Here's the current code:

public class Application implements CommandLineRunner {

	Logger log = Logger.getLogger(Application.class);

	public static void main(String args[]) {, args);

	public void run(String... args) throws Exception {"Inside run() method");
		Settings settings = Settings.builder().put("", "app-cluster")
				                              .put("client.transport.sniff", true)

		TransportClient client = new PreBuiltTransportClient(settings);

		client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300));

		// Add documents
		IndexResponse response = client.prepareIndex("test", "tests")
				 			 .field("message", "Hello!")
				 			 .field("Date", new Date())


My pom.xml:





	<!-- Spring -->


	<!-- Elasticsearch -->



  1. Is there an Spring based annotation that I can use to autoload config files and start the connection to the ElasticSearch server in my Spring Boot app? Kind of like a ServletInitializer or ServletFilter?

  2. Where (what file in the codebase) would I place this and have my Application load it (connect via settings and client) upon Spring Boot Startup?

  3. Also, is there a way to set it up so an external client can call the client.disconnect()?

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