Commit 0a38b9b3 authored by Andy Wilkinson's avatar Andy Wilkinson

Enable the configuration of arbitrary Elasticsearch client properties

Previously, only a handful of properties could be set when
auto-configuring an Elasticsearch client. This commit introduces support
for configuring arbitrary properties using the
spring.data.elasticsearch.properties prefix. For example,
client.transport.sniff can be configured using
spring.data.elasticsearch.properties.client.transport.sniff.

Closes gh-1838
parent 8b20403c
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.elasticsearch; package org.springframework.boot.autoconfigure.elasticsearch;
import java.util.Properties;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
...@@ -75,8 +77,9 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { ...@@ -75,8 +77,9 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
} }
private Client createNodeClient() throws Exception { private Client createNodeClient() throws Exception {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put( ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
"http.enabled", String.valueOf(false)); .put("http.enabled", String.valueOf(false))
.put(this.properties.getProperties());
Node node = new NodeBuilder().settings(settings) Node node = new NodeBuilder().settings(settings)
.clusterName(this.properties.getClusterName()).local(true).node(); .clusterName(this.properties.getClusterName()).local(true).node();
this.releasable = node; this.releasable = node;
...@@ -85,15 +88,21 @@ public class ElasticsearchAutoConfiguration implements DisposableBean { ...@@ -85,15 +88,21 @@ public class ElasticsearchAutoConfiguration implements DisposableBean {
private Client createTransportClient() throws Exception { private Client createTransportClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean(); TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterName(this.properties.getClusterName());
factory.setClusterNodes(this.properties.getClusterNodes()); factory.setClusterNodes(this.properties.getClusterNodes());
factory.setClientTransportSniff(this.properties.getClientTransportSniff()); factory.setProperties(createProperties());
factory.afterPropertiesSet(); factory.afterPropertiesSet();
TransportClient client = factory.getObject(); TransportClient client = factory.getObject();
this.releasable = client; this.releasable = client;
return 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 @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (this.releasable != null) { if (this.releasable != null) {
......
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package org.springframework.boot.autoconfigure.elasticsearch; package org.springframework.boot.autoconfigure.elasticsearch;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
...@@ -40,9 +43,9 @@ public class ElasticsearchProperties { ...@@ -40,9 +43,9 @@ public class ElasticsearchProperties {
private String clusterNodes; 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<String, String> properties = new HashMap<String, String>();
public String getClusterName() { public String getClusterName() {
return this.clusterName; return this.clusterName;
...@@ -60,12 +63,12 @@ public class ElasticsearchProperties { ...@@ -60,12 +63,12 @@ public class ElasticsearchProperties {
this.clusterNodes = clusterNodes; this.clusterNodes = clusterNodes;
} }
public boolean getClientTransportSniff() { public Map<String, String> getProperties() {
return this.clientTransportSniff; return this.properties;
} }
public void setClientTransportSniff(boolean clientTransportSniff) { public void setProperties(Map<String, String> properties) {
this.clientTransportSniff = clientTransportSniff; this.properties = properties;
} }
} }
...@@ -27,7 +27,9 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati ...@@ -27,7 +27,9 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -52,11 +54,16 @@ public class ElasticsearchAutoConfigurationTests { ...@@ -52,11 +54,16 @@ public class ElasticsearchAutoConfigurationTests {
@Test @Test
public void createNodeClient() { public void createNodeClient() {
this.context = new AnnotationConfigApplicationContext( this.context = new AnnotationConfigApplicationContext();
PropertyPlaceholderAutoConfiguration.class, EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.foo.bar:baz");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class); ElasticsearchAutoConfiguration.class);
this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(Client.class).length); 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 @Test
......
...@@ -349,9 +349,9 @@ content into your application; rather pick only the properties that you need. ...@@ -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 spring.data.solr.repositories.enabled=true # if spring data repository support is enabled
# ELASTICSEARCH ({sc-spring-boot-autoconfigure}/elasticsearch/ElasticsearchProperties.{sc-ext}[ElasticsearchProperties]) # 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-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.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 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]) # DATA REST ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment