elasticsearch5.x取消了query/filtered结构

升级es5.x之后,执行搜索是遇到了如下错误

查看了官方文档后发现,在es2.x时,我们可以使用如下的DSL结构

1.search API中只能包含 query 语句,所以我们需要用 filtered 来同时包含 “query” 和 “filter” 子句:

1
2
3
4
5
6
{
"filtered": {
"query": { "match": { "email": "business opportunity" }},
"filter": { "term": { "folder": "inbox" }}
}
}

2.我们在外层再加入 query 的上下文关系:

1
2
3
4
5
6
7
8
9
GET /_search
{
"query": {
"filtered": {
"query": { "match": { "email": "business opportunity" }},
"filter": { "term": { "folder": "inbox" }}
}
}
}

但是值得注意的是,在es5.x中已经抛弃了这一种结构,取而代之的是query/bool/(must/should/filter…)结构

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
POST _search
{
"query": {
"bool": {
"must": {
"term": {
"user": "kimchy"
}
},
"filter": {
"term": {
"tag": "tech"
}
},
"must_not": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
},
"should": [{
"term": {
"tag": "wow"
}
},
{
"term": {
"tag": "elasticsearch"
}
}
],
"minimum_should_match": 1,
"boost": 1.0
}
}
}

参考地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×