0

I'm trying to filter by an array that must be either empty or contain the item 1.

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

According to the docs this is how it should be but it isn't working.

If we apply the first filter it works.

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

If we apply the second filter it also works.

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

But not together.

1 Answer 1

1

I'm trying to filter by an array that must be either empty or contain the item 1

Try this you should add should (meaning) or

{
"query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "memberOrganizationFamilyIds": [
                  1
                ]
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "exists": {
                      "field": "memberOrganizationFamilyIds"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.