当使用should查询时,如果包含了must或者filter查询,那么should的查询语句就不是或者【OR】的意思了,而是有或者没有都行的含义。
filter和should语句组合查询,会导致should语句失效
GET zx_album/album/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"userId": {
"value": "115102970493227008",
"boost": 1
}
}
},
{
"term": {
"status": {
"value": "0",
"boost": 1
}
}
}
],
"should": [
{
"term": {
"moveType": {
"value": 7,
"boost": 1
}
}
},
{
"term": {
"moveType": {
"value": 2,
"boost": 1
}
}
}
],
"disable_coord": false,
"adjust_pure_negative": true,
"boost": 1
}
}
}查询的数据不对,因为should没有被正确执行。
解决方案一:加上"minimum_number_should_match": 1 这个设置should语句的满足条件值。
解决方案二:将should嵌在must语句中。
GET zx_album/album/_search
{"query": {
"bool" : {
"filter" : [
{
"term" : {
"userId" : {
"value" : "115102970493227008"
}
}
},
{
"term" : {
"status" : {
"value" : "0"
}
}
},
{
"bool" : {
"should" : [
{
"term" : {
"moveType" : {
"value" : 7
}
}
},
{
"term" : {
"moveType" : {
"value" : 2
}
}
}
]
}
}
]
}}
}must语句和filter一样的做法
版权声明:本文为zhengchanmin原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。