You don't need any of that code if you just use spring-boot-starter-data-elasticsearch as a dependency and set up the properties according to the doc https://docs.spring.io/spring-boot/appendix/application-properties/index.html#appendix.application-properties.data
Look for the spring.elasticsearch properties. It will greatly simplify your configuration.