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
pull/2707/merge
Andy Wilkinson 10 years ago
parent 8b20403c41
commit 0a38b9b3ab

@ -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) {

@ -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<String, String> properties = new HashMap<String, String>();
public String getClusterName() {
return this.clusterName;
@ -60,12 +63,12 @@ public class ElasticsearchProperties {
this.clusterNodes = clusterNodes;
}
public boolean getClientTransportSniff() {
return this.clientTransportSniff;
public Map<String, String> getProperties() {
return this.properties;
}
public void setClientTransportSniff(boolean clientTransportSniff) {
this.clientTransportSniff = clientTransportSniff;
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}

@ -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

@ -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])

Loading…
Cancel
Save