本文共 6480 字,大约阅读时间需要 21 分钟。
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 { "id”:"1", "score":"5", "name":"上海", "description":"上海是个热城市"}
POST { "id":"2", "score”:"4", "name”:”温岭", "description":”温岭是个沿海城市"}
{"_index": "cityindex","_type": "city","_id": "1","_version": 1,"_score": 1,"_source": { "id":"2", "score”:"4", "name”:”温岭", "description":”温岭是个沿海城市"}}
GET 温岭返回 JSON 如下:
[ { "id": 2, "name": "温岭", "description": "温岭是个沿海城市", "score": 4 }]
GET 温岭&score=4返回 JSON 如下:
[ { "id": 2, "name": "温岭", "description": "温岭是个沿海城市", "score": 4 }]如果换成 score=5 ,就没有结果了。
GET 上海&score=4返回 JSON 如下:
[ { "id": 2, "name": "温岭", "description": "温岭是个沿海城市", "score": 4 }, { "id": 1, "name": "上海", "description": "上海是个好城市", "score": 3 }]
GET 温州返回 JSON 如下:
[ { "id": 2, "name": "温岭", "description": "温岭是个沿海城市", "score": 4 }, { "id": 1, "name": "上海", "description": "上海是个好城市", "score": 3 }]
GET 城市返回 JSON 如下:
[ { "id": 2, "name": "温岭", "description": "温岭是个沿海城市", "score": 4 }, { "id": 1, "name": "上海", "description": "上海是个好城市", "score": 3 }]
# ESspring.data.elasticsearch.repositories.enabled = truespring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。
/** * 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);}
/** * 城市实体类 ** 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 」欢迎转载,保留摘要,谢谢!