更新时间:2022-06-18 23:50:54
cd elasticsearch-2.3.2/
./bin/elasticsearch -d
org.spring.springboot.controller - Controller 层
org.spring.springboot.repository - ES 数据操作层
org.spring.springboot.domain - 实体类
org.spring.springboot.service - ES 业务逻辑层
Application - 应用启动类
application.properties - 应用配置文件,应用启动会自动读取配置
本地启动的 ES ,就不需要改配置文件了。如果连测试 ES 服务地址,需要修改相应配置mvn clean install
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":”温岭是个沿海城市"
}
{
"_index": "cityindex",
"_type": "city",
"_id": "1",
"_version": 1,
"_score": 1,
"_source": {
"id":"2",
"score”:"4",
"name”:”温岭",
"description":”温岭是个沿海城市"
}
}
GET http://localhost:8080/api/city ... on%3D温岭
返回 JSON 如下:[
{
"id": 2,
"name": "温岭",
"description": "温岭是个沿海城市",
"score": 4
}
]
GET http://localhost:8080/api/city ... on%3D温岭&score=4
返回 JSON 如下:[
{
"id": 2,
"name": "温岭",
"description": "温岭是个沿海城市",
"score": 4
}
]
如果换成 score=5 ,就没有结果了。GET http://localhost:8080/api/city ... on%3D上海&score=4
返回 JSON 如下:[
{
"id": 2,
"name": "温岭",
"description": "温岭是个沿海城市",
"score": 4
},
{
"id": 1,
"name": "上海",
"description": "上海是个好城市",
"score": 3
}
]
GET http://localhost:8080/api/city ... on%3D温州
返回 JSON 如下:[
{
"id": 2,
"name": "温岭",
"description": "温岭是个沿海城市",
"score": 4
},
{
"id": 1,
"name": "上海",
"description": "上海是个好城市",
"score": 3
}
]
GET http://localhost:8080/api/city ... on%3D城市
返回 JSON 如下:[
{
"id": 2,
"name": "温岭",
"description": "温岭是个沿海城市",
"score": 4
},
{
"id": 1,
"name": "上海",
"description": "上海是个好城市",
"score": 3
}
]
<?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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>spring-data-elasticsearch-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-elasticsearch-crud :: spring-data-elasticsearch - 基本案例 </name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot Elasticsearch 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
# ES
spring.data.elasticsearch.repositories.enabled = true
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。
/**
* ES 操作类
* <p>
* Created by bysocket on 17/05/2017.
*/
public interface CityRepository extends ElasticsearchRepository<City, Long> {
/**
* AND 语句查询
*
* @param description
* @param score
* @return
*/
List<City> findByDescriptionAndScore(String description, Integer score);
/**
* OR 语句查询
*
* @param description
* @param score
* @return
*/
List<City> findByDescriptionOrScore(String description, Integer score);
/**
* 查询城市描述
*
* 等同于下面代码
* @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
* Page<City> findByDescription(String description, Pageable pageable);
*
* @param description
* @param page
* @return
*/
Page<City> findByDescription(String description, Pageable page);
/**
* NOT 语句查询
*
* @param description
* @param page
* @return
*/
Page<City> findByDescriptionNot(String description, Pageable page);
/**
* LIKE 语句查询
*
* @param description
* @param page
* @return
*/
Page<City> findByDescriptionLike(String description, Pageable page);
}
/**
* 城市实体类
* <p>
* 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;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
}
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!