SearchAPI请求方式
- URL
- Request Body(DSL)
指定查询范围
| 语法 | 范围 |
|---|---|
| /_search | 集群上所以索引 |
| /index1/_search | index1 |
| /index1,index2/_search | index1,index2 |
| /index*/_search | index开头索引 |
# URL
- q:指定查询语句
- df:默认字段,不指定会对所有字段进行查询
- sort:排序/from和size用于分页
- profile:可以查看是如何执行的
GET /test/_doc/_search?q=name:'ymj'&df=title&sort=year:desc&from=0&size=10&timeout=1s
1
# 1. 范查询
q=name:Beautiful Mind
q=name:"Beautiful Mind"
1
2
2
Beautiful Mind =Beautiful OR Mind "Beautiful Mind" = Beautiful ANd Mind
# 2. 分组查询
q=name:(Beautiful Mind)
连个都要存在
分组
+表示must
-表示must_not
q=name:(+Beautiful -Mind)
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3. 布尔查询
AND/OR/NOT 或者 &&/||/!
q=name:(Beautiful NOT Mind)
1
# 4. 范围查询
year:[2018 to 2019]
year:[* to 2019]
year:>=1980
1
2
3
2
3
# 5. 通配符查询
title:中国*
模糊匹配
title:中国~1
1
2
3
4
2
3
4
# DSL
基础了解
# 1. 全部查询
GET user/_search
{
"query": {
"match_all": {}
}
}
1
2
3
4
5
6
2
3
4
5
6
# 2. 匹配搜索
GET /user/_doc/_search
{
"query": {
"match": {
"name":"中 国",
"operator":"AND",
"slop":1
}
}
}
operator 必须包含中和国
slop 中间可以模糊一个字段
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3. 分组匹配搜索
GET /user/_doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name":"中国"
}
},
{
"match": {
"age":3
}
}
]
}
},
"filter": [
{
"range": {
"age": {
"lt": 4
}
}
}
],
"_source": ["name","age"],
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 10
}
gt >
gte >=
lt <
lte <=
term 精确
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
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
# 4.脚本搜索
GET /user/_doc/_search
{
"script_fields":{
"new_field":{
"script":{
"lang":"painless",
"source":"doc['order_date'].value+'_hello'"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 5. 高亮查询
GET /user/_doc/_search
{
"query": {
"match": {
"name":"国"
}
},
"highlight": {
"pre_tags": "<p class='red'>",
"post_tags": "<p>",
"fields": {
"name":{}
}
},
"_source": ["name","age"],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
DSL中也可使用URL使用方式