Spring?Boot?集成Elasticsearch模塊實現(xiàn)簡單查詢功能
背景
項目中我們經常會用搜索功能,普通的搜索我們可以用一個SQL的like也能實現(xiàn)匹配,但是搜索的核心需求是全文匹配,對于全文匹配,數(shù)據(jù)庫的索引是根本派不上用場的,那只能全表掃描。全表掃描的速度已經非常慢了,還需要在每條記錄上做全文匹配,一個字一個字的比對,導致查詢的數(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;
}返回結果
{
"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);
}返回結果
{
"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
總結
本文講解了Spring Boot集成Elasticsearch采用的是ES模板的方式實現(xiàn)基礎查詢,關于相關的高級查詢將在一下章進行講解,如有疑問請隨時反饋。
到此這篇關于Spring Boot 集成Elasticsearch模塊實現(xiàn)簡單查詢功能的文章就介紹到這了,更多相關Spring Boot 集成Elasticsearch查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11
Springboot之@Controller注解不生效問題及解決
這篇文章主要介紹了Springboot之@Controller注解不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
DynamicDataSource怎樣解決多數(shù)據(jù)源的事務問題
這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

