elasticsearch的查询与评分

关键词

  • match
  • term
  • filter
  • bool
  • text
  • keyword

什么时候会进行评分

  • 分词字段&match:会评分
  • 分词字段&term:会评分
  • 分词字段&filter&term:不会评分
  • 分词字段&filter&match:不会评分
  • 不分词字段&match:会评分
  • 不分词字段&term:会评分
  • 不分词字段&filter&term:不会评分
  • 不分词字段&filter&match:不会评分

所以是否进行评分,跟字段是否分词、查询使用match还是term没关系,只跟查询上下文是query还是term又关系

  • query查询会评分
  • filter查询不会评分
  • must会评分
  • should会评分
  • must_not不评分
  • filter&must不评分
  • filter&should不评分
  • filter&must_not不评分

评分会减慢查询速度,所以如果查询场景中都是精确匹配,需要在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
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
curl -s -H 'Content-Type: application/json' -X POST  -d '{"query":{"match":{"name":"doe"}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vePsw3WGo94TO752D",
"_score": 0.25811607,
"_source": {
"name": "John Doe"
}
}
]
}
}


curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"term":{"name":"doe"}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.25811607,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vePsw3WGo94TO752D",
"_score": 0.25811607,
"_source": {
"name": "John Doe"
}
}
]
}
}

curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"bool":{"filter":{"match":{"name":"doe"}}}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vePsw3WGo94TO752D",
"_score": 0,
"_source": {
"name": "John Doe"
}
}
]
}
}

curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"match":{"kw_col":"xxx"}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0.2876821,
"_source": {
"kw_col": "xxx"
}
}
]
}
}

curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"term":{"kw_col":"xxx"}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0.2876821,
"_source": {
"kw_col": "xxx"
}
}
]
}
}

curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"bool":{"filter":{"term":{"kw_col":"xxx"}}}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0,
"_source": {
"kw_col": "xxx"
}
}
]
}
}

curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"bool":{"filter":{"match":{"kw_col":"xxx"}}}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0,
"_source": {
"kw_col": "xxx"
}
}
]
}
}



curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"bool":{"filter":{"bool":{"should":[{"term":{"kw_col":"xxx"}},{"term":{"kw_col":"yyy"}}]}}}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0,
"_source": {
"kw_col": "xxx"
}
}
]
}
}


curl -s -H 'Content-Type: application/json' -X POST -d '{"query":{"bool":{"should":[{"term":{"kw_col":"xxx"}},{"term":{"kw_col":"xxx"}}]}}}' 'http://localhost:9200/hong_test02/doc/_search' | jq
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "hong_test02",
"_type": "doc",
"_id": "AW2vkirj3WGo94TO752Q",
"_score": 0.5753642,
"_source": {
"kw_col": "xxx"
}
}
]
}
}