# Correct Way to Execute Not Exists and Not Empty Query for Not\_Analyzed Fields in Nested Document

**URL:** <https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390>\
**Category:** Elasticsearch\
**Created:** [August 7, 2018, 7:46pm UTC](https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390 "2018-08-07T19:46:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shalini\_hemachandran](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@shalini\_hemachandran](https://discuss.elastic.co/u/shalini_hemachandran)\
**Post date:** [August 7, 2018, 7:46pm UTC](https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390/1 "2018-08-07T19:46:49Z")

</div>

Hi,

I have a "not\_analyzed" field as part of a nested document in my schema. A sample document is given below

```auto
"_source": {
    "identifier": 123,
    .
    .
    "nested_doc_name_xyz":[
      {
        "not_analyzed_field_name_xyz": [
           "ABCD",
           "1234",
           ""
         ]
      }
     ]
    }

```

To search for documents where not\_analyzed\_field\_name\_xyz does not exist and is not empty, the following is the query chunk I am using.

```auto
{
  "bool": {
    "filter": {
      "bool": {
        "must_not": {
          "nested": {
            "path": "nested_doc_name_xyz",
            "query": {
              "bool": {
                "must": {
                  "exists": {
                    "field": "nested_doc_name_xyz.not_analyzed_field_name_xyz"
                  }
                },
                "must_not": {
                  "term": {
                    "nested_doc_name_xyz.not_analyzed_field_name_xyz": ""
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Since the sample document I provided earlier, has an empty string as part of the whole value,

```auto
"not_analyzed_field_name_xyz": [
           "ABCD",
           "1234",
           ""
         ]

```

the search query given above is not returning the document with identifier 123.

Ideally, I want to filter out documents which

1. do not have "nested\_doc\_name\_xyz.not\_analyzed\_field\_name\_xyz" this field  
and
2. those which have value for this field as ("nested\_doc\_name\_xyz.not\_analyzed\_field\_name\_xyz": [""] or "nested\_doc\_name\_xyz.not\_analyzed\_field\_name\_xyz":"")

Please let me know if you need any more details.

---

<div class="post-metadata">

**Author:** ![RahulD](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@RahulD](https://discuss.elastic.co/u/RahulD)\
**Post date:** [August 9, 2018, 9:11pm UTC](https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390/2 "2018-08-09T21:11:07Z")

</div>

Can you post the index mappings for your sample document?

I believe the query needs to look like:

```
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          # nested query satisfying condition 1
        },
        {
          "bool": {
            "should": [
              {
                # nested condition 2 checking for blank value
              },
              {
                # nested condition 2 checking for blank array
              }
            ]
          }
        }
      ]
    }
  }
}

```

Having the index mappings might be helpful.

Thanks.

---

<div class="post-metadata">

**Author:** ![shalini\_hemachandran](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@shalini\_hemachandran](https://discuss.elastic.co/u/shalini_hemachandran)\
**Post date:** [August 14, 2018, 8:13pm UTC](https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390/3 "2018-08-14T20:13:56Z")

</div>

@RahulD

Hi Rahul,

Please find the mapping below.

```auto
"mappings": { 
  "sample" : {
    "_all" : { 
      "enabled": false
     }, 
    "dynamic" : "strict",
    "properties" : {
      "nested_doc_name_xyz" : { 
        "type" : "nested", 
        "dynamic" : "strict", 
        "properties" : {
          "not_analyzed_field_name_xyz" : { 
            "type" : "string", 
            "index" : "not_analyzed", 
            "doc_values": true 
          } 
        }
      } 
   } 
}

```

The query you suggested still might not work since as I mentioned earlier, for a document which has the following value

```auto
"not_analyzed_field_name_xyz": [
           "ABCD",
           "1234",
           ""
         ]

```

the following query

```auto
must_not": {
  "term": {
    "nested_doc_name_xyz.not_analyzed_field_name_xyz": ""
  }
}

```

returns true. But I am looking for a query which returns true ONLY when the value for the field's value is just "".

for example,

```auto
"not_analyzed_field_name_xyz": [
           ""
         ]

```

It has an empty string ("") as the only constituent. This document should be true whereas

```auto
"not_analyzed_field_name_xyz": [
           "ABCD",
           "1234",
           ""
         ]

```

This should be false

---

<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:** [September 11, 2018, 8:13pm UTC](https://discuss.elastic.co/t/correct-way-to-execute-not-exists-and-not-empty-query-for-not-analyzed-fields-in-nested-document/143390/4 "2018-09-11T20:13:57Z")

</div>

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