Skip to content

Commit b08b3df

Browse files
committed
using JavaConfig
1 parent a45f639 commit b08b3df

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.phstudy.sample.configuration;
2+
3+
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
4+
5+
import java.io.IOException;
6+
7+
import org.elasticsearch.client.Client;
8+
import org.elasticsearch.client.transport.TransportClient;
9+
import org.elasticsearch.common.settings.ImmutableSettings;
10+
import org.elasticsearch.common.settings.Settings;
11+
import org.elasticsearch.common.transport.InetSocketTransportAddress;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
15+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
16+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
17+
18+
@Configuration
19+
@EnableElasticsearchRepositories(basePackages = "org.phstudy.sample.repository")
20+
public class ElasticsearchConfig {
21+
@Bean
22+
ElasticsearchOperations elasticsearchTemplate() throws IOException {
23+
24+
// transport client
25+
Settings settings = ImmutableSettings.settingsBuilder()
26+
.put("cluster.name", "elasticsearch")
27+
.put("username","myname")
28+
.put("password","mypassword").build();
29+
30+
Client client = new TransportClient(settings)
31+
.addTransportAddress(new InetSocketTransportAddress("192.168.73.186", 9300));
32+
33+
return new ElasticsearchTemplate(client);
34+
35+
// node client
36+
// return new ElasticsearchTemplate(nodeBuilder()
37+
// .local(true)
38+
// .settings(
39+
// ImmutableSettings.settingsBuilder()
40+
// .put("cluster.name", "elasticsearch")
41+
// .put("username", "myname")
42+
// .put("password", "mypassword").build()).node()
43+
// .client());
44+
}
45+
}

src/main/webapp/WEB-INF/spring/root-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
55

66
<!-- Root Context: defines shared resources visible to all other web components -->
7-
<import resource="elasticsearch-context.xml"/>
7+
<!-- <import resource="elasticsearch-context.xml"/> -->
88
</beans>

0 commit comments

Comments
 (0)