elasticsearch - Elasticsearch-术语条件中的OR

标签 elasticsearch

我在将mysql查询转移到ES方面几乎不需要帮助。查询看起来像这样

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

仅设置为 AND条件很容易,但是如何在术语中将AND与OR混合?
"query" : {
    "bool" : {
      "must" : [{
          "terms" : { "visibility" : ["1", "2"] }
      }, {
          "term" : { "active" : "1" } 
      }, {
          "term" : { "active" : "0", "finished" : "1" } // OR
      },]
    }
  }

最佳答案

通过在主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"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

关于elasticsearch - Elasticsearch-术语条件中的OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630565/

相关文章:

python - 带有 Elasticsearch 后端的 Django Haystack

date - 如何在 NoSQL 中语义存储营业时间?

elasticsearch - 过滤深层嵌套的项目

postgresql - voltDB 或 NuoDB 是否需要 ElasticSearch?

regex - 在kibana中按uri param筛选

elasticsearch - Elasticsearch配置设置

elasticsearch - 如何将 Elasticsearch JSON转换为Tire兼容查询

ruby-on-rails - Elasticsearch on Query 的 Facet 问题

elasticsearch - 使用logstash修改字段的内容

search - Drupal 7搜索API,Elasticsearch连接器,聚合/融合