# Exists query for field that has only 1 nested child not working

**URL:** <https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897>\
**Category:** Elasticsearch\
**Created:** [March 31, 2021, 11:06am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897 "2021-03-31T11:06:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiDe](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MiDe](https://discuss.elastic.co/u/MiDe)\
**Post date:** [March 31, 2021, 11:06am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/1 "2021-03-31T11:06:20Z")

</div>

Trying to query entries based on a mapping like this:

```auto
{"top":
{
"properties":
{
"doc": {
            "properties": {
             "A": {
                "properties": {
                  "B": {
                    "type": "nested",
                    "dynamic": "strict",
                    "properties": {
                      "C": {
                        "type": "keyword"
                      },
                      "D": {
                        "type": "keyword"
                      },                      
                    }
                  }
                }
              }
            }
          }
}
}

```

with a query like this:

```auto
{"query":{"bool":{"filter":[{"exists":{"field":"top.doc.A" }}]}}, "from":0,"size":1000,"sort":[{"Name":{"order":"asc"}}],"track_total_hits":true}

```

and don't get hits but if I try:

```auto
{"query":{"bool":{"filter":[{"exists":{"field":"top.doc" }}]}}, "from":0,"size":1000,"sort":[{"Name":{"order":"asc"}}],"track_total_hits":true}

```

I get the entries that I know I have. What am I doing wrong ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2021, 8:06am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/2 "2021-04-02T08:06:49Z")

</div>

Welcome!

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can copy and paste in Kibana dev console, click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![MiDe](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MiDe](https://discuss.elastic.co/u/MiDe)\
**Post date:** [April 19, 2021, 6:13am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/3 "2021-04-19T06:13:05Z")

</div>

Create mapping:

```auto
  PUT index/_mapping
   {"properties":{"Documents":{"properties":{"Name1":{"properties":{"Parent":{"properties":{"Objects":{"type":"nested","dynamic":"strict","properties":{"Adjective":{"type":"keyword"}}}}}}}}}}}

```

Populate index:

```auto
    PUT index/_doc/<someId>
    {

        "Name1": {

            "Parent": {

                "Objects": [
                    {
                        "Adjective": "Many"
                    },
                    {
                        "Adjective": "Few"
                    },

                    {
                        "Adjective": "Few"
                    }
                ]

            }
        }
    }

```

Querying this with:

```auto
    POST index/_search
    {"query":{"bool":{"filter":[{"exists":{"field":"Documents.Name1.Parent"}}]}},"_source":{"includes":["Documents.*"]},"from":0,"size":1000,"sort":[{"Name":{"order":"asc"}}],"track_total_hits":true}

```

Will get 0 results. while querying with this, or a nested search over the nested properties

```auto
   POST index/_search
   {"query":{"bool":{"filter":[{"exists":{"field":"Documents.Name1"}}]}},"_source":{"includes":["Documents.*"]},"from":0,"size":1000,"sort":[{"Name":{"order":"asc"}}],"track_total_hits":true}

```

will get me the populated example

---

<div class="post-metadata">

**Author:** ![Jason\_Slater](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_slater/32/63484_2.png) [@Jason\_Slater](https://discuss.elastic.co/u/Jason_Slater)\
**Post date:** [April 19, 2021, 7:26am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/4 "2021-04-19T07:26:07Z")

</div>

Just to be sure I understand, I recreated your example:

create index:

```
PUT nested-test

```

create mappings:

```
PUT nested-test/_mapping
{
  "properties": {
    "Documents": {
      "properties": {
        "Name1": {
          "properties": {
            "Parent": {
              "properties": {
                "Objects": {
                  "type": "nested",
                  "dynamic": "strict",
                  "properties": {
                    "Adjective": {
                      "type": "keyword"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Now insert a few test documents:

```
PUT nested-test/_doc/123
{
  "Documents": {
    "Name1": {
      "Parent": {
        "Objects": [
          {
            "Adjective": "Many"
          },
          {
            "Adjective": "Few"
          },
          {
            "Adjective": "Few"
          }
        ]
      }
    }
  }
}
PUT nested-test/_doc/456
{
  "Documents": {
    "Name1": {
      "Parent": {
        "Objects": [
        ]
      }
    }
  }
}
PUT nested-test/_doc/789
{
  "Documents": {
    "Name1": {
      "Parent": {
        "Objects": [
          {
            "Adjective": "foo"
          }
        ]
      }
    }
  }
}

```

Now let's do a query to find docs which have child objects

```
GET nested-test/_search
{
  "query": {
    "nested": {
      "path": "Documents.Name1.Parent.Objects",
      "query": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "Documents.Name1.Parent.Objects"
              }
            }
          ]
        }
      }
    }
  }
}

```

in this example, docs 123 and 789 are returned, but 456 is not.

---

<div class="post-metadata">

**Author:** ![MiDe](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@MiDe](https://discuss.elastic.co/u/MiDe)\
**Post date:** [April 19, 2021, 7:39am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/5 "2021-04-19T07:39:20Z")

</div>

Not exactly. The query is done on :

```auto
`{ "exists": { "field": "Documents.Name1.Parent" } }`

```

and will return no results

---

<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:** [May 17, 2021, 7:39am UTC](https://discuss.elastic.co/t/exists-query-for-field-that-has-only-1-nested-child-not-working/268897/6 "2021-05-17T07:39:36Z")

</div>

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