Skip to content

Commit 0a38b9b

Browse files
committed
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
1 parent 8b20403 commit 0a38b9b

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfiguration.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.elasticsearch;
1818

19+
import java.util.Properties;
20+
1921
import org.apache.commons.logging.Log;
2022
import org.apache.commons.logging.LogFactory;
2123
import org.elasticsearch.client.Client;
@@ -75,8 +77,9 @@ private Client createClient() throws Exception {
7577
}
7678

7779
private Client createNodeClient() throws Exception {
78-
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put(
79-
"http.enabled", String.valueOf(false));
80+
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
81+
.put("http.enabled", String.valueOf(false))
82+
.put(this.properties.getProperties());
8083
Node node = new NodeBuilder().settings(settings)
8184
.clusterName(this.properties.getClusterName()).local(true).node();
8285
this.releasable = node;
@@ -85,15 +88,21 @@ private Client createNodeClient() throws Exception {
8588

8689
private Client createTransportClient() throws Exception {
8790
TransportClientFactoryBean factory = new TransportClientFactoryBean();
88-
factory.setClusterName(this.properties.getClusterName());
8991
factory.setClusterNodes(this.properties.getClusterNodes());
90-
factory.setClientTransportSniff(this.properties.getClientTransportSniff());
92+
factory.setProperties(createProperties());
9193
factory.afterPropertiesSet();
9294
TransportClient client = factory.getObject();
9395
this.releasable = client;
9496
return client;
9597
}
9698

99+
private Properties createProperties() {
100+
Properties properties = new Properties();
101+
properties.put("cluster.name", this.properties.getClusterName());
102+
properties.putAll(this.properties.getProperties());
103+
return properties;
104+
}
105+
97106
@Override
98107
public void destroy() throws Exception {
99108
if (this.releasable != null) {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.elasticsearch;
1818

19+
import java.util.HashMap;
20+
import java.util.Map;
21+
1922
import org.springframework.boot.context.properties.ConfigurationProperties;
2023

2124
/**
@@ -40,9 +43,9 @@ public class ElasticsearchProperties {
4043
private String clusterNodes;
4144

4245
/**
43-
* Allow the client to sniff for other members of the cluster.
46+
* Additional properties used to configure the client
4447
*/
45-
private boolean clientTransportSniff = true;
48+
private Map<String, String> properties = new HashMap<String, String>();
4649

4750
public String getClusterName() {
4851
return this.clusterName;
@@ -60,12 +63,12 @@ public void setClusterNodes(String clusterNodes) {
6063
this.clusterNodes = clusterNodes;
6164
}
6265

63-
public boolean getClientTransportSniff() {
64-
return this.clientTransportSniff;
66+
public Map<String, String> getProperties() {
67+
return this.properties;
6568
}
6669

67-
public void setClientTransportSniff(boolean clientTransportSniff) {
68-
this.clientTransportSniff = clientTransportSniff;
70+
public void setProperties(Map<String, String> properties) {
71+
this.properties = properties;
6972
}
7073

7174
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import org.springframework.boot.test.EnvironmentTestUtils;
2828
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2929

30+
import static org.hamcrest.Matchers.equalTo;
3031
import static org.hamcrest.Matchers.instanceOf;
32+
import static org.hamcrest.Matchers.is;
3133
import static org.junit.Assert.assertEquals;
3234
import static org.junit.Assert.assertThat;
3335

@@ -52,11 +54,16 @@ public void close() {
5254

5355
@Test
5456
public void createNodeClient() {
55-
this.context = new AnnotationConfigApplicationContext(
56-
PropertyPlaceholderAutoConfiguration.class,
57+
this.context = new AnnotationConfigApplicationContext();
58+
EnvironmentTestUtils.addEnvironment(this.context,
59+
"spring.data.elasticsearch.properties.foo.bar:baz");
60+
this.context.register(PropertyPlaceholderAutoConfiguration.class,
5761
ElasticsearchAutoConfiguration.class);
62+
this.context.refresh();
5863
assertEquals(1, this.context.getBeanNamesForType(Client.class).length);
59-
assertThat(this.context.getBean(Client.class), instanceOf(NodeClient.class));
64+
Client client = this.context.getBean(Client.class);
65+
assertThat(client, instanceOf(NodeClient.class));
66+
assertThat(((NodeClient) client).settings().get("foo.bar"), is(equalTo("baz")));
6067
}
6168

6269
@Test

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ content into your application; rather pick only the properties that you need.
349349
spring.data.solr.repositories.enabled=true # if spring data repository support is enabled
350350
351351
# ELASTICSEARCH ({sc-spring-boot-autoconfigure}/elasticsearch/ElasticsearchProperties.{sc-ext}[ElasticsearchProperties])
352-
spring.data.elasticsearch.client-transport-sniff=true # Allow the client to sniff for other members of the cluster
353352
spring.data.elasticsearch.cluster-name= # The cluster name (defaults to elasticsearch)
354353
spring.data.elasticsearch.cluster-nodes= # The address(es) of the server node (comma-separated; if not specified starts a client node)
354+
spring.data.elasticsearch.properties.*= # Additional properties used to configure the client
355355
spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled
356356
357357
# DATA REST ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration])

0 commit comments

Comments
 (0)