1

I have the following mapping

{
  "properties": {
    "restaurant_name": {"type": "string"},
    "menu": {
      "type": "nested",
      "properties": {
        "name": {"type": "string"}
      }
    }
  }
}

I am trying to filter all those documents which have optional "menu" field exists

GET /restaurnats/_search
{
  "filter": {
    "query": {
      "bool": {
        "must": [
          {"exists" : { "field" : "menu" }}
        ]
      }
    }
  }
}

But, when I try the same query to filter those documents which have "restaurant_name", then it works fine. So why nested field check doesn't work? How to get it work?

1 Answer 1

4

You need to use a nested query instead:

{
  "filter": {
    "query": {
      "nested": {
        "path": "menu",
        "query": {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "menu"
                }
              }
            ]
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @val for the solution
This doesn't seem to work when you invert the logic to must_not. In my use case, it returns 0 documents, although there are a lot that are missing the field
@pmishev feel free to create a new question with your specific problem.

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.