elasticsearch search api

使用 Elasticsearch 提供的,基于JSON格式的更加完备的 query domain specific language (DSL),这边记录的是url search的一些语法

  • 使用 Elasticsearch 提供的,基于JSON格式的更加完备的 query domain specific language (DSL)
  • 在URL中使用查询参数

指定查询索引

语法 范围
/_search 集群上所有的索引
/index1/_search index1
/index1,index-2/_search index1和index2
/index*/_search 以index开头的索引

q|指定查询语句
df|默认字段,不指定会对所有字段进行查询
sort|排序
from/size|分页
Profile|查看查询怎么执行

指定字段查询

1
2
3
4
GET /users/_search?q=zjj&df=name
{
"profile": "true"
}

返回

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
{
"took" : 77,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"age" : "30",
"name" : "zjj"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[dg8Ap-yUQUWlMbgmg6RbdQ][users][0]",
"searches" : [
{
"query" : [
{
"type" : "TermQuery",
"description" : "name:zjj",
"time_in_nanos" : 1619976,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 5181,
"match" : 0,
"next_doc_count" : 2,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 6413,
"build_scorer_count" : 4,
"create_weight" : 73448,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 1534926
}
}
],
"rewrite_time" : 22423,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 1253068,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 15759
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}

范查询

1
2
3
4
GET /users/_search?q=zjj
{
"profile": "true"
}

返回

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
{
"took" : 53,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"age" : "30",
"name" : "zjj"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[dg8Ap-yUQUWlMbgmg6RbdQ][users][0]",
"searches" : [
{
"query" : [
{
"type" : "DisjunctionMaxQuery",
"description" : "(age.keyword:zjj | name:zjj | name.keyword:zjj | age:zjj)",
"time_in_nanos" : 14956224,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 2418833,
"match" : 0,
"next_doc_count" : 2,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 18436,
"build_scorer_count" : 4,
"create_weight" : 7110812,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 5408135
},
"children" : [
{
"type" : "TermQuery",
"description" : "age.keyword:zjj",
"time_in_nanos" : 1841882,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 0,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 0,
"build_scorer_count" : 3,
"create_weight" : 1839895,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 1983
}
},
{
"type" : "TermQuery",
"description" : "name:zjj",
"time_in_nanos" : 4720557,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 2,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 1,
"compute_max_score" : 2338296,
"advance" : 2467,
"advance_count" : 2,
"score" : 2636,
"build_scorer_count" : 4,
"create_weight" : 1785375,
"shallow_advance" : 574163,
"create_weight_count" : 1,
"build_scorer" : 17609
}
},
{
"type" : "TermQuery",
"description" : "name.keyword:zjj",
"time_in_nanos" : 35304,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 2,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 1,
"compute_max_score" : 6788,
"advance" : 1100,
"advance_count" : 2,
"score" : 662,
"build_scorer_count" : 4,
"create_weight" : 20786,
"shallow_advance" : 1694,
"create_weight_count" : 1,
"build_scorer" : 4263
}
},
{
"type" : "TermQuery",
"description" : "age:zjj",
"time_in_nanos" : 29197,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 0,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 0,
"build_scorer_count" : 3,
"create_weight" : 28483,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 710
}
}
]
}
],
"rewrite_time" : 86847,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 32828,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 23114
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}

term 与 phrase

  • (term) 苹果 笔 等效于 苹果 OR 笔
  • “phrase” “苹果 笔” 等效于 苹果 AND 笔,先后顺序保持一致
1
2
3
4
5
6
7
8
9
10
11
# 同时包含 Boys The
GET /moves/_search?q=title:"Boys The"
{
"profile": "true"
}

# Boys OR The
GET /moves/_search?q=title:(Boys The)
{
"profile": "true"
}

布尔操作

AND/OR/NOT 或者 &&/||/!

  • 必须大写
  • title:(a not b)
1
2
3
4
GET /moves/_search?q=title:(Perez AND Family)
{
"profile": "true"
}

分组

    • 表示 must
    • 表示 must_not
  • title:(+matrix AND -reloaded)
1
GET /moves/_search?q=title:(-Perez AND +Family)

范围查询

区间表示:[] 闭区间 ,{}开区间
* year : {2019 TO 2018}
* year : [* TO 2018]

算术符号

  • year:> 1980
  • year:(>2010&&<=2018)
  • year:(+>2010+<=2018)
1
GET /moves/_search?q=year:>2014

通配符查询(性能不好)

  • ? 代表1个字符,*代表0或多个字符
    • title:mi?d
    • title:be*
      1
      GET /moves/_search?q=title:c*

正则表

  • title:[bt]oy

模糊匹配与近似查询

  • title:befutifl~1 (1个冗余)
  • title:”lord rings”~2 (相隔位置)
1
GET /moves/_search?q=title:Woter~1

Request Body Search

Query DSL

1
2
3
4
5
6
7
POST /moves/_search
{
"profile": "true"
, "query": {
"match_all": {}
}
}

翻页

1
2
3
4
5
6
7
8
POST /moves/_search
{
"from": 0,
"size": 5,
"profile": "true"
, "query": {
"match_all": {}
}

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POST /moves/_search
{
"sort": [
{
"_id": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"profile": "true"
, "query": {
"match_all": {}
}
}

脚本字段

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"script_fields": {
"FIELD": {
"script": {
"lang": "painless",
"source": "doc['year'].value + '_aaaa'"
}
}
}
}

表达式(Match)

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"query": {
"match": {
"title": {
"query": "dog cat"
, "operator": "and"
}
}
}
}

短语搜索

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"query": {
"match_phrase": {
"title": {
"query": "1 3"
, "slop": 1
}
}
}
}

query_string

1
2
3
4
5
6
7
8
9
10
11
12

POST users/_search
{
"profile": "true",
"query": {
"query_string": {
"default_field": "name",
"query": "zhang OR wang"
}
}

}

simple_query_string

  • 类似 query string,但是会忽略错误语法,同时值支持部分查询语法
  • 不支持 AND OR NOT,会当做字符来处理
  • Term 默认关系是OR,可以指定 operator
  • 自持
      • 代替AND
    • | 代替OR
      • 代替 NOT
1
2
3
4
5
6
7
8
9
10
11
POST users/_search
{
"profile": "true",
"query": {
"simple_query_string": {
"query": "zhang wang",
"fields": ["name"],
"default_operator": "AND"
}
}
}
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~