赞
踩
在实际应用中,发现当bool中同时使用must和should 没有达到想要的想过,而是只展示了must中的命中数据,所以打算探究一下bool中 三种逻辑关系的组合。
上述查询语句只展示了must的结果,没有should中的结果,(我一开始以为是must 和 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"}
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
},
{
"range": {
"price": {
"gte": 30,
"lte": 60
}
}
}
]
}
}
}
结果:30,40,50
GET mystore/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"price": "40"
}
},
{
"match": {
"price": "70"
}
}
]
}
}
}
结果:10,20,30,50,60,80
GET mystore/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
},
{
"range": {
"price": {
"gte": 50,
"lte": 70
}
}
}
]
}
}
}
结果:50,60,40,70
谁命中的多,谁分数高,在前边
GET mystore/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 20,
"lte": 50
}
}
}
],
"must_not": [
{
"match": {
"price": "40"
}
}
]
}
}
}
结果:20,30,50
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中提到的数据,分数高,在前边
GET mystore/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": 40,
"lte": 60
}
}
}
],
"must_not": [
{
"match": {
"price": "40"
}
}
]
}
}
}
结果:50,60
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中提到的数据分数高,在前边
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。