Spring?Boot?集成Elasticsearch模塊實(shí)現(xiàn)簡(jiǎn)單查詢功能
背景
項(xiàng)目中我們經(jīng)常會(huì)用搜索功能,普通的搜索我們可以用一個(gè)SQL的like也能實(shí)現(xiàn)匹配,但是搜索的核心需求是全文匹配,對(duì)于全文匹配,數(shù)據(jù)庫(kù)的索引是根本派不上用場(chǎng)的,那只能全表掃描。全表掃描的速度已經(jīng)非常慢了,還需要在每條記錄上做全文匹配,一個(gè)字一個(gè)字的比對(duì),導(dǎo)致查詢的數(shù)據(jù)更慢。所以,使用數(shù)據(jù)來(lái)做搜索,性能上完全沒(méi)法滿足要求。所以我們需要采用Elasticsearch來(lái)實(shí)現(xiàn)檢索,本文將介紹SpringBoot如何集成Elasticsearch?
系統(tǒng)集成
引入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
application.yml文件中添加ES配置
elasticsearch: rest: uris: http://localhost:9200
注意:不同的ES版本,引入jar包和配送屬性文件的方式不同,本文采用的是Spring Boot 2.2+Elasticsearch7.0的版本。
創(chuàng)建文檔實(shí)體
@Document(indexName = "product", createIndex = true) public class Product implements Serializable { private static final long serialVersionUID = -2408117939493050954L; @Id @Field(type = FieldType.Text) private String id; @Field(type = FieldType.Text) private String skuNo; @Field(type = FieldType.Text) private String tilte; @Field(type = FieldType.Double) private BigDecimal price; @Field(type = FieldType.Date, format = DateFormat.basic_date_time) private Date createDate; }
說(shuō)明:
- indexName:索引的名稱
- createIndex:ture表示如果不存在,則創(chuàng)建
- @Id:索引id
- @Field:type字段的類型,format:查詢出時(shí)間格式化類型。
接口實(shí)現(xiàn)
public interface EsProductRepository extends ElasticsearchRepository<Product,String> { List<Product> findByskuNoAndTilte(String sku,String title); }
說(shuō)明:集成ElasticsearchRepository接口,采用的是JPA的方式實(shí)現(xiàn),JPA默認(rèn)提供了相關(guān)的接口實(shí)現(xiàn)。
具體實(shí)現(xiàn)
Elasticsearch的實(shí)現(xiàn)分為基礎(chǔ)查詢和DSL查詢。
基礎(chǔ)查詢
基礎(chǔ)查詢主要包含的CRUD查詢,以及一些模糊、范圍查詢等。
新增文檔
請(qǐng)求參數(shù)
{ "id":"5", "skuNo":"sku0005", "tilte":"紅樓夢(mèng)", "price":"93.37", "createDate":"1514736000000" }
說(shuō)明:date類型傳入的參數(shù)為long類型。
Controller實(shí)現(xiàn)
@PostMapping("/addProduct") public Result addProduct(@RequestBody Product product) { esProductRepository.save(product); Result result = new Result(); result.setCode(200); result.setData(product); return result; }
返回結(jié)果
{ "data": { "id": "5", "skuNo": "sku0005", "tilte": "紅樓夢(mèng)", "price": 93.37, "createDate": "2017-12-31T16:00:00.000+00:00" }, "code": 200, "msg": null }
修改文檔
修改與新增基本相同,唯一區(qū)別為:請(qǐng)求參數(shù)傳入的Id,如果存在則為修改,否則為新增。
通過(guò)id查詢文檔信息
Controller實(shí)現(xiàn)
@GetMapping("/getProductById") public Result getProductById(@RequestParam String id) { Optional<Product> product = esProductRepository.findById(id); return Result.success(product); }
刪除文檔
Controller實(shí)現(xiàn)
@PostMapping("/deleteById") public Result deleteById(@RequestParam String id) { return Result.success(null); }
分頁(yè)查詢
Controller實(shí)現(xiàn)
@GetMapping("/getPageList") public Result getPageList(@RequestParam int pageNum,@RequestParam int pageSize) { Pageable pageable = PageRequest.of(pageNum, pageSize); Page<Product> pageList= esProductRepository.findAll(pageable); return Result.success(pageList); }
返回結(jié)果
{ "data": { "content": [ { "id": "1", "skuNo": "p0001", "tilte": null, "price": 99.9, "createDate": null }, { "id": "3", "skuNo": "p0002", "tilte": null, "price": 99.8, "createDate": null }, { "id": "4", "skuNo": "p0004", "tilte": null, "price": 110, "createDate": null }, { "id": "L1zuVYEBuycvlc7eiQ7_", "skuNo": "sku0001", "tilte": "水滸傳", "price": 93.37, "createDate": "1970-01-01T05:37:00.611+00:00" }, { "id": "5", "skuNo": "sku0005", "tilte": "紅樓夢(mèng)", "price": 93.37, "createDate": "2017-12-31T16:00:00.000+00:00" } ], "pageable": { "sort": { "sorted": false, "unsorted": true, "empty": true }, "offset": 0, "pageSize": 5, "pageNumber": 0, "paged": true, "unpaged": false }, "aggregations": null, "scrollId": null, "maxScore": 1.0, "totalPages": 1, "totalElements": 5, "number": 0, "size": 5, "sort": { "sorted": false, "unsorted": true, "empty": true }, "numberOfElements": 5, "first": true, "last": true, "empty": false }, "code": 200, "msg": null }
說(shuō)明:
- totalPages:總頁(yè)數(shù)
- totalElements:總記錄數(shù)
模糊查詢
Controller實(shí)現(xiàn)
@GetMapping("/findByTilteLike") public Result findByTilteLike(@RequestParam String key) { List<Product> products = esProductRepository.findByTilteLike(key); return Result.success(products); }
說(shuō)明:模糊查詢通過(guò)findByxxlike
范圍查詢
范圍查詢通常是指>、< >= <=等
Controller實(shí)現(xiàn)
@GetMapping("/findByPriceGreaterThanEqual") public Result findByPriceGreaterThanEqual(@RequestParam Double price) { List<Product> products = esProductRepository.findByPriceGreaterThanEqual(price); return Result.success(products); }
說(shuō)明:范圍查詢通過(guò)findByxxGreaterThanEqual
- 大于:GreaterThan
- 大于等于:GreaterThanEqual
- 小于:LessThan
- 小于等于:LessThanEqual
總結(jié)
本文講解了Spring Boot集成Elasticsearch采用的是ES模板的方式實(shí)現(xiàn)基礎(chǔ)查詢,關(guān)于相關(guān)的高級(jí)查詢將在一下章進(jìn)行講解,如有疑問(wèn)請(qǐng)隨時(shí)反饋。
到此這篇關(guān)于Spring Boot 集成Elasticsearch模塊實(shí)現(xiàn)簡(jiǎn)單查詢功能的文章就介紹到這了,更多相關(guān)Spring Boot 集成Elasticsearch查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis如何實(shí)現(xiàn)多表查詢(多對(duì)一、一對(duì)多)
這篇文章主要給大家介紹了關(guān)于MyBatis如何實(shí)現(xiàn)多表查詢(多對(duì)一、一對(duì)多)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Java中for、foreach、stream區(qū)別和性能比較詳解
for、foreach、stream都可以循環(huán)處理數(shù)據(jù),如果單純當(dāng)循環(huán)使用,for、foreach、stream哪個(gè)性能更好,這篇文章主要給大家介紹了關(guān)于Java中for、foreach、stream區(qū)別和性能的相關(guān)資料,需要的朋友可以參考下2024-03-03詳解Spring Boot 項(xiàng)目部署到heroku爬坑
這篇文章主要介紹了詳解Spring Boot 項(xiàng)目部署到heroku爬坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08解決restlet client報(bào)錯(cuò)No response.Is the cer
這篇文章主要介紹了解決restlet client報(bào)錯(cuò)No response.Is the certificate valid? Click here to check.問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01gateway、webflux、reactor-netty請(qǐng)求日志輸出方式
這篇文章主要介紹了gateway、webflux、reactor-netty請(qǐng)求日志輸出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot對(duì)Filter過(guò)濾器中的異常進(jìn)行全局處理方案詳解
這篇文章主要介紹了SpringBoot對(duì)Filter過(guò)濾器中的異常進(jìn)行全局處理,在SpringBoot中我們通過(guò) @ControllerAdvice 注解和 @ExceptionHandler注解注冊(cè)了全局異常處理器,需要的朋友可以參考下2023-09-09