mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Data Elasticsearch - 基本案例
This commit is contained in:
39
spring-data-elasticsearch-crud/pom.xml
Executable file
39
spring-data-elasticsearch-crud/pom.xml
Executable file
@@ -0,0 +1,39 @@
|
|||||||
|
<?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/maven-4.0.0.xsd">
|
||||||
|
<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>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package org.spring.springboot;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring Boot 应用启动类
|
||||||
|
*
|
||||||
|
* Created by bysocket on 16/4/26.
|
||||||
|
*/
|
||||||
|
// Spring Boot 应用的标识
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 程序启动入口
|
||||||
|
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||||
|
SpringApplication.run(Application.class,args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package org.spring.springboot.controller;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.spring.springboot.service.CityService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市 Controller 实现 Restful HTTP 服务
|
||||||
|
* <p>
|
||||||
|
* Created by bysocket on 03/05/2017.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class CityRestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CityService cityService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入 ES 新城市
|
||||||
|
*
|
||||||
|
* @param city
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
|
||||||
|
public Long createCity(@RequestBody City city) {
|
||||||
|
return cityService.saveCity(city);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AND 语句查询
|
||||||
|
*
|
||||||
|
* @param description
|
||||||
|
* @param score
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city/and/find", method = RequestMethod.GET)
|
||||||
|
public List<City> findByDescriptionAndScore(@RequestParam(value = "description") String description,
|
||||||
|
@RequestParam(value = "score") Integer score) {
|
||||||
|
return cityService.findByDescriptionAndScore(description, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OR 语句查询
|
||||||
|
*
|
||||||
|
* @param description
|
||||||
|
* @param score
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city/or/find", method = RequestMethod.GET)
|
||||||
|
public List<City> findByDescriptionOrScore(@RequestParam(value = "description") String description,
|
||||||
|
@RequestParam(value = "score") Integer score) {
|
||||||
|
return cityService.findByDescriptionOrScore(description, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询城市描述
|
||||||
|
*
|
||||||
|
* @param description
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city/description/find", method = RequestMethod.GET)
|
||||||
|
public List<City> findByDescription(@RequestParam(value = "description") String description) {
|
||||||
|
return cityService.findByDescription(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NOT 语句查询
|
||||||
|
*
|
||||||
|
* @param description
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city/description/not/find", method = RequestMethod.GET)
|
||||||
|
public List<City> findByDescriptionNot(@RequestParam(value = "description") String description) {
|
||||||
|
return cityService.findByDescriptionNot(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LIKE 语句查询
|
||||||
|
*
|
||||||
|
* @param description
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/city/like/find", method = RequestMethod.GET)
|
||||||
|
public List<City> findByDescriptionLike(@RequestParam(value = "description") String description) {
|
||||||
|
return cityService.findByDescriptionLike(description);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package org.spring.springboot.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市实体类
|
||||||
|
* <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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package org.spring.springboot.repository;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.elasticsearch.annotations.Query;
|
||||||
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ES 操作类
|
||||||
|
* <p>
|
||||||
|
* Created by bysocket on 17/05/2017.
|
||||||
|
*/
|
||||||
|
public interface CityRepository extends ElasticsearchRepository<City, Long> {
|
||||||
|
/**
|
||||||
|
* @param description
|
||||||
|
* @param score
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<City> findByDescriptionAndScore(String description, Integer score);
|
||||||
|
|
||||||
|
List<City> findByDescriptionOrScore(String description, Integer score);
|
||||||
|
|
||||||
|
Page<City> findByDescription(String description, Pageable page);
|
||||||
|
|
||||||
|
// @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
|
||||||
|
// Page<City> findByDescription(String description, Pageable pageable);
|
||||||
|
|
||||||
|
Page<City> findByDescriptionNot(String description, Pageable page);
|
||||||
|
|
||||||
|
Page<City> findByDescriptionLike(String description, Pageable page);
|
||||||
|
|
||||||
|
Page<City> findByScoreBetween(Integer score, Pageable page);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
package org.spring.springboot.service;
|
||||||
|
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface CityService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增 ES 城市信息
|
||||||
|
*
|
||||||
|
* @param city
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Long saveCity(City city);
|
||||||
|
|
||||||
|
List<City> findByDescriptionAndScore(String description, Integer score);
|
||||||
|
|
||||||
|
List<City> findByDescriptionOrScore(String description, Integer score);
|
||||||
|
|
||||||
|
List<City> findByDescription(String description);
|
||||||
|
|
||||||
|
List<City> findByDescriptionNot(String description);
|
||||||
|
|
||||||
|
List<City> findByDescriptionLike(String description);
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package org.spring.springboot.service.impl;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spring.springboot.domain.City;
|
||||||
|
import org.spring.springboot.repository.CityRepository;
|
||||||
|
import org.spring.springboot.service.CityService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 城市 ES 业务逻辑实现类
|
||||||
|
* <p>
|
||||||
|
* Created by bysocket on 07/02/2017.
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class CityESServiceImpl implements CityService {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(CityESServiceImpl.class);
|
||||||
|
|
||||||
|
// 分页参数 -> TODO 代码可迁移到具体项目的公共 common 模块
|
||||||
|
private static final Integer pageNumber = 0;
|
||||||
|
private static final Integer pageSize = 10;
|
||||||
|
Pageable pageable = new PageRequest(pageNumber, pageSize);
|
||||||
|
|
||||||
|
// ES 操作类
|
||||||
|
@Autowired
|
||||||
|
CityRepository cityRepository;
|
||||||
|
|
||||||
|
public Long saveCity(City city) {
|
||||||
|
City cityResult = cityRepository.save(city);
|
||||||
|
return cityResult.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> findByDescriptionAndScore(String description, Integer score) {
|
||||||
|
return cityRepository.findByDescriptionAndScore(description, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> findByDescriptionOrScore(String description, Integer score) {
|
||||||
|
return cityRepository.findByDescriptionOrScore(description, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> findByDescription(String description) {
|
||||||
|
return cityRepository.findByDescription(description, pageable).getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> findByDescriptionNot(String description) {
|
||||||
|
return cityRepository.findByDescriptionNot(description, pageable).getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<City> findByDescriptionLike(String description) {
|
||||||
|
return cityRepository.findByDescriptionLike(description, pageable).getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# ES
|
||||||
|
spring.data.elasticsearch.repositories.enabled = true
|
||||||
|
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
|
||||||
Reference in New Issue
Block a user