Elasticsearch 高级复杂查询

  • 复杂查询:
    • 1.条件查询 2.分页、排序 3.过滤字段 4.模糊查询 5.高亮查询
  • 聚合查询:
    • 1.最大值 2.分组统计
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.wzh.elasticsearch.query;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch._types.query_dsl.FuzzyQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.SearchTemplateResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.wzh.elasticsearch.model.User;
import com.wzh.elasticsearch.util.ElasticsearchUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author 75654
* @date 2023/6/5 18:04
* <p>
* 复杂查询:
* 1.条件查询 2.分页、排序 3.过滤字段 4.模糊查询 5.高亮查询
* 聚合查询:
* 1.最大值 2.分组统计
*/
public class QueryTest {
/**
* 准备数据
*
* @throws IOException
*/
@Test
public void dataAdd() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 数据
List<User> users = new ArrayList<>();
users.add(new User("1001", "张三", 23, "男"));
users.add(new User("1002", "李四", 24, "女"));
users.add(new User("1003", "王五", 25, "男"));
users.add(new User("1004", "赵六", 26, "男"));
users.add(new User("1005", "梁七", 27, "女"));
users.add(new User("1006", "甲", 28, "女"));
users.add(new User("1007", "乙", 29, "男"));
users.add(new User("1008", "丙", 30, "女"));
users.add(new User("1009", "丁1", 31, "女"));
users.add(new User("1010", "丁2", 31, "男"));
users.add(new User("1011", "丁3", 31, "女"));
users.add(new User("1012", "丁4", 31, "女"));
users.add(new User("1013", "丁41", 31, "女"));
users.add(new User("1014", "丁42", 31, "男"));

List<BulkOperation> bulkOperations = new ArrayList<>();
for (User user : users) {
bulkOperations.add(BulkOperation.of(
s -> s.index(
v -> v.id(user.getUid()).document(user)
)
));
}
// 执行
BulkResponse bulkResponse = esClient.bulk(s -> s.index("user").operations(bulkOperations));
for (BulkResponseItem item : bulkResponse.items()) {
System.out.println(item.result());
}
System.out.println("errors==" + bulkResponse.errors());

restClient.close();
}

/**
* 单条件查询
*
* @throws IOException
*/
@Test
public void conditionSearchDoc() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 条件查询
String byName = "王五";
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.match(
m -> m.field("uname").query(byName)
)
), User.class);
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}

restClient.close();
}

/**
* 复杂条件查询(嵌套查询)
* - 创建条件 Query
*
* @throws IOException
*/
@Test
public void moreConditionSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 条件
String qName = "李四";
int qAge = 23;
Query queryName = MatchQuery.of(c -> c.field("uname").query(qName))._toQuery();
Query queryAge = RangeQuery.of(r -> r.field("age").gt(JsonData.of(qAge)))._toQuery();
// 调用 ES 服务
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.bool(
b -> b.must(queryName).must(queryAge)
)
), User.class);
// 结果
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 自定义模板搜索
*
* @throws IOException
*/
@Test
public void conditionTemplateSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 定义 template 模板 名称: myQueryTemplate
String templateName = "myQueryTemplate";
esClient.putScript(p -> p.id(templateName).script(
s -> s.lang("mustache").source("{\"query\":{\"match\":{\"{{field}}\":\"{{value}}\"}}}")
));
// 使用模板搜索
String fieldName = "uname";
String valueName = "张三";
SearchTemplateResponse<User> templateResponse = esClient.searchTemplate(t -> t.index("user").id(templateName)
.params("field", JsonData.of(fieldName))
.params("value", JsonData.of(valueName)), User.class);
// 结果
List<Hit<User>> hits = templateResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 分页、排序搜索出的数据
*
* @throws IOException
*/
@Test
public void limitAndOrderSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 条件
int tempAge = 24;
Query queryAge = RangeQuery.of(r -> r.field("age").gte(JsonData.of(tempAge)))._toQuery();
// 调用 ES
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.bool(
b -> b.must(queryAge)
)
// from 第1页 页大小5 排序
).from(0).size(5).sort(SortOptions.of(sort -> sort.field(
// 字段,排序规则
f -> f.field("age").order(SortOrder.Desc)
))), User.class);
// 结果
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 查询全部文档并分页
*
* @throws IOException
*/
@Test
public void limitSearchAllDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 条件
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
// queryName 自定义搜索名称,可直接返回 m -> m
q -> q.matchAll(m -> m.queryName("q1"))
).from(0).size(5).sort(SortOptions.of(
sort -> sort.field(
f -> f.field("age").order(SortOrder.Desc)
)
)), User.class);
// 结果
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 过滤字段
*
* @throws IOException
*/
@Test
public void filterSearchAllDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 搜索全部,第一页,页大小5,根据 age 降序,只显示 uname、gender 字段
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.matchAll(m -> m)
).from(0).size(5).sort(SortOptions.of(
sort -> sort.field(
f -> f.field("age").order(SortOrder.Desc)
)
)).source(t -> t.filter(
f -> f.includes("uname", "age").excludes("")
)), User.class);
// 结果
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 模糊查询,有问题
* @throws IOException
*/
@Test
public void likeSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 模糊查询 容错2,只显示字段 uname、age
String name = "1";
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.fuzzy(FuzzyQuery.of(
f -> f.field("uname").value(name).fuzziness("1")
))
), User.class);
// 结果
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
User user = hit.source();
System.out.println(Objects.requireNonNull(user).toString());
}
}

/**
* 聚合搜索 - 最大值 max
* @throws IOException
*/
@Test
public void maxSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// age 字段最大值,key 为maxAge01
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").size(0).aggregations(
"maxAge01", a -> a.max(MaxAggregation.of(
m -> m.field("age")
))
), User.class);

MaxAggregate maxAge01 = searchResponse.aggregations().get("maxAge01").max();
System.out.println(maxAge01.value());
}

/**
* 分组查询 - terms
* @throws IOException
*/
@Test
public void groupSearchDoc() throws IOException {
ElasticsearchClient esClient = ElasticsearchUtils.getEsClient();
// 分组字段 age
// todo 为什么分组字段是 gender 就不行: 初始想法 因为 text 类型字段不支持分组?
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").aggregations(
"groupGender01", g -> g.terms(TermsAggregation.of(
t -> t.field("age")
))
), User.class);
// 结果
Aggregate groupGender01 = searchResponse.aggregations().get("groupGender01");
LongTermsAggregate lterms = groupGender01.lterms();
Buckets<LongTermsBucket> buckets = lterms.buckets();
List<LongTermsBucket> array = buckets.array();
array.forEach(System.out::println);
}
}