简体   繁体   English

对于 Elasticsearch,如何在 filter->terms 查询中使用 OR 而不是 AND?

[英]With Elasticsearch, how to use an OR instead of AND within filter->terms query?

I have this following query with elastic:我有以下弹性查询:

{
    "query": {
        "bool": {
            "filter": [{
                "terms": {
                    "participants.group": ["group1","group2"]
                }
            }, {
                "range": {
                    "recordDate": {
                        "gte": "2020-05-14 00:00:00.000",
                        "lte": "2020-07-22 20:30:56.566"
                    }
                }
            }]
        }
    }
}

Currently, this finds records with participants with group "group1" and "group2".目前,这会查找具有组“group1”“group2”的参与者的记录。 How to change the query so it finds records with participants from "group1" or "group2?如何更改查询,以便找到来自“group1”“group2”的参与者的记录?

Is it possible to do it without changing the structure of the query?是否可以在改变查询结构的情况下做到这一点?

I'm assuming that the field participants.group is of keyword type and not text type.我假设字段participants.groupkeyword类型而不是text类型。

Assuming that, the query you have roughly translates to (group1) or (group2) or (group1 and group2) .假设您的查询大致转换为(group1) or (group2) or (group1 and group2)

All you need to do is modify the query as below and add a must_not clause like below:您需要做的就是如下修改查询并添加一个must_not子句,如下所示:

POST my_filter_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
              "must": [
                {
                  "range": {
                    "recordDate": {
                      "gte": "2020-05-14 00:00:00.000",
                      "lte": "2020-07-22 20:30:56.566"
                    }
                }
              }
            ], 
            "should": [
              {
                "terms": {
                  "participants.group": ["group1", "group2"]
                }
              }
            ]
          }
        }
      ], 
      "must_not": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "participants.group": "group1"
                }
              },
              {
                "term": {
                  "participants.group": "group2"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Let me know if that works!让我知道这是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM