# How to query documents than not include nested objects field

**URL:** <https://discuss.elastic.co/t/how-to-query-documents-than-not-include-nested-objects-field/318583>\
**Category:** Elasticsearch\
**Created:** [November 9, 2022, 6:57pm UTC](https://discuss.elastic.co/t/how-to-query-documents-than-not-include-nested-objects-field/318583 "2022-11-09T18:57:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volna13](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/volna13/32/113178_2.png) [@Volna13](https://discuss.elastic.co/u/Volna13)\
**Post date:** [November 9, 2022, 6:57pm UTC](https://discuss.elastic.co/t/how-to-query-documents-than-not-include-nested-objects-field/318583/1 "2022-11-09T18:57:49Z")

</div>

I have a psaInfo field which is nested.  
It is optional and not available on all events, how can I write a query to get all events where there is no psaInfo field?  
Here is what I have tried

1. The impression is that the elastic does not react to the condition at all. I see in the response documents that have psaInfo

```auto
{
  "_source": [
    "time", "psaInfo", "alertStatus"
  ],
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "nested": {
                  "path": "psaInfo",
                  "query": {
                    "exists": {
                      "field": "psaInfo"
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": {
        "range": {
          "time": {
            "gte": "2022-10-09"
          }
        }
      }
    }
  },
  "sort": [
    {
      "time": "desc"
    }
  ],
  "size": 1000
}

```

1. I was sure that this query would work since I had used a similar one elsewhere to get documents where not exist psaInfo.type

But when I run it I get an empty response (an empty hits array)

```auto
GET /dev_events*/_search
{
  "_source": [
    "time"
  ],
  "query": {
    "bool": {
      "must": {
        "nested": {
          "query": {
            "bool": {
              "minimum_should_match": 1,
              "should": [
                {
                  "bool": {
                    "must_not": {
                      "exists": {
                        "field": "psaInfo"
                      }
                    }
                  }
                }
              ]
            }
          },
          "path": "psaInfo"
        }
      },
      "filter": {
        "range": {
          "time": {
            "gte": "2022-01-09"
          }
        }
      }
    }
  },
  "sort": [
    {
      "time": "desc"
    }
  ],
  "size": 1000
}

```

1. While I was reading different things here, I found out that there is also a flag "ignore\_unmapped": true, I tried to use it, but why did I get some random 75 events, although I know that there should be much more of them

```auto
GET /dev_events*/_search
{
  "_source": [
    "time", "psaInfo", "alertStatus"
  ],
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "nested": {
                  "path": "psaInfo",
                  "ignore_unmapped": true,
                  "query": {
                    "exists": {
                      "field": "psaInfo"
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "filter": {
        "range": {
          "time": {
            "gte": "2022-10-09"
          }
        }
      }
    }
  },
  "sort": [
    {
      "time": "desc"
    }
  ],
  "size": 1000
}

```

Help me please. Thanks

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [November 9, 2022, 8:41pm UTC](https://discuss.elastic.co/t/how-to-query-documents-than-not-include-nested-objects-field/318583/2 "2022-11-09T20:41:43Z")

</div>

Hi @Volna13

My example return only doc id 2 because it doesn't have field psaInfo.

```auto
PUT idx_nested
{
  "mappings": {
    "properties": {
      "psaInfo":{
        "type": "nested"
      }
    }
  }
}

POST idx_nested/_doc/1
{
  "alertStatus": 1,
  "psaInfo": {
    "message":"anu text"
  }
}

POST idx_nested/_doc/2
{
  "alertStatus": 2
}

GET idx_nested/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "psaInfo",
            "query": {
              "bool": {
                "must": [
                  {
                    "exists": {
                      "field": "psaInfo"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<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 7, 2022, 8:42pm UTC](https://discuss.elastic.co/t/how-to-query-documents-than-not-include-nested-objects-field/318583/3 "2022-12-07T20:42:25Z")

</div>

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