DSL-组合查询
bool
把各种其它查询通过如下的方式进行组合
- must 必须
- must_not 必须不
- should 应该
在 Postman 中,向 ES 服务器发 GET 请求
GET http://localhost:9200/student/_search
1
body
{
"query":{
"bool":{
"must":{
"match":{ // 也可以有term等操作
"name": "zhangsan"
}
},
"must_not":{
"match":{
"age":"40"
}
},
"should":{
"match":{
"sex":"男"
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
response
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.261763,
"hits": [
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_score": 2.261763,
"_source": {
"name": "zhangsan",
"nickname": "zhangsan",
"sex": "男",
"age": 30
}
}
]
}
}
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
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
注意:创建的mapping中,如果age是非indexed字段,则会报错
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [age] since it is not indexed.",
"index_uuid": "EbD7tHUtT2qC0wWo58hkEQ",
"index": "student"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "student",
"node": "mhiikNDwSX-kxGyTE3bIKg",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [age] since it is not indexed.",
"index_uuid": "EbD7tHUtT2qC0wWo58hkEQ",
"index": "student",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [age] since it is not indexed."
}
}
}
]
},
"status": 400
}
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
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
Last Updated: 2022/02/05, 15:58:51