簡單說,ElasticSearch(簡稱 ES)是搜索引擎,是結構化數據的分布式搜索引擎。Elastic Search是一個開源的,分布式,實時搜索和分析引擎。Spring Boot為Elasticsearch及Spring Data Elasticsearch提供的基于它的抽象提供了基本的配置。Spring Boot提供了一個用于聚集依賴的spring-boot-starter-data-elasticsearch 'StarterPOM'。
引入spring-boot-starter-data-elasticsearch
依賴,在pom.xml配置文件中增加如下內容(基于之前章節“Spring Boot 構建框架”中的pom.xml文件):
1
2
3
4
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> |
可以像其他Spring beans那樣注入一個自動配置的ElasticsearchTemplate或Elasticsearch客戶端實例。默認情況下,該實例將嘗試連接到一個本地內存服務器(在Elasticsearch項目中的一個NodeClient),但可以通過設置spring.data.elasticsearch.clusterNodes為一個以逗號分割的host:port列表來將其切換到一個遠程服務器(比如,TransportClient)。
1
2
3
4
5
6
7
8
9
10
|
@Component public class MyBean { private ElasticsearchTemplate template; @Autowired public MyBean(ElasticsearchTemplate template) { this .template = template; } // ... } |
如果添加一個自己的ElasticsearchTemplate類型的@Bean,它將替換默認的。
應用集成ElasticSearch案例
新建elasticsearch.properties配置文件,添加如下配置內容:
1
2
|
elasticsearch.host=localhost elasticsearch.port= 9300 |
ElasticSearch配置,讀取elasticsearch.properties配置文件信息,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Configuration @PropertySource (value = "classpath:elasticsearch.properties" ) @EnableElasticsearchRepositories (basePackages = "co.paan.repository" ) public class ElasticsearchConfiguration { @Resource private Environment environment; @Bean public Client client() { TransportClient client = new TransportClient(); TransportAddress address = new InetSocketTransportAddress(environment.getProperty( "elasticsearch.host" ), Integer.parseInt(environment.getProperty( "elasticsearch.port" ))); client.addTransportAddress(address); return client; } @Beanpublic ElasticsearchOperations elasticsearchTemplate() { return new ElasticsearchTemplate(client()); } } |
兩個實體類,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
@Document (indexName = "post" , type = "post" , shards = 1 , replicas = 0 ) public class Post { @Id private String id; private String title; @Field (type= FieldType.Nested) private List<Tag> tags; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getTitle() { return title; } public void setTitle(String title) { this .title = title; } public List<Tag> getTags() { return tags; } public void setTags(List<Tag> tags) { this .tags = tags; } } public class Tag { private String id; private String name; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } } |
數據源繼承ElasticsearchRepository類,封裝接口代碼如下:
1
2
3
|
public interface PostRepository extends ElasticsearchRepository<Post, String>{ Page<Post> findByTagsName(String name, Pageable pageable); } |
數據服務接口及實現類,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public interface PostService { Post save(Post post); Post findOne(String id); Iterable<Post> findAll(); Page<Post> findByTagsName(String tagName, PageRequest pageRequest); } @Servicepublic class PostServiceImpl implements PostService{ @Autowired private PostRepository postRepository; @Override public Post save(Post post) { postRepository.save(post); return post; } @Overridepublic Post findOne(String id) { return postRepository.findOne(id); } @Overridepublic Iterable<Post> findAll() { return postRepository.findAll(); } @Overridepublic Page<Post> findByTagsName(String tagName, PageRequest pageRequest) { return postRepository.findByTagsName(tagName, pageRequest); } } |
測試代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@Test public void testFindByTagsName() throws Exception { Tag tag = new Tag(); tag.setId( "1" ); tag.setName( "tech" ); Tag tag2 = new Tag(); tag2.setId( "2" ); tag2.setName( "elasticsearch" ); Post post = new Post(); post.setId( "1" ); post.setTitle( "Bigining with spring boot application and elasticsearch" ); post.setTags(Arrays.asList(tag, tag2)); postService.save(post); Post post2 = new Post(); post2.setId( "1" ); post2.setTitle( "Bigining with spring boot application" ); post2.setTags(Arrays.asList(tag)); postService.save(post); Page<Post> posts = postService.findByTagsName( "tech" , new PageRequest( 0 , 10 )); Page<Post> posts2 = postService.findByTagsName( "tech" , new PageRequest( 0 , 10 )); Page<Post> posts3 = postService.findByTagsName( "maz" , new PageRequest( 0 , 10 )); assertThat(posts.getTotalElements(), is(1L)); assertThat(posts2.getTotalElements(), is(1L)); assertThat(posts3.getTotalElements(), is(0L)); } |
總結
以上所述是小編給大家介紹的Spring Boot整合Elasticsearch實現全文搜索引擎案例解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.tuicool.com/articles/vqMVzqN