0

I am trying to write a query that will match a document with a nested object that is an array of objects. Example :

{"ListName" : "List 1", "Fruits": [ { "name" : "Apple"}, {"name": "Orange"}, {"name" : "banana"} ] }

I would like to write a query were I get the document above based on the names inside the Fruits array, e.g. Apple and Orange.

What I currently have matches names with both Apple and Orange in the same name field rather than each object.

 {
  "query": {
    "nested": {
      "path": "Fruits",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "Fruits.name": "Apple"
              }
            },
            {
              "match": {
                "Fruits.name": "Orange"
              }
            }
          ]
        }
      }
    }
  }
}

1 Answer 1

3

You need to make two different nested queries in a bool/must query instead, like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "Fruits",
            "query": {
              "term": {
                "Fruits.name": "Apple"
              }
            }
          }
        },
        {
          "nested": {
            "path": "Fruits",
            "query": {
              "match": {
                "Fruits.name": "Orange"
              }
            }
          }
        }
      ]
    }
  }
}
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.