IncompleteCoder
IncompleteCoder

Reputation: 153

Problems connecting to existing ElasticSearch instance in a spring boot application

I have an elasticsearch instance running locally. I have a spring boot application. In my application I have a service ServiceX which contains an elasticsearch repository which extends ElasticsearchRepository. So Service X contains YRepository extends ElasticsearchRepository

I have an elasticsearch instance running locally.

My elastic search settings are

ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.properties.http.enabled=true
spring.data.elasticsearch.properties.host = localhost
spring.data.elasticsearch.properties.port = 9300

When the application is started an elasticsearch template is created. The client that is used is a NodeClient. The settings for the NodeClient are

"http.enabled" -> "true"
"port" -> "9300"
"host" -> "localhost"
"cluster.name" -> "elasticsearch"
"node.local" -> "true"
"name" -> "Human Robot"
"path.logs" -> "C:/dev/git/xxx/logs"

The name of the elasticsearch (Human Robot in this case), does not match the local elasticsearch instance running (Nikki in this case).

It looks like it 1. creates a new instance of logstash 2. creates an embedded instance of logstash.

I have searched through a lot of information but cannot find any documentation to help.

Could people please advise about what settings to use? Thanks.

Upvotes: 1

Views: 2324

Answers (2)

IncompleteCoder
IncompleteCoder

Reputation: 153

Thanks. Would you believe I actually just started trying to use a configuration file before I saw your post. I added a configuration class

@Configuration
public class ElasticSearchConfig {

    @Bean
    public Client client() {
      TransportClient client = new TransportClient();
      TransportAddress address = new InetSocketTransportAddress(
              "localhost",9300);
      client.addTransportAddress(address);
      return client;
  }
}

And the client is now being injected into the elasticsearch template (so don't need the elasticsearchtemplate bean).

I had an error when I tried to connect but that turned out to be due to elasticsearch 2.2.0, have tried it with elasticsearch 1.7.3 and it worked so now onto the next problem!

Upvotes: 0

tbo
tbo

Reputation: 9832

I believe that you do not want to use the NodeClient but the TransportClient unless you want your application to become part of the cluster

I believe you have the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artificatId>spring-boot-starter-data-elasticsearch</artificatId>
</dependency>

then you need to create some configuration class as follows:

@Configuration
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ElasticsearchConfiguration {

    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(
                environment.getProperty("elasticsearch.host"), 
                Integer.parseInt(environment.getProperty("elasticsearch.port"))
        );
        client.addTransportAddress(address);        
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

Also check ElasticSearch section of the Spring Boot guide, and especially the section about spring.data.elasticsearch.cluster-nodes if you put multiple comma seperated list of host port it will be generated a TransportClient instead, your choice

Try it, hope it helps

Upvotes: 1

Related Questions