Reputation: 103
i have a requirement to configure elasticsearch 3.x with springboot, can any body help..
i tried with the following configuration
@Configuration
@EnableJpaRepositories(basePackages =
"com.demo.elastic.jparepository")
@EnableElasticsearchRepositories(basePackages =
"com.demo.elastic.repository")
public class ElasticConfiguration {
@Bean
public NodeBuilder nodeBuilder() {
return new NodeBuilder();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws IOException {
File tmpDir = File.createTempFile("elastic", Long.toString(System.nanoTime()));
System.out.println("Temp directory: " + tmpDir.getAbsolutePath());
Settings.Builder elasticsearchSettings =
Settings.settingsBuilder()
.put("http.enabled", "true") // 1
.put("index.number_of_shards", "1")
.put("path.data", new File(tmpDir, "data").getAbsolutePath()) // 2
.put("path.logs", new File(tmpDir, "logs").getAbsolutePath()) // 2
.put("path.work", new File(tmpDir, "work").getAbsolutePath()) // 2
.put("path.home", tmpDir); // 3
return new ElasticsearchTemplate(nodeBuilder()
.local(true)
.settings(elasticsearchSettings.build())
.node()
.client());
}
}
but, NodeBuilder is missing from jar.
Thanks
Upvotes: 1
Views: 1798
Reputation: 156
Try as I might, I failed to create a node locally. I separately launched server for elasticsearch and my configuration looks so:
application.yaml:
spring.data.elasticsearch:
cluster-name: elasticsearch
cluster-nodes: localhost:9300
with config:
package com.max.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.max.repository")
public class EsConfig {
}
or
package com.max.config;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.max.repository")
public class EsConfig {
@Bean
public ElasticsearchOperations elasticsearchTemplate(
@Value("${spring.data.elasticsearch.cluster-nodes}") String esNode,
@Value("${spring.data.elasticsearch.cluster-name}") String esClusterName) throws UnknownHostException {
Settings esSettings = Settings.builder()
.put("cluster.name", esClusterName)
.build();
String host = esNode.split(":")[0];
Integer port = Integer.valueOf(esNode.split(":")[1]);
return new ElasticsearchTemplate(
new PreBuiltTransportClient(esSettings)
.addTransportAddress(
new InetSocketTransportAddress(
InetAddress.getByName(host), port)));
}
}
Upvotes: 1