Elasticsearch - 在条件条件下的OR

时间:2017-06-19 12:27:23

标签: elasticsearch

将mysql查询转移到ES我几乎无需帮助。查询看起来像这样

SELECT * FROM `xyz` WHERE visibility IN (1,2) AND (active=0 OR (active=1 AND finished=1)

很容易, AND条件,但如何在 term 中与OR混合?

"query" : {
    "bool" : {
      "must" : [{
          "terms" : { "visibility" : ["1", "2"] }
      }, {
          "term" : { "active" : "1" } 
      }, {
          "term" : { "active" : "0", "finished" : "1" } // OR
      },]
    }
  }

1 个答案:

答案 0 :(得分:2)

通过在bool/should主查询中嵌套bool/filterbool/filter查询来尝试这样做:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "visibility": [
              "1",
              "2"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "active": "0"
                }
              },
              {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "active": "1"
                      }
                    },
                    {
                      "term": {
                        "finished": "1"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}