# Search docs with (term = "val" OR not\_exists)

**URL:** <https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687>\
**Category:** Elasticsearch\
**Created:** [June 5, 2018, 7:46pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687 "2018-06-05T19:46:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chief06](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chief06](https://discuss.elastic.co/u/chief06)\
**Post date:** [June 5, 2018, 7:46pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/1 "2018-06-05T19:46:12Z")

</div>

I have a \_search query running that checks certain properties and aggregates values based on the search. One of the search criteria is:  
{ "term": { "Status": 0} }

"Status" was a property added after documents were already indexed. By default, I treat Status = 0. So I'm trying to search for documents where Status = 0 OR the property doesn't exist in which case I want to treat as 0 anyways.

Is there a way to structure my search query to handle this? Or possibly set a default value when adding a new property to a mapping?

---

<div class="post-metadata">

**Author:** ![chief06](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chief06](https://discuss.elastic.co/u/chief06)\
**Post date:** [June 5, 2018, 8:34pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/2 "2018-06-05T20:34:05Z")

</div>

I was able to accomplish this by using a "script" like so:

{ "script": { "script": { "source": "doc['Status'].empty || doc['Status'].value == 0" } } }

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 5, 2018, 8:39pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/3 "2018-06-05T20:39:04Z")

</div>

> [@chief06](#):
>
> I was able to accomplish this by using a "script" like so:

This is not search, this is full index scan. If you want to implement it as a search you can combine together your term query with [exists](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-exists-query.html#_literal_missing_literal_query) query, using [bool](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-bool-query.html) query.

---

<div class="post-metadata">

**Author:** ![chief06](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chief06](https://discuss.elastic.co/u/chief06)\
**Post date:** [June 5, 2018, 9:27pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/4 "2018-06-05T21:27:20Z")

</div>

@Igor_Motov Hmmm...ok. Can you elaborate a little on that? I couldn't figure out how to have an OR using the bool query. Here is the full example of what I'm trying to accomplish. I want to find all documents with a CloseDate = '2018-02-01' and Status = 0. But documents that were indexed prior to adding Status don't have that property. So currently it's filtering those documents out

> ```
> "query": {
> "bool": {
> "must": [{
> "term": { "CloseDate": "2018-02-01" }
> }, {
> "term": { "Status": 0 }
> }]
> }
> },
> "aggs": {
> "GrossSales": {
> "sum": { "field": "GrossSales" }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![chief06](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chief06](https://discuss.elastic.co/u/chief06)\
**Post date:** [June 5, 2018, 9:28pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/5 "2018-06-05T21:28:52Z")

</div>

This is how I was using the script

> ```
> "query": {
> "bool": {
> "must": [{
> "term": { "CloseDate": "2018-02-01" }
> }, {
> "script": {
> "script": {
> "source": "(doc['Status'].empty || doc['Status'].value == 0)"
> }
> }
> }]
> }
> },
> "aggs": {
> "GrossSales": {
> "sum": { "field": "GrossSales" }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 5, 2018, 10:47pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/6 "2018-06-05T22:47:45Z")

</div>

```auto
PUT test/doc/1
{
  "CloseDate": "2018-02-01",
  "Status": 0,
  "GrossSales": 100
}

PUT test/doc/2
{
  "CloseDate": "2018-02-02",
  "Status": 0,
  "GrossSales": 200
}

PUT test/doc/3
{
  "CloseDate": "2018-02-01",
  "GrossSales": 200
}

PUT test/doc/4
{
  "CloseDate": "2018-02-01",
  "Status": 1,
  "GrossSales": 400
}

POST test/doc/_search
{
  "query": {
    "bool": {
      "must": [{ "term": { "CloseDate": "2018-02-01" } }],
      "should": [
        { "term": { "Status": 0 } },
        {
          "bool": {
            "must_not": [{ "exists": { "field": "Status" } }]
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "aggs": {
    "GrossSales": {
      "sum": {
        "field": "GrossSales"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![chief06](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@chief06](https://discuss.elastic.co/u/chief06)\
**Post date:** [June 6, 2018, 4:27pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/7 "2018-06-06T16:27:39Z")

</div>

@Igor_Motov Thank you!!! That makes perfect sense

---

<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 4, 2018, 4:27pm UTC](https://discuss.elastic.co/t/search-docs-with-term-val-or-not-exists/134687/8 "2018-07-04T16:27:48Z")

</div>

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