Skip to content

Commit a6c910d

Browse files
author
liqiangqiang
committed
Spring Data Elasticsearch - 基本案例
1 parent eaa6800 commit a6c910d

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/repository/CityRepository.java

+34-5
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,52 @@
1515
*/
1616
public interface CityRepository extends ElasticsearchRepository<City, Long> {
1717
/**
18+
* AND 语句查询
19+
*
1820
* @param description
1921
* @param score
2022
* @return
2123
*/
2224
List<City> findByDescriptionAndScore(String description, Integer score);
2325

26+
/**
27+
* OR 语句查询
28+
*
29+
* @param description
30+
* @param score
31+
* @return
32+
*/
2433
List<City> findByDescriptionOrScore(String description, Integer score);
2534

35+
/**
36+
* 查询城市描述
37+
*
38+
* 等同于下面代码
39+
* @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
40+
* Page<City> findByDescription(String description, Pageable pageable);
41+
*
42+
* @param description
43+
* @param page
44+
* @return
45+
*/
2646
Page<City> findByDescription(String description, Pageable page);
2747

28-
// @Query("{\"bool\" : {\"must\" : {\"term\" : {\"description\" : \"?0\"}}}}")
29-
// Page<City> findByDescription(String description, Pageable pageable);
30-
48+
/**
49+
* NOT 语句查询
50+
*
51+
* @param description
52+
* @param page
53+
* @return
54+
*/
3155
Page<City> findByDescriptionNot(String description, Pageable page);
3256

57+
/**
58+
* LIKE 语句查询
59+
*
60+
* @param description
61+
* @param page
62+
* @return
63+
*/
3364
Page<City> findByDescriptionLike(String description, Pageable page);
3465

35-
Page<City> findByScoreBetween(Integer score, Pageable page);
36-
3766
}

spring-data-elasticsearch-crud/src/main/java/org/spring/springboot/service/CityService.java

+36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
import java.util.List;
77

8+
/**
9+
* 城市 ES 业务接口类
10+
*
11+
*/
812
public interface CityService {
913

1014
/**
@@ -15,13 +19,45 @@ public interface CityService {
1519
*/
1620
Long saveCity(City city);
1721

22+
/**
23+
* AND 语句查询
24+
*
25+
* @param description
26+
* @param score
27+
* @return
28+
*/
1829
List<City> findByDescriptionAndScore(String description, Integer score);
1930

31+
/**
32+
* OR 语句查询
33+
*
34+
* @param description
35+
* @param score
36+
* @return
37+
*/
2038
List<City> findByDescriptionOrScore(String description, Integer score);
2139

40+
/**
41+
* 查询城市描述
42+
*
43+
* @param description
44+
* @return
45+
*/
2246
List<City> findByDescription(String description);
2347

48+
/**
49+
* NOT 语句查询
50+
*
51+
* @param description
52+
* @return
53+
*/
2454
List<City> findByDescriptionNot(String description);
2555

56+
/**
57+
* LIKE 语句查询
58+
*
59+
* @param description
60+
* @return
61+
*/
2662
List<City> findByDescriptionLike(String description);
2763
}

0 commit comments

Comments
 (0)