# Best way to query nested objects in indexes where they don't exist

**URL:** <https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923>\
**Category:** Elasticsearch\
**Created:** [November 22, 2018, 6:10pm UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923 "2018-11-22T18:10:19Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 27, 2018, 1:30pm UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/2 "2018-11-27T13:30:07Z")

</div>

The key to what you want to do is the `ignore_unmapped` option of the `nested query`. It tells Elasticsearch to ignore indexes where the nested path has not been mapped, rather than return an error for those indexes. You can make use of that to separately query indexes with and without a nested type.

Let's say you have two indexes. "my\_index1" in which a nested type `my_nested_object` has been mapped, and "my\_index2" where it has not:

```auto
PUT my_index1
{
  "mappings": {
    "_doc": {
      "properties": {
        "my_nested_object": {
          "type": "nested",
          "properties": {
            "foo": {
              "type": "keyword"
            }
          }
        },
        "bar": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index2
{
  "mappings": {
    "_doc": {
      "properties": {
        "bar": {
          "type": "keyword"
        }
      }
    }
  }
}

```

Now, let's index a document in each of these two indexes:

```auto
PUT my_index1/_doc/1
{
  "my_nested_object": {
    "foo": "123"
  },
  "bar": "456"
}

PUT my_index2/_doc/2
{
  "bar": "456"
}

```

We can now query across these two indexes using a nested query with `ignore_unmapped` set to `true` to silently ignore the index where I do not have a nested object `my_nested_object`:

```auto
GET my_index1,my_index2/_search
{
  "query": {
    "nested": {
      "path": "my_nested_object",
      "ignore_unmapped": true,
      "query": {
        "match": {
          "my_nested_object.foo": "123"
        }
      }
    }
  }
}

```

I can also do the reverse: query only those indexes that do not have a nested object `my_nested_object`. I can use the `exists` query for that, which I wrap in a `bool` query's `must_not`:

```auto
GET my_index1,my_index2/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "my_nested_object",
            "ignore_unmapped": true,
            "query": {
              "exists": {
                "field": "my_nested_object"
              }
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "bar": "456"
          }
        }
      ]
    }
  }
}

```

Both queries can be combined to get your desired OR-like behavior by wrapping them in a `bool` query's `should` clause:

```auto
GET my_index1,my_index2/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "my_nested_object",
            "ignore_unmapped": true,
            "query": {
              "match": {
                "my_nested_object.foo": "123"
              }
            }
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "nested": {
                  "path": "my_nested_object",
                  "ignore_unmapped": true,
                  "query": {
                    "exists": {
                      "field": "my_nested_object"
                    }
                  }
                }
              }
            ],
            "must": [
              {
                "match": {
                  "bar": "456"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

It's not pretty, but it works 😃

---

_[View the full topic](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923)._
