自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

本文提綱

[[194551]]

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

二、運行 spring-data-elasticsearch-crud 工程

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

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

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

二、運行 spring-data-elasticsearch-crud 工程

注意的是這里使用的是 ElasticSearch 2.3.2。是因為版本對應關系 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**
  • * - 只需要你修改下對應的 pom 文件版本號
  • ** - 下一個 ES 的版本會有重大的更新

1. 后臺起守護線程啟動 Elasticsearch

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

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

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

2. 項目結構介紹

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

本地啟動的 ES ,就不需要改配置文件了。如果連測試 ES 服務地址,需要修改相應配置

3.編譯工程

在項目根目錄 spring-data-elasticsearch-crud,運行 maven 指令:

  1. mvn clean install 

4.運行工程

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

用 Postman 工具新增兩個城市

a. 新增城市信息

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

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

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

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

  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":”溫嶺是個沿海城市" 

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

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

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

返回 JSON 如下:

  1.     {         
  2.     "id": 2,         
  3.     "name""溫嶺",         
  4.     "description""溫嶺是個沿海城市",         
  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""溫嶺是個沿海城市",         
  5. "score": 4 
  6.     } 

如果換成 score=5 ,就沒有結果了。

c. OR 語句查詢

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

返回 JSON 如下:

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

d. NOT 語句查詢

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

返回 JSON 如下:

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

e. LIKE 語句查詢

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

返回 JSON 如下:

  1.     {         
  2. "id": 2,         
  3. "name""溫嶺",         
  4. "description""溫嶺是個沿海城市",         
  5. "score": 4 
  6.     }, 
  7.     {         
  8. "id": 1,         
  9. "name""上海",         
  10. "description""上海是個好城市",         
  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 啟動父依賴 --> 
  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,對應的 spring-data-elasticsearch 版本是 2.1.0.RELEASE。對應官方文檔:http://docs.spring.io/spring-d ... html/。后面數據操作層都是通過該 spring-data-elasticsearch 提供的接口實現。

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. 默認 9300 是 Java 客戶端的端口。9200 是支持 Restful HTTP 的接口。 

更多配置:

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

3. ES 數據操作層

  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 類即可。默認會提供很多實現,比如 CRUD 和搜索相關的實現。類似于 JPA 讀取數據,是使用 CrudRepository 進行操作 ES 數據。支持的默認方法有: count(), findAll(), findOne(ID), delete(ID), deleteAll(), exists(ID), save(DomainObject), save(Iterable<DomainObject>)。

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

關鍵字 方法命名

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

4. 實體類

  1. /** 
  2.  * 城市實體類 
  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專欄作者“李強強”的原創(chuàng)稿件,轉載請通過51CTO聯系作者獲取授權】

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

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2017-06-06 15:24:13

springElasticSear架構

2017-06-14 10:53:58

spring-data快速入門

2025-03-27 09:38:35

2022-01-12 08:54:52

Spring編程架構設計

2021-03-16 08:54:35

AQSAbstractQueJava

2011-05-05 14:44:43

SurfaceFlinSurfaceActivity

2009-06-22 15:34:00

Javascript

2011-07-04 10:39:57

Web

2009-06-18 10:23:03

Javascript 基本框架

2021-07-20 15:20:02

FlatBuffers阿里云Java

2017-07-02 18:04:53

塊加密算法AES算法

2019-01-07 15:29:07

HadoopYarn架構調度器

2012-05-21 10:06:26

FrameworkCocoa

2020-05-27 20:25:47

SpringSpringBoot數據

2022-09-26 09:01:15

語言數據JavaScript

2022-09-29 09:19:04

線程池并發(fā)線程

2011-01-27 10:11:46

J2EEjavaspring

2022-01-11 07:52:22

CSS 技巧代碼重構

2019-12-04 10:13:58

Kubernetes存儲Docker

2022-11-09 08:06:15

GreatSQLMGR模式
點贊
收藏

51CTO技術棧公眾號