# How to use another field for query if the first does not exist value

**URL:** <https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291>\
**Category:** Elasticsearch\
**Created:** [February 13, 2020, 11:26pm UTC](https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291 "2020-02-13T23:26:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianog3](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@adrianog3](https://discuss.elastic.co/u/adrianog3)\
**Post date:** [February 13, 2020, 11:26pm UTC](https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291/1 "2020-02-13T23:26:51Z")

</div>

I would like to do a date filter on a field called "last\_update\_date", however, if the value is "null", I should use another field called "creation\_date". Is there any way to do this?

The filter for the "last\_update\_date" field is this:

"query": {  
"range": {  
"last\_update\_date": {  
"gte": "2020-02-01T23: 20: 16.537Z"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 14, 2020, 9:27am UTC](https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291/2 "2020-02-14T09:27:29Z")

</div>

hey,

how about this structure. You have a query that checks for `last_update_date` OR checks for `last_update_date` being null/not exists AND `creation_date`

```auto
bool:
  filter: [
    bool: {
      "should" : [
        { "range" : { "last_update_date"} },
        { "bool": { "must_not" : { "exists" : "last_update_date" }, "filter" : { "range" : { "creation_date"} } } }
      ]
    }
  ]

```

Note, this is on top of my head and untested, but might be worth a try. A bool query, with only should clauses, treats those as an or, so I think this should work, if not, we can reiterate.

---

<div class="post-metadata">

**Author:** ![adrianog3](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@adrianog3](https://discuss.elastic.co/u/adrianog3)\
**Post date:** [February 14, 2020, 2:45pm UTC](https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291/3 "2020-02-14T14:45:28Z")

</div>

Many thanks for the reply.

I found a solution by making a query similar to the one you made. Perhaps more complex than it should. But it was like this:

{  
"query": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"exists": {  
"field": "last\_update\_date"  
}  
},  
{  
"range": {  
"last\_update\_date": {  
"lte": "2020-01-18T01:58:03.478Z"  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"bool": {  
"must\_not": [  
{  
"exists": {  
"field": "last\_update\_date"  
}  
}  
]  
}  
},  
{  
"range": {  
"creation\_date": {  
"lte": "2020-01-18T01:58:03.478Z"  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}

---

<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:** [March 13, 2020, 2:45pm UTC](https://discuss.elastic.co/t/how-to-use-another-field-for-query-if-the-first-does-not-exist-value/219291/4 "2020-03-13T14:45:44Z")

</div>

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