elasticsearch过滤器/查询中的多个布尔子句 [英] Multiple bool clauses in elasticsearch filter/query

查看:55
本文介绍了elasticsearch过滤器/查询中的多个布尔子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何将此SQL查询转换为Elasticsearch查询?

How should I translate this SQL query into an elasticsearch query?

SELECT * FROM myTable WHERE (id = 99 AND isonline <> 1) OR (id = 98 AND isonline <> 0)

如何进行具有多个布尔过滤器的查询?(额外的好处是还要显示如何在NEST上做到这一点)

How do I make a query that has multiple bool filters? (bonus would be to also show how to do it in NEST)

我提出的弹性查询无法正常工作,因为它包含重复的对象键.

The elastic query I've come up with doesn't work because it contains duplicate object keys.

{
    "size": 1000,
    "query": {
        "match_all": {}
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "id": 99
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "isonline": true
                    }
                }
            ]
        },
        "bool": {
            "must": [
                {
                    "term": {
                        "id": 98
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "isonline": false
                    }
                }
            ]
        }        
    }
}

elasticsearch 1.7版

推荐答案

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "id": 99
                }
              },
              {
                "term": {
                  "isonline": false
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "id": 98
                }
              },
              {
                "term": {
                  "isonline": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这篇关于elasticsearch过滤器/查询中的多个布尔子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆