文章目录
- 一、单个使用
- 二、must和must_not组合(A-B)
- 三、must和should组合(A)
- 四、should和must_not组合(A-B)
- 五、must和should和must_not组合(A-C)
- 六、验证脚本,执行之后自行观察结果(price字段用来测试)即可理解
- 查考文档
一、单个使用
- must(且):数组里面的条件都要满足,该条数据才被选择,所有的条件为且的关系
- must_not(或,然后取反):数组里面的条件满足其中一个,该条数据则不被选择
- should(或):数组里面的条件满足其中一个,该条数据被选择
二、must和must_not组合(A-B)
- 满足must的数据减去满足must_not的数据则是最终结果
三、must和should组合(A)
- 满足must的数据
- should用来打分(字段为:_score ),影响返回排序
- 满足should条件的数据分值会更高
四、should和must_not组合(A-B)
- 满足should的数据减去满足must_not的数据则是最终结果
五、must和should和must_not组合(A-C)
- 满足must的数据减去满足must_not的数据则是最终结果
- should用来打分,影响返回排序
- 满足should条件的数据分值会更高
六、验证脚本,执行之后自行观察结果(price字段用来测试)即可理解
- 初始化数据
txt
POST mystore/_bulk
{"index":{"_id":1}}
{"price":10,"productID":"XHDK-A-1293-#fJ3"}
{"index":{"_id":2}}
{"price":20,"productID":"XHDK-A-1293-#f20"}
{"index":{"_id":3}}
{"price":30,"productID":"JODL-X-1937-#pV7"}
{"index":{"_id":4}}
{"price":40,"productID":"QQPX-R-3956-#aD8"}
{"index":{"_id":5}}
{"price":50,"productID":"KDKE-B-9947-#kL5"}
{"index":{"_id":6}}
{"price":60,"productID":"KDKE-B-9947-#kL5"}
{"index":{"_id":7}}
{"price":70,"productID":"JODL-X-1937-#pV7"}
{"index":{"_id":8}}
{"price":80,"productID":"JODL-X-1937-#pV7"}
- must和must_not组合
txt
GET mystore/_search
{
"query": {
"bool": {
"must": [{"range": {"price": {"gte": 20,"lte": 50}}}],
"must_not": [{"match": {"price": "40"}}]}
}
}
- must和should组合
txt
GET mystore/_search
{
"query": {
"bool": {
"must": [{"range": {"price": {"gte": 20,"lte": 50}}}],
"should": [{"range": {"price": {"gte": 40,"lte": 60}}}]
}
}
}
- should和must_not组合
txt
GET mystore/_search
{
"query": {
"bool": {
"should": [{"range": {"price": {"gte": 40,"lte": 60}}}],
"must_not": [{"match": {"price": "40"}}]}
}
}
- must和should和must_not组合
txt
GET mystore/_search
{
"query": {
"bool": {
"must": [{"range": {"price": {"gte": 20,"lte": 50}}}],
"should": [{"range": {"price": {"gte": 40,"lte": 60}}}],
"must_not": [{"match": {"price": "40"}}]
}
}
}