ES基础三 条件搜索

(1)bool:must,must_not,should,组合多个过滤条件
(2)bool可以嵌套
(3)相当于SQL中的多个and条件:当你把搜索语法学好了以后,基本可以实现部分常用的sql语法对应的功能
  
 补充,在kibana中一般filter没有只能提示了。可以先在query 直接写。
should 当一个条件是是必须含有,两个条件是满足一个即可。综合前两句是,至少满足一个条件即可。
GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "articleID": "XHDK-A-1293-#fJ3"
              }
            }
          ],
          "must_not": [
            {
              "term": {
                "postDate": "2017-01-02"
              }
            }
          ]
        }
      }
    }
  }
}

上述查询是查出

("postDate": "2017-01-01" or "articleID": "XHDK-A-1293-#fJ3") and !("postDate": "2017-01-02")

当需要嵌套是,不能直接bool嵌套,bool是嵌套在should,must,must_not里面的
GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "articleID": "XHDK-A-1293-#fJ3"
              }
            }
          ],
          "must": [
            {
              "term": {
                "postDate": "2017-01-02"
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "term": {
                      "_id": 3
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
 
原文地址:https://www.cnblogs.com/javabeginer/p/13060219.html