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

Trying to query entries based on a mapping like this:

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

with a query like this:

{"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:

{"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 ?

Welcome!

Could you provide a full recreation script as described in About the Elasticsearch category. 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.

Create mapping:

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

Populate index:

    PUT index/_doc/<someId>
    {

        "Name1": {

            "Parent": {

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

                    {
                        "Adjective": "Few"
                    }
                ]

            }
        }
    }

Querying this with:

    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

   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

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.

Not exactly. The query is done on :

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

and will return no results

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