在ElasticSearch中查询多个字段和条件

时间:2015-02-23 00:49:49

标签: elasticsearch

在ElasticSearch中,可以在一个查询中转换此SQL吗?

SELECT * FROM table WHERE state IN ('NY', 'CA', 'FL') AND grade IN ('GOOD', 'BAD') AND active = 'YES' AND quantity > 100 AND quantity < 200

1 个答案:

答案 0 :(得分:1)

您可以将 filtered query bool filter 一起使用

{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : {}
      },
      "filter" : {
        "bool" : {
          "must" : [{
              "terms" : {
                "state" : [
                  "NY",
                  "CA",
                  "FL"
                ]
              }
            }, 
            {
              "terms" : {
                "grade" : [
                  "GOOD",
                  "BAD"
                ]
              }
            }, 
            {
              "term" : {
                "active" : "YES"
              }
            }, 
            {
              "range" : {
                "quantity" : {
                  "gt" : 100,
                  "lt" : 200
                }
              }
            }
          ]
        }
      }
    }
  }
}