Springboot集成spring data elasticsearch過程詳解
版本對(duì)照
各版本的文檔說明:https://docs.spring.io/spring-data/elasticsearch/docs/
1、在application.yml中添加配置
spring: data: elasticsearch: repositories: enabled: true #多實(shí)例集群擴(kuò)展時(shí)需要配置以下兩個(gè)參數(shù) #cluster-name: datab-search #cluster-nodes: 127.0.0.1:9300,127.0.0.1:9301
2、添加 Maven 依賴
<!---開箱即用,版本默認(rèn)和springboot版本對(duì)應(yīng)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
3、建立實(shí)體entity
注解說明:
Spring Data通過注解來聲明字段的映射屬性,有下面的三個(gè)注解:
- @Document 作用在類,標(biāo)記實(shí)體類為文檔對(duì)象,一般有兩個(gè)屬性
- indexName:對(duì)應(yīng)索引庫名稱
- type:對(duì)應(yīng)在索引庫中的類型
- shards:分片數(shù)量,默認(rèn)5
- replicas:副本數(shù)量,默認(rèn)1
- @Id 作用在成員變量,標(biāo)記一個(gè)字段作為id主鍵
- @Field 作用在成員變量,標(biāo)記為文檔的字段,并指定字段映射屬性:
- type:字段類型,是枚舉:FieldType,可以是text、long、short、date、integer、object等
- text:存儲(chǔ)數(shù)據(jù)時(shí)候,會(huì)自動(dòng)分詞,并生成索引
- keyword:存儲(chǔ)數(shù)據(jù)時(shí)候,不會(huì)分詞建立索引
- Numerical:數(shù)值類型,分兩類
- 基本數(shù)據(jù)類型:long、interger、short、byte、double、float、half_float
- 浮點(diǎn)數(shù)的高精度類型:scaled_float
- 需要指定一個(gè)精度因子,比如10或100。elasticsearch會(huì)把真實(shí)值乘以這個(gè)因子后存儲(chǔ),取出時(shí)再還原。
- Date:日期類型
- elasticsearch可以對(duì)日期格式化為字符串存儲(chǔ),但是建議我們存儲(chǔ)為毫秒值,存儲(chǔ)為long,節(jié)省空間。
- index:是否索引,布爾類型,默認(rèn)是true
- store:是否存儲(chǔ),布爾類型,默認(rèn)是false
- analyzer:分詞器名稱,這里的ik_max_word即使用ik分詞器
示例:
@Document(indexName = "cp_doc", type = "doc", shards = 10, replicas = 0) public class CpDocument extends BaseEntity { @Id//作用在成員變量,標(biāo)記一個(gè)字段作為id主鍵 private long id ; @Field(type = FieldType.Text) private String name ; @Field(type = FieldType.Text) private String address ; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
4、編寫 Repository 訪問層
/** * 基本操作repository-curd * @author 滾動(dòng)的蛋 * */ public interface CpRepository extends ElasticsearchRepository<CpDocument, Integer> { }
5、創(chuàng)建索引+查詢示例
@RunWith(SpringRunner.class) @SpringBootTest public class ElasticSearchTest { @Autowired CpRepository cpRepository; @Autowired ElasticsearchTemplate elsTemplate;//ElasticsearchTemplate中提供了創(chuàng)建索引的API<br data-filtered="filtered"><br data-filtered="filtered"> @Test public void addIndexTest() { //創(chuàng)建索引 boolean indexRes = elsTemplate.createIndex(CpDocument.class); System.out.println("======創(chuàng)建索引結(jié)果:"+indexRes+"========="); //添加索引 CpDocument cpTest = new CpDocument(); cpTest.setId(1); cpTest.setName("阿里巴巴"); cpTest.setAddress("北京路12號(hào)"); cpRepository.save(cpTest); } @Test public void srarchTest() { //這個(gè)只做一個(gè)多字段的匹配查詢示例,其它的可以查看API文檔使用 //"name","address" 為匹配的字段 MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery("阿里巴巴","address","name");//多字段匹配QueryBuilder SearchQuery searchQuery = new NativeSearchQueryBuilder()//構(gòu)建查詢對(duì)象 .withQuery(multiMatchQuery) .withIndices("cp_doc")//索引名 .withPageable(PageRequest.of(0, 10))//分頁 .build(); Iterable<CpDocument> productDtos = cpRepository.search(searchQuery); ArrayList<CpDocument> CpDocuments = Lists.newArrayList(productDtos); for (CpDocument cpDocument : CpDocuments) { System.out.printf("企業(yè)名稱:%s,企業(yè)地址:%s\n",cpDocument.getName(),cpDocument.getAddress()); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot?實(shí)現(xiàn)Redis分布式鎖原理
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)Redis分布式鎖原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08IDEA使用Tomcat運(yùn)行web項(xiàng)目教程分享
在非Spring Boot項(xiàng)目中運(yùn)行Nacos示例,需要手動(dòng)配置Tomcat容器,本文介紹了如何在IDEA中配置Tomcat,并詳細(xì)解決了配置過程中可能遇到的異常情況,步驟包括修改IDEA項(xiàng)目結(jié)構(gòu)、添加Web模塊、配置Artifacts和Tomcat Server2024-10-10Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐
這篇文章主要介紹了Maven profile實(shí)現(xiàn)不同環(huán)境的配置管理實(shí)踐,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀
這篇文章主要介紹了C/C++中的struct結(jié)構(gòu)體詳細(xì)解讀,結(jié)構(gòu)體是由一批數(shù)據(jù)組合而成的結(jié)構(gòu)型數(shù)據(jù),組成結(jié)構(gòu)型數(shù)據(jù)的每個(gè)數(shù)據(jù)稱為結(jié)構(gòu)型數(shù)據(jù)的“成員”,其描述了一塊內(nèi)存區(qū)間的大小及意義,需要的朋友可以參考下2023-10-10在js與java中判斷json數(shù)據(jù)中是否含有某字段的案例
這篇文章主要介紹了在js與java中判斷json數(shù)據(jù)中是否含有某字段的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12HTTP 415錯(cuò)誤-Unsupported media type詳解
這篇文章主要介紹了HTTP 415錯(cuò)誤-Unsupported media type詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Eclipse中引入com.sun.image.codec.jpeg包報(bào)錯(cuò)的完美解決辦法
Java開發(fā)中對(duì)圖片的操作需要引入 com.sun.image.codec.jpeg,但有時(shí)引入這個(gè)包會(huì)報(bào)錯(cuò),利用下面的操作可以完成解決這個(gè)問題2018-02-02