# Query to find if a field exists in last 7 hours

**URL:** <https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056>\
**Category:** Elasticsearch\
**Created:** [August 11, 2021, 12:16pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056 "2021-08-11T12:16:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [August 11, 2021, 12:16pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/1 "2021-08-11T12:16:12Z")

</div>

I am trying to find documents containing opsgenieAction:create in the last 7 hours. I constructed the following query but somehow the result is not as expected.

can someone help to find what is wrong here?

Query :

```auto
GET my-index*/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "now-7h",
              "lt" : "now"
            }
          }
        }
      ],
      "should": [
        {
          "query_string": { "fields": [
              "opsgenieAction"
            ],
              "query": "opsgenieAction:create"
          }
        }
      ]
    }
  }
}

```

Response :

```auto
response: 
{
  "took" : 658,
  "timed_out" : false,
  "_shards" : {
    "total" : 276,
    "successful" : 276,
    "skipped" : 275,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 1:03pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/2 "2021-08-11T13:03:45Z")

</div>

You made the `opsgenieAction:create` clause optional by putting it in the `should` section.  
Place it in the `filters` section along with the other mandatory clause.

---

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [August 11, 2021, 1:12pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/3 "2021-08-11T13:12:47Z")

</div>

@Mark_Harwood thanks for replying.

do you mean like this ?

```auto
GET my-index*/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "now-7h",
              "lt": "now"
            }
          },
          "term": {
            "opsgenieAction": "create"
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 1:15pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/4 "2021-08-11T13:15:03Z")

</div>

Close. The filter array should have 2 objects `[{...}, {...}]` not one.

---

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [August 11, 2021, 1:24pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/5 "2021-08-11T13:24:28Z")

</div>

I tried the way you suggested, the query was executed successfully but again result is not as expected.

Query :

```auto
GET my-index*/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "date": {
              "gte": "now-7h",
              "lt": "now"
            }
          }
        },
       {
         "term": {
           "opsgenieAction": "create"
         }
       }
      ]
    }
  }
}

```

Response :

```auto
{
  "took" : 250,
  "timed_out" : false,
  "_shards" : {
    "total" : 276,
    "successful" : 276,
    "skipped" : 275,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 1:26pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/6 "2021-08-11T13:26:46Z")

</div>

To help further I'd need the following JSON:

1. Your index mapping
2. An example doc which you expect to match but doesn't

---

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [August 11, 2021, 1:54pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/7 "2021-08-11T13:54:10Z")

</div>

sure @Mark\_Harwood....Both are as following. I have removed some information, but field names are as it is.

Sample doc:

> <https://gist.github.com/ankitdevnalkar/805d69b867a5afd8deb79c587b1c6681#file-sample-document>

mapping of my index :

> <https://gist.github.com/ankitdevnalkar/1647dbd49f83d3972fb6c715e03dcfe2#file-index_mapping>

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 11, 2021, 2:12pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/8 "2021-08-11T14:12:57Z")

</div>

Your example document has no field at the root called `date`. Your query is expecting that

---

<div class="post-metadata">

**Author:** ![ankitdevnalkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitdevnalkar/32/46158_2.png) [@ankitdevnalkar](https://discuss.elastic.co/u/ankitdevnalkar)\
**Post date:** [August 11, 2021, 2:20pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/9 "2021-08-11T14:20:44Z")

</div>

@Mark_Harwood got it !!

yeah it works now. Thanks alot 🙏

---

<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 8, 2021, 2:21pm UTC](https://discuss.elastic.co/t/query-to-find-if-a-field-exists-in-last-7-hours/281056/10 "2021-09-08T14:21:25Z")

</div>

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