Spring?Boot?集成Elasticsearch模塊實(shí)現(xiàn)簡單查詢功能
背景
項(xiàng)目中我們經(jīng)常會(huì)用搜索功能,普通的搜索我們可以用一個(gè)SQL的like也能實(shí)現(xiàn)匹配,但是搜索的核心需求是全文匹配,對(duì)于全文匹配,數(shù)據(jù)庫的索引是根本派不上用場的,那只能全表掃描。全表掃描的速度已經(jīng)非常慢了,還需要在每條記錄上做全文匹配,一個(gè)字一個(gè)字的比對(duì),導(dǎo)致查詢的數(shù)據(jù)更慢。所以,使用數(shù)據(jù)來做搜索,性能上完全沒法滿足要求。所以我們需要采用Elasticsearch來實(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;
} 說明:
- 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);
}說明:集成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":"紅樓夢",
"price":"93.37",
"createDate":"1514736000000"
}說明: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": "紅樓夢",
"price": 93.37,
"createDate": "2017-12-31T16:00:00.000+00:00"
},
"code": 200,
"msg": null
}修改文檔
修改與新增基本相同,唯一區(qū)別為:請(qǐng)求參數(shù)傳入的Id,如果存在則為修改,否則為新增。
通過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);
}分頁查詢
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": "紅樓夢",
"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實(shí)現(xiàn)
@GetMapping("/findByTilteLike")
public Result findByTilteLike(@RequestParam String key) {
List<Product> products = esProductRepository.findByTilteLike(key);
return Result.success(products);
}說明:模糊查詢通過findByxxlike
范圍查詢
范圍查詢通常是指>、< >= <=等
Controller實(shí)現(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模板的方式實(shí)現(xiàn)基礎(chǔ)查詢,關(guān)于相關(guān)的高級(jí)查詢將在一下章進(jìn)行講解,如有疑問請(qǐng)隨時(shí)反饋。
到此這篇關(guān)于Spring Boot 集成Elasticsearch模塊實(shí)現(xiàn)簡單查詢功能的文章就介紹到這了,更多相關(guān)Spring Boot 集成Elasticsearch查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11
SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法
在實(shí)際項(xiàng)目使用中,必須要考慮服務(wù)的安全性,當(dāng)服務(wù)部署到互聯(lián)網(wǎng)以后,就要考慮服務(wù)被惡意請(qǐng)求和暴力攻擊的情況,所以本文給大家介紹了SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法,需要的朋友可以參考下2024-11-11
Java HashMap 如何正確遍歷并刪除元素的方法小結(jié)
這篇文章主要介紹了Java HashMap 如何正確遍歷并刪除元素的方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Springboot之@Controller注解不生效問題及解決
這篇文章主要介紹了Springboot之@Controller注解不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題
這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

