本文提纲
[[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;
1. 后台起守护线程启动 Elasticsearch
- cd elasticsearch-2.3.2/
- ./bin/elasticsearch -d
git clone 下载工程 springboot-elasticsearch ,项目地址见 GitHub - https://github.com/JeffLi1993/ ... ample。
下面开始运行工程步骤(Quick Start):
2. 项目结构介绍
- org.spring.springboot.controller - Controller 层
- org.spring.springboot.repository - ES 数据操作层
- org.spring.springboot.domain - 实体类
- org.spring.springboot.service - ES 业务逻辑层
- Application - 应用启动类
- application.properties - 应用配置文件,应用启动会自动读取配置
本地启动的 ES ,就不需要改配置文件了。如果连测试 ES 服务地址,需要修改相应配置
3.编译工程
在项目根目录 spring-data-elasticsearch-crud,运行 maven 指令:
- mvn clean install
4.运行工程
右键运行 Application 应用启动类(位置:/springboot-learning-example/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java)的 main 函数,这样就成功启动了 springboot-elasticsearch 案例。
用 Postman 工具新增两个城市
a. 新增城市信息
- POST http://127.0.0.1:8080/api/city
- {
- "id”:"1",
- "score":"5",
- "name":"上海",
- "description":"上海是个热城市"
- }
- POST http://127.0.0.1:8080/api/city
- {
- "id":"2",
- "score”:"4",
- "name”:”温岭",
- "description":”温岭是个沿海城市"
- }
可以打开 ES 可视化工具 head 插件:http://localhost:9200/_plugin/head/:
(如果不知道怎么安装,请查阅 《Elasticsearch 和插件 elasticsearch-head 安装详解》 http://www.bysocket.com/?p=1744 。)
在「数据浏览」tab,可以查阅到 ES 中数据是否被插入,插入后的数据格式如下:
- {
- "_index": "cityindex",
- "_type": "city",
- "_id": "1",
- "_version": 1,
- "_score": 1,
- "_source": {
- "id":"2",
- "score”:"4",
- "name”:”温岭",
- "description":”温岭是个沿海城市"
- }
- }
下面是基本查询语句的接口:
a. 普通查询,查询城市描述
- GET http://localhost:8080/api/city ... on%3D温岭
返回 JSON 如下:
- [
- {
- "id": 2,
- "name": "温岭",
- "description": "温岭是个沿海城市",
- "score": 4
- }
- ]
b. AND 语句查询
- GET http://localhost:8080/api/city ... on%3D温岭&score=4
返回 JSON 如下:
- [
- {
- "id": 2,
- "name": "温岭",
- "description": "温岭是个沿海城市",
- "score": 4
- }
- ]
如果换成 score=5 ,就没有结果了。
c. OR 语句查询
- GET http://localhost:8080/api/city ... on%3D上海&score=4
返回 JSON 如下:
- [
- {
- "id": 2,
- "name": "温岭",
- "description": "温岭是个沿海城市",
- "score": 4
- },
- {
- "id": 1,
- "name": "上海",
- "description": "上海是个好城市",
- "score": 3
- }
- ]
d. NOT 语句查询
- GET http://localhost:8080/api/city ... on%3D温州
返回 JSON 如下:
- [
- {
- "id": 2,
- "name": "温岭",
- "description": "温岭是个沿海城市",
- "score": 4
- },
- {
- "id": 1,
- "name": "上海",
- "description": "上海是个好城市",
- "score": 3
- }
- ]
e. LIKE 语句查询
- GET http://localhost:8080/api/city ... on%3D城市
返回 JSON 如下:
- [
- {
- "id": 2,
- "name": "温岭",
- "description": "温岭是个沿海城市",
- "score": 4
- },
- {
- "id": 1,
- "name": "上海",
- "description": "上海是个好城市",
- "score": 3
- }
- ]
三、spring-data-elasticsearch-crud 工程代码详解
具体代码见 GitHub - https://github.com/JeffLi1993/springboot-learning-example
1.pom.xml 依赖
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B
4.0.0 springboot spring-data-elasticsearch-crud 0.0.1-SNAPSHOT spring-data-elasticsearch-crud :: spring-data-elasticsearch - 基本案例 org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-web junit junit 4.12
这里依赖的 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 地址
- # ES
- spring.data.elasticsearch.repositories.enabled = true
- spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
- 默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。
更多配置:
3. ES 数据操作层
- /**
- * ES 操作类
- *
- * Created by bysocket on 17/05/2017.
- */public interface CityRepository extends ElasticsearchRepository
{ /** - * AND 语句查询
- *
- * @param description
- * @param score
- * @return
- */
- List
findByDescriptionAndScore(String description, Integer score); /** - * OR 语句查询
- *
- * @param description
- * @param score
- * @return
- */
- List
findByDescriptionOrScore(String description, Integer score); /** - * 查询城市描述
- *
- * 等同于下面代码
- * @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
- * Page
findByDescription(String description, Pageable pageable); - *
- * @param description
- * @param page
- * @return
- */
- Page
findByDescription(String description, Pageable page); /** - * NOT 语句查询
- *
- * @param description
- * @param page
- * @return
- */
- Page
findByDescriptionNot(String description, Pageable page); /** - * LIKE 语句查询
- *
- * @param description
- * @param page
- * @return
- */
- Page
findByDescriptionLike(String description, Pageable page); - }
接口只要继承 ElasticsearchRepository 类即可。默认会提供很多实现,比如 CRUD 和搜索相关的实现。类似于 JPA 读取数据,是使用 CrudRepository 进行操作 ES 数据。支持的默认方法有: count(), findAll(), findOne(ID), delete(ID), deleteAll(), exists(ID), save(DomainObject), save(Iterable
另外可以看出,接口的命名是遵循规范的。常用命名规则如下:
关键字 方法命名
4. 实体类
- /**
- * 城市实体类
- *
- * Created by bysocket on 03/05/2017.
- */@Document(indexName = "province", type = "city")public class City implements Serializable {
- private static final long serialVersionUID = -1L; /**
- * 城市编号
- */
- private Long id; /**
- * 城市名称
- */
- private String name; /**
- * 描述
- */
- private String description; /**
- * 城市评分
- */
- private Integer score;
- }
注意
a. City 属性名不支持驼峰式。
b. indexName 配置必须是全部小写,不然会出异常。
org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase
【本文为51CTO专栏作者“李强强”的原创稿件,转载请通过51CTO联系作者获取授权】
戳这里,看该作者更多好文