# How can I query a count on documents that may not have a field set?

**URL:** <https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115>\
**Category:** Elasticsearch\
**Created:** [August 3, 2016, 2:37pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115 "2016-08-03T14:37:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xamox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xamox/32/5447_2.png) [@xamox](https://discuss.elastic.co/u/xamox)\
**Post date:** [August 3, 2016, 2:37pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/1 "2016-08-03T14:37:55Z")

</div>

I've defined my mapping with a field such as "state\_reason". What I want to do is be able to query and show all records that do not have "state\_reason" set.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [August 3, 2016, 2:41pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/2 "2016-08-03T14:41:18Z")

</div>

I think you might be looking for the [`missing` query](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_dealing_with_null_values.html#_missing_query)?

---

<div class="post-metadata">

**Author:** ![xamox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xamox/32/5447_2.png) [@xamox](https://discuss.elastic.co/u/xamox)\
**Post date:** [August 3, 2016, 5:05pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/3 "2016-08-03T17:05:37Z")

</div>

Thanks @colings86, I think that maybe what I was looking for, although I tested it and getting a strange response (index already exists).

If I do a GET on the index it works fine, and my response is:

```
{
  "jnap": {
    "aliases": {},
    "mappings": {
      "overall": {
        "properties": {
          "model": {
            "type": "string",
            "index": "not_analyzed"
          },
          "state": {
            "type": "string",
            "index": "not_analyzed"
          },
          "timestamp": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
          "vin": {
            "type": "string"
          }
        }
      },
      "inspection": {
        "properties": {
          "bypass": {
            "type": "string",
            "index": "not_analyzed"
          },
          "expected": {
            "type": "string",
            "index": "not_analyzed"
          },
          "expected color": {
            "type": "string"
          },
          "found": {
            "type": "string"
          },
          "found color": {
            "type": "string"
          },
          "image": {
            "type": "string",
            "index": "not_analyzed"
          },
          "inspection": {
            "type": "string",
            "index": "not_analyzed"
          },
          "location": {
            "type": "string",
            "index": "not_analyzed"
          },
          "model": {
            "type": "string",
            "index": "not_analyzed"
          },
          "state": {
            "type": "string",
            "index": "not_analyzed"
          },
          "state_reason": {
            "type": "string",
            "index": "not_analyzed"
          },
          "timestamp": {
            "type": "date",
            "format": "strict_date_optional_time||epoch_millis"
          },
          "vehicle_color": {
            "type": "string",
            "index": "not_analyzed"
          },
          "vin": {
            "type": "string"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1469554327968",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "6UVhwuZUT-ioH6vt2ON3uA",
        "version": {
          "created": "2010299"
        }
      }
    },
    "warmers": {}
  }
}

```

But when I try with the missing clause, I get

```
GET jnap
{
  "query": {
    "constant_score": {
      "filter": {
        "missing": {
          "field": "state_reason"
        }
      }
    }
  }
}

```

Response is:

```
{
   "error": {
      "root_cause": [
         {
            "type": "index_already_exists_exception",
            "reason": "already exists",
            "index": "jnap"
         }
      ],
      "type": "index_already_exists_exception",
      "reason": "already exists",
      "index": "jnap"
   },
   "status": 400
}

```

I'm using Elasticsearch 2.1.2.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [August 3, 2016, 11:17pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/4 "2016-08-03T23:17:50Z")

</div>

Use `GET jnap/_search` instead of `GET jnap`.

---

<div class="post-metadata">

**Author:** ![xamox](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xamox/32/5447_2.png) [@xamox](https://discuss.elastic.co/u/xamox)\
**Post date:** [August 4, 2016, 12:04pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/6 "2016-08-04T12:04:59Z")

</div>

That worked @nik9000. . I've been using the python DSL library and at lot of that has been taken care of for me, so newbie slip up on my part. Thanks for the help!

---

<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:** [July 5, 2017, 10:30pm UTC](https://discuss.elastic.co/t/how-can-i-query-a-count-on-documents-that-may-not-have-a-field-set/57115/7 "2017-07-05T22:30:06Z")

</div>


