Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能
背景
項目中我們經(jīng)常會用搜索功能,普通的搜索我們可以用一個SQL的like也能實現(xiàn)匹配,但是搜索的核心需求是全文匹配,對于全文匹配,數(shù)據(jù)庫的索引是根本派不上用場的,那只能全表掃描。全表掃描的速度已經(jīng)非常慢了,還需要在每條記錄上做全文匹配,一個字一個字的比對,導致查詢的數(shù)據(jù)更慢。所以,使用數(shù)據(jù)來做搜索,性能上完全沒法滿足要求。所以我們需要采用Elasticsearch來實現(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)建文檔實體
@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; }
說明:
- indexName:索引的名稱
- createIndex:ture表示如果不存在,則創(chuàng)建
- @Id:索引id
- @Field:type字段的類型,format:查詢出時間格式化類型。
接口實現(xiàn)
public interface EsProductRepository extends ElasticsearchRepository<Product,String> { List<Product> findByskuNoAndTilte(String sku,String title); }
說明:集成ElasticsearchRepository接口,采用的是JPA的方式實現(xiàn),JPA默認提供了相關的接口實現(xiàn)。
具體實現(xiàn)
Elasticsearch的實現(xiàn)分為基礎查詢和DSL查詢。
基礎查詢
基礎查詢主要包含的CRUD查詢,以及一些模糊、范圍查詢等。
新增文檔
請求參數(shù)
{ "id":"5", "skuNo":"sku0005", "tilte":"紅樓夢", "price":"93.37", "createDate":"1514736000000" }
說明:date類型傳入的參數(shù)為long類型。
Controller實現(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": "紅樓夢", "price": 93.37, "createDate": "2017-12-31T16:00:00.000+00:00" }, "code": 200, "msg": null }
修改文檔
修改與新增基本相同,唯一區(qū)別為:請求參數(shù)傳入的Id,如果存在則為修改,否則為新增。
通過id查詢文檔信息
Controller實現(xiàn)
@GetMapping("/getProductById") public Result getProductById(@RequestParam String id) { Optional<Product> product = esProductRepository.findById(id); return Result.success(product); }
刪除文檔
Controller實現(xiàn)
@PostMapping("/deleteById") public Result deleteById(@RequestParam String id) { return Result.success(null); }
分頁查詢
Controller實現(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": "紅樓夢", "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 }
說明:
- totalPages:總頁數(shù)
- totalElements:總記錄數(shù)
模糊查詢
Controller實現(xiàn)
@GetMapping("/findByTilteLike") public Result findByTilteLike(@RequestParam String key) { List<Product> products = esProductRepository.findByTilteLike(key); return Result.success(products); }
說明:模糊查詢通過findByxxlike
范圍查詢
范圍查詢通常是指>、< >= <=等
Controller實現(xiàn)
@GetMapping("/findByPriceGreaterThanEqual") public Result findByPriceGreaterThanEqual(@RequestParam Double price) { List<Product> products = esProductRepository.findByPriceGreaterThanEqual(price); return Result.success(products); }
說明:范圍查詢通過findByxxGreaterThanEqual
- 大于:GreaterThan
- 大于等于:GreaterThanEqual
- 小于:LessThan
- 小于等于:LessThanEqual
總結(jié)
本文講解了Spring Boot集成Elasticsearch采用的是ES模板的方式實現(xiàn)基礎查詢,關于相關的高級查詢將在一下章進行講解,如有疑問請隨時反饋。
到此這篇關于Spring Boot 集成Elasticsearch模塊實現(xiàn)簡單查詢功能的文章就介紹到這了,更多相關Spring Boot 集成Elasticsearch查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatis如何實現(xiàn)多表查詢(多對一、一對多)
這篇文章主要給大家介紹了關于MyBatis如何實現(xiàn)多表查詢(多對一、一對多)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05Java中for、foreach、stream區(qū)別和性能比較詳解
for、foreach、stream都可以循環(huán)處理數(shù)據(jù),如果單純當循環(huán)使用,for、foreach、stream哪個性能更好,這篇文章主要給大家介紹了關于Java中for、foreach、stream區(qū)別和性能的相關資料,需要的朋友可以參考下2024-03-03解決restlet client報錯No response.Is the cer
這篇文章主要介紹了解決restlet client報錯No response.Is the certificate valid? Click here to check.問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01gateway、webflux、reactor-netty請求日志輸出方式
這篇文章主要介紹了gateway、webflux、reactor-netty請求日志輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot對Filter過濾器中的異常進行全局處理方案詳解
這篇文章主要介紹了SpringBoot對Filter過濾器中的異常進行全局處理,在SpringBoot中我們通過 @ControllerAdvice 注解和 @ExceptionHandler注解注冊了全局異常處理器,需要的朋友可以參考下2023-09-09