elasticsearch must和should组合查询

{"query": {
   "bool": {
     "should": [
       {"bool": {
         "must": [
           {}
         ]
       }
       },
       {"bool": {
         "must": [
           {}
         ]
       }}
     ]
   }
  }
}

bool 过滤

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:

  • must :: 多个查询条件的完全匹配,相当于 and。
  • must_not :: 多个查询条件的相反匹配,相当于 not。
  • should :: 至少有一个查询条件匹配, 相当于 or。

这些参数可以分别继承一个过滤条件或者一个过滤条件的数组:


    "bool": { 
        "must":     { "term": { "folder": "inbox" }}, 
        "must_not": { "term": { "tag":    "spam"  }}, 
        "should": [ 
                    { "term": { "starred": true   }}, 
                    { "term": { "unread":  true   }} 
        ] 
    } 
}

原文地址:https://www.cnblogs.com/crystaltu/p/8953056.html