按数组项或空数组过滤

时间:2019-11-12 11:53:27

标签: elasticsearch

我正在尝试按必须为空或包含项1的数组进行过滤。

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "memberOrganizationFamilyIds": [
                  1
                ]
              }
            }
          ],
          "must_not": [
            {
              "exists": {
                "field": "memberOrganizationFamilyIds"
              }
            }
          ]
        }
      }
    }
  }
}

根据文档,这应该是应该的,但它不起作用。

如果我们应用第一个过滤器,它将起作用。

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "memberOrganizationFamilyIds": [
                  1
                ]
              }
            }
          ]
        }
      }
    }
  }
}

如果我们应用第二个过滤器,它也将起作用。

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "memberOrganizationFamilyIds"
              }
            }
          ]
        }
      }
    }
  }
}

但不能在一起。

1 个答案:

答案 0 :(得分:1)

  

我正在尝试按必须为空包含项目1的数组进行过滤

尝试此操作,您应该添加should(意思是or

{
"query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "memberOrganizationFamilyIds": [
                  1
                ]
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "exists": {
                      "field": "memberOrganizationFamilyIds"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}