ElasticSearch作為基于Lucene的搜索服務(wù)器,既可以作為一個(gè)獨(dú)立的服務(wù)部署,也可以簽入Web應(yīng)用中。SpringBoot作為Spring家族的全新框架,使得使用SpringBoot開(kāi)發(fā)Spring應(yīng)用變得非常簡(jiǎn)單。本文要介紹如何整合ElasticSearch與SpringBoot。
實(shí)體設(shè)計(jì):
每一本書(shū)(Book)都屬于一個(gè)分類(lèi)(Classify),都有一個(gè)作者(Author)。
生成這個(gè)三個(gè)實(shí)體類(lèi),并實(shí)現(xiàn)其get和set方法。
SpringBoot配置修改:
1.修改pom.xml文件,引入相應(yīng)依賴(lài)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< parent > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-parent </ artifactId > < version > 1.3.0.RELEASE </ version > </ parent > < dependencies > <!-- 添加 web 應(yīng)用的依賴(lài) --> < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-web</ artifactId > </ dependency > <!-- 添加 spring-data-elasticsearch的依賴(lài) --> < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-data-elasticsearch </ artifactId > </ dependency > < dependency > < groupId > org.springframework.boot</ groupId > < artifactId > spring-boot-starter-test </ artifactId > </ dependency > </ dependencies > |
2.修改配置文件application.yml。
這些配置的屬性,最終會(huì)設(shè)置到ElasticsearchProperties這個(gè)實(shí)體中。
1
2
3
4
5
6
7
8
9
|
spring: data: elasticsearch: cluster-name: #默認(rèn)為elasticsearch cluster-nodes: #配置es節(jié)點(diǎn)信息,逗號(hào)分隔,如果沒(méi)有指定,則啟動(dòng)ClientNode properties: path: logs: ./elasticsearch/log #elasticsearch日志存儲(chǔ)目錄 data: ./elasticsearch/data #elasticsearch數(shù)據(jù)存儲(chǔ)目錄 |
3.為實(shí)體添加ElascticSearch注解
Spring-data-elasticSearch提供了一些注解來(lái)幫助我們快速針對(duì)實(shí)體建立索引。
因?yàn)槲覀兿M鸄rticle作為我們文章的搜索入口,所以我們?cè)贏rticle類(lèi)上添加@Document注解。
1
2
3
4
|
@Document (indexName= "projectname" ,type= "article" ,indexStoreType= "fs" ,shards= 5 ,replicas= 1 ,refreshInterval= "-1" ) public class Book implements Serializable{ .... } |
默認(rèn)情況下,添加@Document注解會(huì)對(duì)實(shí)體中的所有屬性建立索引,由于本教程是講解如何整合,并不是專(zhuān)門(mén)講解ElasticSearch,故對(duì)于其他注解不再講解。
4.建立搜索類(lèi)
我們只要編寫(xiě)一個(gè)接口ArticleSearchRepository,來(lái)繼承Spring-data-elasticSearch提供的ElasticsearchRepository即可。
1
2
3
4
5
6
7
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import com.tianshouzhi.springbootstudy.domain.Article; //泛型的參數(shù)分別是實(shí)體類(lèi)型和主鍵類(lèi)型 public interface BookSearchRepository extends ElasticsearchRepository<Article, Long>{ } |
5.創(chuàng)建索引及搜索測(cè)試
創(chuàng)建索引
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@RunWith (SpringJUnit4ClassRunner. class ) @SpringApplicationConfiguration (classes = Application. class ) public class ElasticSearchTest { @Autowired private BookSearchRepository articleSearchRepository; @Test public void testSaveArticleIndex(){ //初始化實(shí)體類(lèi),建立索引 ...... } } |
運(yùn)行單元測(cè)試,項(xiàng)目根目錄下出現(xiàn):
說(shuō)明我們索引已經(jīng)創(chuàng)建成功。
搜索測(cè)試
1
2
3
4
5
6
7
8
9
10
|
@Test public void testSearch(){ String queryString= "springboot" ; //搜索關(guān)鍵字 QueryStringQueryBuilder builder= new QueryStringQueryBuilder(queryString); Iterable<Book> searchResult = articleSearchRepository.search(builder); Iterator<Book> iterator = searchResult.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } |
運(yùn)行單元測(cè)試,控制臺(tái)輸出
- Article [id=1, title=springboot integreate elasticsearch, abstracts=springboot integreate elasticsearch is very easy, content=elasticsearch based on lucene,spring-data-elastichsearch based on elaticsearch,this tutorial tell you how to integrete springboot with spring-data-elasticsearch, postTime=Sun Feb 21 16:01:37 CST 2016, clickCount=1,
- 搜索結(jié)果
說(shuō)明搜索成功。讀者可以嘗試其他的搜索關(guān)鍵字進(jìn)行搜索。
說(shuō)明:
以上方式是SpringBoot與ElasticSearch進(jìn)行本地整合,即將ElasticSearch內(nèi)嵌在應(yīng)用,如果我們搭建了ElasticSearch集群,只需要將配置改為如下配置即可:
1
2
3
4
|
spring: data: elasticsearch: cluster-nodes:115.28.65.149:9300 #配置es節(jié)點(diǎn)信息,逗號(hào)分隔,如果沒(méi)有指定,則啟動(dòng)ClientNode |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/wangshuang1631/article/details/72627489