# 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:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![wparad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wparad/32/37992_2.png) [@wparad](https://discuss.elastic.co/u/wparad)
#### Post date: [November 22, 2018, 6:10pm UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/1 "2018-11-22T18:10:20Z")

</div>

Right now it seems like I need to create a new nested property and add that to every index I have. Otherwise the "[nested] failed to find nested object under path [MY\_OBJECT\_PATH]"

This topic which was closed:

> [@How to avoid "failed to find nested object under path" when searching over many indexes?](https://discuss.elastic.co/t/how-to-avoid-failed-to-find-nested-object-under-path-when-searching-over-many-indexes/7804):
>
> Hi, is it possible to search on indexes that have no nested objects in the mapping when searching with a nested query over many indexes? I get the following error message "failed to find nested object under path". The situation is, some indexes have a nested object "dc:subject.xbib:subject", some not. I would expect a successful search. It would be nice to fail silently on those indexes where the mapping is different, even if another mapping such as an un-nested field is present. To work…

and this issue applies:

> <https://github.com/elastic/elasticsearch/issues/33644>
>
> \*\*Elasticsearch version\*\* (\`bin/elasticsearch --version\`): 6.4.0
> 
> \*\*Plugins in…stalled\*\*: \[\]
> 
> \*\*JVM version\*\* (\`java -version\`): 1.8.0\_171
> 
> \*\*OS version\*\* (\`uname -a\` if on a Unix-like system): Windows 10
> 
> \*\*Description of the problem including expected versus actual behavior\*\*:
> 
> Field \`unmapped\_type\` in \*\*sort\*\* section inside \*\*query\*\* doesn't work properly in case of nested properties. If I query for the data sorted by non-existing (unmapped) nested field I get \`query\_shard\_exception\`. The behavior I expect is to sort the data without nested field by a default value of a type specified in \`unmapped\_type\` field. It would be consistent with current behavior for non-nested fields.
> 
> \*\*Steps to reproduce\*\*:
> 
> Let's say I have 2 indices (One with nested field and one without):
> 
> \`\`\`javascript
> PUT index-nested 
> {
> "mappings": {
> "doc": {
> "properties": {
> "notNestedField": {
> "type": "keyword"
> },
> "Nested": {
> "type": "nested",
> "properties": {
> "nestedField": {
> "type": "keyword"
> }
> }
> }
> }
> }
> }
> }
> 
> PUT index-not-nested
> {
> "mappings": {
> "doc": {
> "properties": {
> "notNestedField": {
> "type": "keyword"
> }
> }
> }
> }
> }
> \`\`\`
> 
> The query for sorted data from both indices:
> 
> \`\`\`javascript
> GET index-nested,index-not-nested/\_search
> {
> "sort": \[
> {
> "Nested.nestedField": {
> "unmapped\_type" : "keyword",
> "nested": {
> "path": "Nested"
> }, 
> "order": "desc"
> }
> }
> \]
> }
> \`\`\`
> 
> Only data from \*\*index-nested\*\* are returned, all shards belonging to indices without nested field are failing:
> 
> \`\`\`javascript
> ...
> "failures": \[
> {
> "shard": 0,
> "index": "index-not-nested",
> "node": "w8\_R6Fg8TLGsKq7zCRAXKg",
> "reason": {
> "type": "query\_shard\_exception",
> "reason": "\[nested\] failed to find nested object under path \[Nested\]",
> "index\_uuid": "1D0UTBwaRHmnG2TC6PJZdg",
> "index": "index-not-nested"
> }
> }
> \]
> ...
> \`\`\`

I want to make an OR query by the nested object property or a different property, and have the query not fail if the nested object doesn't exist yet.

How do I do that?

---

<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 😃

---

<div class="post-metadata">

### Author: ![wparad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wparad/32/37992_2.png) [@wparad](https://discuss.elastic.co/u/wparad)
#### Post date: [December 3, 2018, 9:29am UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/3 "2018-12-03T09:29:44Z")

</div>

I'm still getting the same problem. I'm on Elastic Search version 6.0. Is that a new feature?

---

<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: [December 3, 2018, 10:04am UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/4 "2018-12-03T10:04:51Z")

</div>

Maybe you can share your mapping and the query that you are trying to execute?

---

<div class="post-metadata">

### Author: ![wparad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wparad/32/37992_2.png) [@wparad](https://discuss.elastic.co/u/wparad)
#### Post date: [December 3, 2018, 11:49am UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/5 "2018-12-03T11:49:39Z")

</div>

Thanks for helping, I figured out what is going on. Basically I tried to run the nested query on a mapping that already exists and is NOT nested. I would have expected it to not give that error, but it still does. I was hoping to convert the property from the current mapping to nested, but it seems I will have to use two different fields.

For instance if you had

```auto
PUT my_index2/_doc/2
{
  "foo": "123",
  "bar": "456"
}

```

Then I would expect the subsequent nested search to fail.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 31, 2018, 11:49am UTC](https://discuss.elastic.co/t/best-way-to-query-nested-objects-in-indexes-where-they-dont-exist/157923/6 "2018-12-31T11:49:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
