成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入淺出spring-data-elasticsearch - 基本案例詳解

企業(yè)動(dòng)態(tài)
spring-data-elasticsearch-crud 的工程,介紹 Spring Data Elasticsearch 簡單的 ES 操作。Spring Data Elasticsearch 可以跟 JPA 進(jìn)行類比。其使用方法也很簡單。

本文提綱

[[194551]]

一、spring-data-elasticsearch-crud 的工程介紹

二、運(yùn)行 spring-data-elasticsearch-crud 工程

三、spring-data-elasticsearch-crud 工程代碼詳解

一、spring-data-elasticsearch-crud 的工程介紹

spring-data-elasticsearch-crud 的工程,介紹 Spring Data Elasticsearch 簡單的 ES 操作。Spring Data Elasticsearch 可以跟 JPA 進(jìn)行類比。其使用方法也很簡單。

二、運(yùn)行 spring-data-elasticsearch-crud 工程

注意的是這里使用的是 ElasticSearch 2.3.2。是因?yàn)榘姹緦?yīng)關(guān)系 https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix;

  • Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
  • x <= 1.3.5 y <= 1.3.4 z <= 1.7.2*
  • x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**
  • * - 只需要你修改下對應(yīng)的 pom 文件版本號
  • ** - 下一個(gè) ES 的版本會有重大的更新

1. 后臺起守護(hù)線程啟動(dòng) Elasticsearch

  1. cd elasticsearch-2.3.2/  
  2. ./bin/elasticsearch -d 

git clone 下載工程 springboot-elasticsearch ,項(xiàng)目地址見 GitHub - https://github.com/JeffLi1993/ ... ample。

下面開始運(yùn)行工程步驟(Quick Start):

2. 項(xiàng)目結(jié)構(gòu)介紹

  1. org.spring.springboot.controller - Controller 層 
  2. org.spring.springboot.repository - ES 數(shù)據(jù)操作層 
  3. org.spring.springboot.domain - 實(shí)體類 
  4. org.spring.springboot.service - ES 業(yè)務(wù)邏輯層 
  5. Application - 應(yīng)用啟動(dòng)類 
  6. application.properties - 應(yīng)用配置文件,應(yīng)用啟動(dòng)會自動(dòng)讀取配置 

本地啟動(dòng)的 ES ,就不需要改配置文件了。如果連測試 ES 服務(wù)地址,需要修改相應(yīng)配置

3.編譯工程

在項(xiàng)目根目錄 spring-data-elasticsearch-crud,運(yùn)行 maven 指令:

  1. mvn clean install 

4.運(yùn)行工程

右鍵運(yùn)行 Application 應(yīng)用啟動(dòng)類(位置:/springboot-learning-example/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java)的 main 函數(shù),這樣就成功啟動(dòng)了 springboot-elasticsearch 案例。

用 Postman 工具新增兩個(gè)城市

a. 新增城市信息

  1. POST http://127.0.0.1:8080/api/city 
  2.  
  3. {     
  4.     "id”:"1", 
  5.     "score":"5"
  6.     "name":"上海"
  7.     "description":"上海是個(gè)熱城市" 
  1. POST http://127.0.0.1:8080/api/city 
  2.  
  3. {     
  4.     "id":"2",     
  5.     "score”:"4", 
  6.     "name”:”溫嶺"
  7.     "description":”溫嶺是個(gè)沿海城市" 
  8.  

可以打開 ES 可視化工具 head 插件:http://localhost:9200/_plugin/head/:

(如果不知道怎么安裝,請查閱 《Elasticsearch 和插件 elasticsearch-head 安裝詳解》 http://www.bysocket.com/?p=1744 。)

在「數(shù)據(jù)瀏覽」tab,可以查閱到 ES 中數(shù)據(jù)是否被插入,插入后的數(shù)據(jù)格式如下:

  1. "_index""cityindex"
  2. "_type""city"
  3. "_id""1"
  4. "_version": 1, 
  5. "_score": 1, 
  6. "_source": {   
  7.     "id":"2",     
  8.     "score”:"4", 
  9.     "name”:”溫嶺"
  10.     "description":”溫嶺是個(gè)沿海城市" 

下面是基本查詢語句的接口:

a. 普通查詢,查詢城市描述

  1. GET http://localhost:8080/api/city ... on%3D溫嶺 

返回 JSON 如下:

  1.     {         
  2.     "id": 2,         
  3.     "name""溫嶺",         
  4.     "description""溫嶺是個(gè)沿海城市",         
  5.     "score": 4 
  6.     } 

b. AND 語句查詢

  1. GET http://localhost:8080/api/city ... on%3D溫嶺&score=4 

返回 JSON 如下:

  1.     {         
  2. "id": 2,         
  3. "name""溫嶺",         
  4. "description""溫嶺是個(gè)沿海城市",         
  5. "score": 4 
  6.     } 

如果換成 score=5 ,就沒有結(jié)果了。

c. OR 語句查詢

  1. GET http://localhost:8080/api/city ... on%3D上海&score=4 

返回 JSON 如下:

  1.     {         
  2. "id": 2,         
  3. "name""溫嶺",         
  4. "description""溫嶺是個(gè)沿海城市",         
  5. "score": 4 
  6.     }, 
  7.     {         
  8. "id": 1,         
  9. "name""上海",         
  10. "description""上海是個(gè)好城市",         
  11. "score": 3 
  12.     } 

d. NOT 語句查詢

  1. GET http://localhost:8080/api/city ... on%3D溫州 

返回 JSON 如下:

  1.     {         
  2. "id": 2,         
  3. "name""溫嶺",         
  4. "description""溫嶺是個(gè)沿海城市",         
  5. "score": 4 
  6.     }, 
  7.     {         
  8. "id": 1,         
  9. "name""上海",         
  10. "description""上海是個(gè)好城市",         
  11. "score": 3 
  12.     } 

e. LIKE 語句查詢

  1. GET http://localhost:8080/api/city ... on%3D城市 

返回 JSON 如下:

  1.     {         
  2. "id": 2,         
  3. "name""溫嶺",         
  4. "description""溫嶺是個(gè)沿海城市",         
  5. "score": 4 
  6.     }, 
  7.     {         
  8. "id": 1,         
  9. "name""上海",         
  10. "description""上海是個(gè)好城市",         
  11. "score": 3 
  12.     } 

三、spring-data-elasticsearch-crud 工程代碼詳解

具體代碼見 GitHub - https://github.com/JeffLi1993/springboot-learning-example

1.pom.xml 依賴

  1. <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B 
  3.      
  4.  
  5.     <modelVersion>4.0.0</modelVersion>    <groupId>springboot</groupId>     
  6. <artifactId>spring-data-elasticsearch-crud</artifactId>     
  7. <version>0.0.1-SNAPSHOT</version>     
  8. <name>spring-data-elasticsearch-crud :: spring-data-elasticsearch - 基本案例 </name>     
  9.  
  10. <!-- Spring Boot 啟動(dòng)父依賴 --> 
  11.     <parent> 
  12.         <groupId>org.springframework.boot</groupId>         
  13. <artifactId>spring-boot-starter-parent</artifactId>         
  14. <version>1.5.1.RELEASE</version>     
  15. </parent> 
  16.  
  17.  
  18.     <dependencies> 
  19.  
  20.  
  21.         <!-- Spring Boot Elasticsearch 依賴 --> 
  22.         <dependency> 
  23.             <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>         
  24. </dependency> 
  25.  
  26.  
  27.         <!-- Spring Boot Web 依賴 --> 
  28.         <dependency> 
  29.             <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency> 
  30.  
  31.  
  32.         <!-- Junit --> 
  33.         <dependency> 
  34.             <groupId>junit</groupId>            
  35.  <artifactId>junit</artifactId>            
  36.   <version>4.12</version>        
  37.    </dependency> 
  38.     </dependencies> 
  39.  
  40. </project> 

這里依賴的 spring-boot-starter-data-elasticsearch 版本是 1.5.1.RELEASE,對應(yīng)的 spring-data-elasticsearch 版本是 2.1.0.RELEASE。對應(yīng)官方文檔:http://docs.spring.io/spring-d ... html/。后面數(shù)據(jù)操作層都是通過該 spring-data-elasticsearch 提供的接口實(shí)現(xiàn)。

2. application.properties 配置 ES 地址

  1. # ES 
  2. spring.data.elasticsearch.repositories.enabled = true 
  3. spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 
  4.  
  5. 默認(rèn) 9300 是 Java 客戶端的端口。9200 是支持 Restful HTTP 的接口。 

更多配置:

  • spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默認(rèn)值: elasticsearch)
  • spring.data.elasticsearch.cluster-nodes 集群節(jié)點(diǎn)地址列表,用逗號分隔。如果沒有指定,就啟動(dòng)一個(gè)客戶端節(jié)點(diǎn)。
  • spring.data.elasticsearch.propertie 用來配置客戶端的額外屬性。
  • spring.data.elasticsearch.repositories.enabled 開啟 Elasticsearch 倉庫。(默認(rèn)值:true。)

3. ES 數(shù)據(jù)操作層

  1. /** 
  2.  * ES 操作類 
  3.  * <p> 
  4.  * Created by bysocket on 17/05/2017. 
  5.  */public interface CityRepository extends ElasticsearchRepository<City, Long> {    /** 
  6.      * AND 語句查詢 
  7.      * 
  8.      * @param description 
  9.      * @param score 
  10.      * @return 
  11.      */ 
  12.     List<City> findByDescriptionAndScore(String description, Integer score);    /** 
  13.      * OR 語句查詢 
  14.      * 
  15.      * @param description 
  16.      * @param score 
  17.      * @return 
  18.      */ 
  19.     List<City> findByDescriptionOrScore(String description, Integer score);    /** 
  20.      * 查詢城市描述 
  21.      * 
  22.      * 等同于下面代碼 
  23.      * @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}"
  24.      * Page<City> findByDescription(String description, Pageable pageable); 
  25.      * 
  26.      * @param description 
  27.      * @param page 
  28.      * @return 
  29.      */ 
  30.     Page<City> findByDescription(String description, Pageable page);    /** 
  31.      * NOT 語句查詢 
  32.      * 
  33.      * @param description 
  34.      * @param page 
  35.      * @return 
  36.      */ 
  37.     Page<City> findByDescriptionNot(String description, Pageable page);    /** 
  38.      * LIKE 語句查詢 
  39.      * 
  40.      * @param description 
  41.      * @param page 
  42.      * @return 
  43.      */ 
  44.     Page<City> findByDescriptionLike(String description, Pageable page); 

接口只要繼承 ElasticsearchRepository 類即可。默認(rèn)會提供很多實(shí)現(xiàn),比如 CRUD 和搜索相關(guān)的實(shí)現(xiàn)。類似于 JPA 讀取數(shù)據(jù),是使用 CrudRepository 進(jìn)行操作 ES 數(shù)據(jù)。支持的默認(rèn)方法有: count(), findAll(), findOne(ID), delete(ID), deleteAll(), exists(ID), save(DomainObject), save(Iterable<DomainObject>)。

另外可以看出,接口的命名是遵循規(guī)范的。常用命名規(guī)則如下:

關(guān)鍵字 方法命名

  • And findByNameAndPwd
  • Or findByNameOrSex
  • Is findById
  • Between findByIdBetween
  • Like findByNameLike
  • NotLike findByNameNotLike
  • OrderBy findByIdOrderByXDesc
  • Not findByNameNot

4. 實(shí)體類

  1. /** 
  2.  * 城市實(shí)體類 
  3.  * <p> 
  4.  * Created by bysocket on 03/05/2017. 
  5.  */@Document(indexName = "province", type = "city")public class City implements Serializable { 
  6.     private static final long serialVersionUID = -1L;    /** 
  7.      * 城市編號 
  8.      */ 
  9.     private Long id;    /** 
  10.      * 城市名稱 
  11.      */ 
  12.     private String name;    /** 
  13.      * 描述 
  14.      */ 
  15.     private String description;    /** 
  16.      * 城市評分 
  17.      */ 
  18.     private Integer score;    

注意

a. City 屬性名不支持駝峰式。

b. indexName 配置必須是全部小寫,不然會出異常。

org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase

【本文為51CTO專欄作者“李強(qiáng)強(qiáng)”的原創(chuàng)稿件,轉(zhuǎn)載請通過51CTO聯(lián)系作者獲取授權(quán)】

戳這里,看該作者更多好文

責(zé)任編輯:武曉燕 來源: 51CTO專欄
相關(guān)推薦

2017-06-06 15:24:13

springElasticSear架構(gòu)

2017-06-14 10:53:58

spring-data快速入門

2025-03-27 09:38:35

2021-03-16 08:54:35

AQSAbstractQueJava

2022-01-12 08:54:52

Spring編程架構(gòu)設(shè)計(jì)

2011-05-05 14:44:43

SurfaceFlinSurfaceActivity

2011-07-04 10:39:57

Web

2009-06-22 15:34:00

Javascript

2009-06-18 10:23:03

Javascript 基本框架

2019-01-07 15:29:07

HadoopYarn架構(gòu)調(diào)度器

2021-07-20 15:20:02

FlatBuffers阿里云Java

2012-05-21 10:06:26

FrameworkCocoa

2017-07-02 18:04:53

塊加密算法AES算法

2022-09-26 09:01:15

語言數(shù)據(jù)JavaScript

2020-05-27 20:25:47

SpringSpringBoot數(shù)據(jù)

2022-10-31 09:00:24

Promise數(shù)組參數(shù)

2018-11-09 16:24:25

物聯(lián)網(wǎng)云計(jì)算云系統(tǒng)

2022-11-09 08:06:15

GreatSQLMGR模式

2022-01-11 07:52:22

CSS 技巧代碼重構(gòu)

2021-04-27 08:54:43

ConcurrentH數(shù)據(jù)結(jié)構(gòu)JDK8
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久se精品一区精品二区 | 日韩久久精品视频 | 国产精品无码久久久久 | 一区二区三区视频在线 | 日日操夜夜操天天操 | 久久国产精品72免费观看 | 91精品国产色综合久久不卡蜜臀 | 成人网址在线观看 | 久久久区| 欧美a级成人淫片免费看 | 色婷婷婷婷色 | 国产精品不卡 | 国产精品视频偷伦精品视频 | 精品国产乱码一区二区三 | 国产成视频在线观看 | 精品亚洲国产成av人片传媒 | 黄色一级视频免费 | 日韩五月天 | 成人视屏在线观看 | 国产免费人成xvideos视频 | 亚洲成人免费视频在线观看 | 在线久草 | 成人激情免费视频 | 少妇性l交大片免费一 | 大久| 成人性视频在线播放 | 国产在线观看一区二区 | 在线观看黄免费 | 91社区视频 | 国产精品二区三区在线观看 | 欧美日日| 欧美国产一区二区三区 | 欧美日韩亚洲一区 | 秋霞性生活| 欧美在线免费 | 国产乱码精品一区二区三区五月婷 | 成人在线视频免费播放 | 罗宾被扒开腿做同人网站 | 免费在线看黄 | 亚洲国产一区二区在线 | 色综合久久久 |