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
|
# bool 查询语法
POST /products/_search
{
"query": {
"bool": {
"must": {
"term": {"price": "30"}
},
"filter": {
"term": {"avaliable": "true"}
},
"must_not": {
"range": {
"price": {"lte": 10}
}
},
"should": [
{
"term": {"productID.keyword": "JODL-X-1937-#pV7"}
},
{
"term": {"productID.keyword": "XHDK-A-1293-#fJ3"}
}
],
"minimum_should_match": 1
}
}
}
# bool 查询嵌套结构,实现 should not 逻辑
POST /products/_search
{
"query": {
"bool": {
"must": {
"term": {
"price": "30"
}
},
"should": [
{
"bool": {
"must_not": {
"term": {
"avaliable": "false"
}
}
}
}
],
"minimum_should_match": 1
}
}
}
# 结构化查询"包含而不是相等"的问题
DELETE movies
POST /movies/_bulk
{ "index":{ "_id":1}}
{ "title" : "Faimer of the Bridge Part II","year":1995, "genre":"Comedy"}
{ "index":{ "_id": 2}}
{ "title" : "Dave","year":1993,"genre":["Comedy","Romance"]}
POST /movies/_search
{
"query": {"constant_score": {
"filter": {
"term": {
"genre.keyword": "Comedy"
}
}
}}
}
# 调整结构,增加genre_count
POST /newmovies/_bulk
{ "index":{ "_id":1}}
{ "title" : "Faimer of the Bridge Part II","year":1995, "genre":"Comedy","genre_count":1}
{ "index":{ "_id": 2}}
{ "title" : "Dave","year":1993,"genre":["Comedy","Romance"],"genre_count":2 }
# must 算分
GET newmovies/_search
{
"query": {
"bool": {
"must": [
{"term": {"genre.keyword": {"value": "Comedy"}}},
{"term": {"genre_count": {"value": "1"}}}
]
}
}
}
# filter 不算分
GET newmovies/_search
{
"query": {
"bool": {
"filter": [
{"term": {"genre.keyword": {"value": "Comedy"}}},
{"term": {"genre_count": {"value": "1"}}}
]
}
}
}
# 通过boost来控制打分
POST /blogs/_bulk
{ "index": { "_id":1}}
{"title":"Apple iPad", "content":"Apple iPad,Apple iPad"}
{ "index":{ "_id":2}}
{"title":"Apple iPad,Apple iPad", "content":"Apple iPad"}
POST /blogs/_search
{
"query": {
"bool": {
"should": [
{"match": {
"title":{
"query": "apple,ipad",
"boost": 2
}
}},
{"match": {
"content": {
"query": "apple,ipad",
"boost": 1
}
}}
]
}
}
}
# 要求苹果公司的产品优先展示,而不是食品相关的信息
POST /news/_bulk
{ "index": { "_id":1}}
{ "content":"Apple Mac" }
{ "index": { "_id": 2 }}
{ "content":"Apple iPad"}
{ "index":{ "_id":3}}
{ "content":"Apple employee like Apple Pie and Apple Juice"}
# id为3的文档排名会靠前
POST news/_search
{
"query": {
"bool": {
"must": {
"match": {
"content": "apple"
}
}
}
}
}
POST news/_search
{
"query": {
"bool": {
"must": {
"match": {"content": "apple"}
},
"must_not": {
"match": {"content": "pie"}
}
}
}
}
# 希望仍能搜索出但排名靠后
POST news/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "apple"
}
},
"negative": {
"match": {
"content": "pie"
}
},
"negative_boost": 0.2
}
}
}
|