当使用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一样的做法

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐