diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java index 9a4444ad0c..24d91fef59 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.elasticsearch; +import java.util.Properties; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.elasticsearch.client.Client; @@ -75,8 +77,9 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { } private Client createNodeClient() throws Exception { - ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put( - "http.enabled", String.valueOf(false)); + ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder() + .put("http.enabled", String.valueOf(false)) + .put(this.properties.getProperties()); Node node = new NodeBuilder().settings(settings) .clusterName(this.properties.getClusterName()).local(true).node(); this.releasable = node; @@ -85,15 +88,21 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { private Client createTransportClient() throws Exception { TransportClientFactoryBean factory = new TransportClientFactoryBean(); - factory.setClusterName(this.properties.getClusterName()); factory.setClusterNodes(this.properties.getClusterNodes()); - factory.setClientTransportSniff(this.properties.getClientTransportSniff()); + factory.setProperties(createProperties()); factory.afterPropertiesSet(); TransportClient client = factory.getObject(); this.releasable = client; return client; } + private Properties createProperties() { + Properties properties = new Properties(); + properties.put("cluster.name", this.properties.getClusterName()); + properties.putAll(this.properties.getProperties()); + return properties; + } + @Override public void destroy() throws Exception { if (this.releasable != null) { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index a15dae26b9..535fd1890f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.elasticsearch; +import java.util.HashMap; +import java.util.Map; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -40,9 +43,9 @@ public class ElasticsearchProperties { private String clusterNodes; /** - * Allow the client to sniff for other members of the cluster. + * Additional properties used to configure the client */ - private boolean clientTransportSniff = true; + private Map properties = new HashMap(); public String getClusterName() { return this.clusterName; @@ -60,12 +63,12 @@ public class ElasticsearchProperties { this.clusterNodes = clusterNodes; } - public boolean getClientTransportSniff() { - return this.clientTransportSniff; + public Map getProperties() { + return this.properties; } - public void setClientTransportSniff(boolean clientTransportSniff) { - this.clientTransportSniff = clientTransportSniff; + public void setProperties(Map properties) { + this.properties = properties; } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java index c80f2e2538..30c499ea2b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java @@ -27,7 +27,9 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -52,11 +54,16 @@ public class ElasticsearchAutoConfigurationTests { @Test public void createNodeClient() { - this.context = new AnnotationConfigApplicationContext( - PropertyPlaceholderAutoConfiguration.class, + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.data.elasticsearch.properties.foo.bar:baz"); + this.context.register(PropertyPlaceholderAutoConfiguration.class, ElasticsearchAutoConfiguration.class); + this.context.refresh(); assertEquals(1, this.context.getBeanNamesForType(Client.class).length); - assertThat(this.context.getBean(Client.class), instanceOf(NodeClient.class)); + Client client = this.context.getBean(Client.class); + assertThat(client, instanceOf(NodeClient.class)); + assertThat(((NodeClient) client).settings().get("foo.bar"), is(equalTo("baz"))); } @Test diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 82200db115..60687d9464 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -349,9 +349,9 @@ content into your application; rather pick only the properties that you need. spring.data.solr.repositories.enabled=true # if spring data repository support is enabled # ELASTICSEARCH ({sc-spring-boot-autoconfigure}/elasticsearch/ElasticsearchProperties.{sc-ext}[ElasticsearchProperties]) - spring.data.elasticsearch.client-transport-sniff=true # Allow the client to sniff for other members of the cluster spring.data.elasticsearch.cluster-name= # The cluster name (defaults to elasticsearch) spring.data.elasticsearch.cluster-nodes= # The address(es) of the server node (comma-separated; if not specified starts a client node) + spring.data.elasticsearch.properties.*= # Additional properties used to configure the client spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled # DATA REST ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration])