最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot?集成Elasticsearch模塊實(shí)現(xiàn)簡單查詢功能

 更新時(shí)間:2022年06月13日 15:48:43   作者:劍圣無痕  
本文講解了Spring?Boot集成Elasticsearch采用的是ES模板的方式實(shí)現(xiàn)基礎(chǔ)查詢,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

背景

項(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將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法

    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é)

    這篇文章主要介紹了Java HashMap 如何正確遍歷并刪除元素的方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • java Swing實(shí)現(xiàn)彈窗效果

    java Swing實(shí)現(xiàn)彈窗效果

    這篇文章主要為大家詳細(xì)介紹了java Swing實(shí)現(xiàn)彈窗效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Java多線程之中斷線程(Interrupt)的使用詳解

    Java多線程之中斷線程(Interrupt)的使用詳解

    interrupt字面上是中斷的意思,但在Java里Thread.interrupt()方法實(shí)際上通過某種方式通知線程,并不會(huì)直接中止該線程
    2013-05-05
  • Springboot之@Controller注解不生效問題及解決

    Springboot之@Controller注解不生效問題及解決

    這篇文章主要介紹了Springboot之@Controller注解不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 基于Java信號(hào)量解決死鎖過程解析

    基于Java信號(hào)量解決死鎖過程解析

    這篇文章主要介紹了基于Java信號(hào)量解決死鎖過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java排序算法中的快速排序算法實(shí)現(xiàn)

    Java排序算法中的快速排序算法實(shí)現(xiàn)

    這篇文章主要介紹了Java排序算法中的快速排序算法實(shí)現(xiàn),通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨(dú)立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對(duì)這兩部分?jǐn)?shù)據(jù)分別進(jìn)行快速排序,需要的朋友可以參考下
    2023-12-12
  • 淺談TreeSet中的兩種排序方式

    淺談TreeSet中的兩種排序方式

    下面小編就為大家?guī)硪黄獪\談TreeSet中的兩種排序方式。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題

    DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題

    這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

资溪县| 临澧县| 射阳县| 贡觉县| 敦化市| 蓝山县| 凤冈县| 绥阳县| 保康县| 宜春市| 贵港市| 洞头县| 当涂县| 安泽县| 定日县| 富源县| 焦作市| 南通市| 丰台区| 四会市| 茌平县| 洪洞县| 蛟河市| 天峨县| 山西省| 渝中区| 文水县| 抚顺县| 邛崃市| 临潭县| 武清区| 秭归县| 平乐县| 色达县| 元阳县| 禄劝| 西乌珠穆沁旗| 米脂县| 高雄县| 台安县| 申扎县|