Skip to content

Commit 40bf6e9

Browse files
author
liqiangqiang
committed
Spring Data Elasticsearch - 基本案例
1 parent 3426afb commit 40bf6e9

File tree

8 files changed

+345
-0
lines changed

8 files changed

+345
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>spring-data-elasticsearch-crud</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>spring-data-elasticsearch-crud :: spring-data-elasticsearch - 基本案例 </name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Elasticsearch 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
24+
</dependency>
25+
26+
<!-- Spring Boot Web 依赖 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<!-- Junit -->
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
</dependency>
38+
</dependencies>
39+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
* Created by bysocket on 16/4/26.
10+
*/
11+
// Spring Boot 应用的标识
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
// 程序启动入口
17+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18+
SpringApplication.run(Application.class,args);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.spring.springboot.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.service.CityService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.List;
10+
11+
/**
12+
* 城市 Controller 实现 Restful HTTP 服务
13+
* <p>
14+
* Created by bysocket on 03/05/2017.
15+
*/
16+
@RestController
17+
public class CityRestController {
18+
19+
@Autowired
20+
private CityService cityService;
21+
22+
/**
23+
* 插入 ES 新城市
24+
*
25+
* @param city
26+
* @return
27+
*/
28+
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
29+
public Long createCity(@RequestBody City city) {
30+
return cityService.saveCity(city);
31+
}
32+
33+
/**
34+
* AND 语句查询
35+
*
36+
* @param description
37+
* @param score
38+
* @return
39+
*/
40+
@RequestMapping(value = "/api/city/and/find", method = RequestMethod.GET)
41+
public List<City> findByDescriptionAndScore(@RequestParam(value = "description") String description,
42+
@RequestParam(value = "score") Integer score) {
43+
return cityService.findByDescriptionAndScore(description, score);
44+
}
45+
46+
/**
47+
* OR 语句查询
48+
*
49+
* @param description
50+
* @param score
51+
* @return
52+
*/
53+
@RequestMapping(value = "/api/city/or/find", method = RequestMethod.GET)
54+
public List<City> findByDescriptionOrScore(@RequestParam(value = "description") String description,
55+
@RequestParam(value = "score") Integer score) {
56+
return cityService.findByDescriptionOrScore(description, score);
57+
}
58+
59+
/**
60+
* 查询城市描述
61+
*
62+
* @param description
63+
* @return
64+
*/
65+
@RequestMapping(value = "/api/city/description/find", method = RequestMethod.GET)
66+
public List<City> findByDescription(@RequestParam(value = "description") String description) {
67+
return cityService.findByDescription(description);
68+
}
69+
70+
/**
71+
* NOT 语句查询
72+
*
73+
* @param description
74+
* @return
75+
*/
76+
@RequestMapping(value = "/api/city/description/not/find", method = RequestMethod.GET)
77+
public List<City> findByDescriptionNot(@RequestParam(value = "description") String description) {
78+
return cityService.findByDescriptionNot(description);
79+
}
80+
81+
/**
82+
* LIKE 语句查询
83+
*
84+
* @param description
85+
* @return
86+
*/
87+
@RequestMapping(value = "/api/city/like/find", method = RequestMethod.GET)
88+
public List<City> findByDescriptionLike(@RequestParam(value = "description") String description) {
89+
return cityService.findByDescriptionLike(description);
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.spring.springboot.domain;
2+
3+
import org.springframework.data.elasticsearch.annotations.Document;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* 城市实体类
9+
* <p>
10+
* Created by bysocket on 03/05/2017.
11+
*/
12+
@Document(indexName = "province", type = "city")
13+
public class City implements Serializable {
14+
15+
private static final long serialVersionUID = -1L;
16+
17+
/**
18+
* 城市编号
19+
*/
20+
private Long id;
21+
22+
/**
23+
* 城市名称
24+
*/
25+
private String name;
26+
27+
/**
28+
* 描述
29+
*/
30+
private String description;
31+
32+
/**
33+
* 城市评分
34+
*/
35+
private Integer score;
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
53+
public String getDescription() {
54+
return description;
55+
}
56+
57+
public void setDescription(String description) {
58+
this.description = description;
59+
}
60+
61+
public Integer getScore() {
62+
return score;
63+
}
64+
65+
public void setScore(Integer score) {
66+
this.score = score;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.spring.springboot.repository;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.elasticsearch.annotations.Query;
7+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
8+
9+
import java.util.List;
10+
11+
/**
12+
* ES 操作类
13+
* <p>
14+
* Created by bysocket on 17/05/2017.
15+
*/
16+
public interface CityRepository extends ElasticsearchRepository<City, Long> {
17+
/**
18+
* @param description
19+
* @param score
20+
* @return
21+
*/
22+
List<City> findByDescriptionAndScore(String description, Integer score);
23+
24+
List<City> findByDescriptionOrScore(String description, Integer score);
25+
26+
Page<City> findByDescription(String description, Pageable page);
27+
28+
// @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
29+
// Page<City> findByDescription(String description, Pageable pageable);
30+
31+
Page<City> findByDescriptionNot(String description, Pageable page);
32+
33+
Page<City> findByDescriptionLike(String description, Pageable page);
34+
35+
Page<City> findByScoreBetween(Integer score, Pageable page);
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
package org.spring.springboot.service;
3+
4+
import org.spring.springboot.domain.City;
5+
6+
import java.util.List;
7+
8+
public interface CityService {
9+
10+
/**
11+
* 新增 ES 城市信息
12+
*
13+
* @param city
14+
* @return
15+
*/
16+
Long saveCity(City city);
17+
18+
List<City> findByDescriptionAndScore(String description, Integer score);
19+
20+
List<City> findByDescriptionOrScore(String description, Integer score);
21+
22+
List<City> findByDescription(String description);
23+
24+
List<City> findByDescriptionNot(String description);
25+
26+
List<City> findByDescriptionLike(String description);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.spring.springboot.service.impl;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.spring.springboot.domain.City;
6+
import org.spring.springboot.repository.CityRepository;
7+
import org.spring.springboot.service.CityService;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.domain.Page;
10+
import org.springframework.data.domain.PageRequest;
11+
import org.springframework.data.domain.Pageable;
12+
import org.springframework.stereotype.Service;
13+
14+
import java.util.List;
15+
16+
/**
17+
* 城市 ES 业务逻辑实现类
18+
* <p>
19+
* Created by bysocket on 07/02/2017.
20+
*/
21+
@Service
22+
public class CityESServiceImpl implements CityService {
23+
24+
private static final Logger LOGGER = LoggerFactory.getLogger(CityESServiceImpl.class);
25+
26+
// 分页参数 -> TODO 代码可迁移到具体项目的公共 common 模块
27+
private static final Integer pageNumber = 0;
28+
private static final Integer pageSize = 10;
29+
Pageable pageable = new PageRequest(pageNumber, pageSize);
30+
31+
// ES 操作类
32+
@Autowired
33+
CityRepository cityRepository;
34+
35+
public Long saveCity(City city) {
36+
City cityResult = cityRepository.save(city);
37+
return cityResult.getId();
38+
}
39+
40+
public List<City> findByDescriptionAndScore(String description, Integer score) {
41+
return cityRepository.findByDescriptionAndScore(description, score);
42+
}
43+
44+
public List<City> findByDescriptionOrScore(String description, Integer score) {
45+
return cityRepository.findByDescriptionOrScore(description, score);
46+
}
47+
48+
public List<City> findByDescription(String description) {
49+
return cityRepository.findByDescription(description, pageable).getContent();
50+
}
51+
52+
public List<City> findByDescriptionNot(String description) {
53+
return cityRepository.findByDescriptionNot(description, pageable).getContent();
54+
}
55+
56+
public List<City> findByDescriptionLike(String description) {
57+
return cityRepository.findByDescriptionLike(description, pageable).getContent();
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ES
2+
spring.data.elasticsearch.repositories.enabled = true
3+
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300

0 commit comments

Comments
 (0)