ElasticSearch语句中must,must_not,should 组合关系
媒介:在实际应用中,发现当bool中同时利用must和should 没有到达想要的想过,而是只展示了must中的命中数据,以是计划探究一下bool中 三种逻辑关系的组合。
https://img-blog.csdnimg.cn/direct/7b1fb8ee726b4a8ebb7600c53be764c6.png
上述查询语句只展示了must的结果,没有should中的结果,(我一开始以为是must 和 should 是交集的关系)
概念
一、单个利用概念
[*]must(且):数组内里的条件都要满足,该条数据才被选择,全部的条件为且的关系,既交集
[*]must_not(或,然后取反):数组内里的条件满足其中一个,该条数据则不被选择,既差集
[*]should(或):数组内里的条件满足其中一个,该条数据被选择,既并集
二、must和must组合(A、B交集)
[*]多个must 组合,就是求这些条件数据的交集
三、must_not和must_not组合(all-A-B)
[*]多个must_not组合,就是全部的数据减去命中 must_not 的数据集
四、should和should组合(A、B并集)
[*]多个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条件的数据分值会更高
实验
一、数据准备
PUT mystore
----------------
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组合(A、B交集)
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
},
{
"range": {
"price": {
"gte": 30,
"lte": 60
}
}
}
]
}
}
}
结果:30,40,50
https://img-blog.csdnimg.cn/direct/38e28c19431147a1a5688ff8adf4e708.png
三、must_not和must_not组合(all-A-B)
GET mystore/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"price": "40"
}
},
{
"match": {
"price": "70"
}
}
]
}
}
}
结果:10,20,30,50,60,80
https://img-blog.csdnimg.cn/direct/f025680ef69e4dadb15cb2a4c59f3053.png
四、should和should组合(A、B并集)
GET mystore/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
},
{
"range": {
"price": {
"gte": 50,
"lte": 70
}
}
}
]
}
}
}
结果:50,60,40,70
谁命中的多,谁分数高,在前边
https://img-blog.csdnimg.cn/direct/dcf78524f3bb4f2781f709265d7618c9.png
五、must和must_not组合(A-B)
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
}
],
"must_not": [
{
"match": {
"price": "40"
}
}
]
}
}
}
结果:20,30,50
https://img-blog.csdnimg.cn/direct/effb5f25b1084b9696edd6943b0b64cc.png
六、must和should组合(A)
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
}
],
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
}
]
}
}
}
结果:40,50,20,30
只展示must的数据集,但是在should中提到的数据,分数高,在前边
https://img-blog.csdnimg.cn/direct/cd686914750e4af6bbd1a2c721356d3b.png
七、should和must_not组合(A-B)
GET mystore/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
}
],
"must_not": [
{
"match": {
"price": "40"
}
}
]
}
}
}
结果:50,60
https://img-blog.csdnimg.cn/direct/9f115fc35c934605941520d3a5600037.png
八、must和should和must_not组合(A-C)
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
}
],
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
}
],
"must_not": [
{
"match": {
"price": "40"
}
}
]
}
}
}
结果:50,20,30
结果就是must - must_not,不过在should中提到的数据分数高,在前边
https://img-blog.csdnimg.cn/direct/2fc174f79f5c44f1bd29756e3af354e6.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]