# How to query by field value and date

**URL:** <https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835>\
**Category:** Elastic Search\
**Created:** [June 4, 2020, 6:21pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835 "2020-06-04T18:21:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@nino](https://discuss.elastic.co/u/nino)\
**Post date:** [June 4, 2020, 6:21pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/1 "2020-06-04T18:21:14Z")

</div>

Hello all,

i am trying to create this query in python but i can't find out the correct syntax. I would like to filter by date greater than and field value:

```auto
       query_body = {
                   "size": 100,
                   "query": { 
                       "match" : { "account_value":"ATT11111" }
                       "range": {
                           "@timestamp": {
                               "gt":str(last_trackrecord_day)
                           }
                       }
                   } 
               }

```

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [June 4, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/2 "2020-06-04T18:36:26Z")

</div>

I am not expert in python but you define this body just like you have and call it

response = elastic\_client.search(index="some\_index", body=query\_body)

where elastic\_client is another file handle

elastic\_client = Elasticsearch('elkm01', http\_auth=('user', 'password'), port=9200)

---

<div class="post-metadata">

**Author:** ![nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@nino](https://discuss.elastic.co/u/nino)\
**Post date:** [June 4, 2020, 6:52pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/3 "2020-06-04T18:52:19Z")

</div>

Thanks elasticforme, sorry, i didn't explain well. This Query works:

```auto
query_body = {
                   "size": 100,
                   "query": { 
                       "range": {
                           "@timestamp": {
                               "gt":str(last_trackrecord_day)
                           }
                       }
                   } 
               }

```

This query it doesn't, sure my syntax is wrong.

````auto
query_body = {
                   "size": 100,
                   "query": { 
                       "match" : { "account_value":"ATT11111" }
                       "range": {
                           "@timestamp": {
                               "gt":str(last_trackrecord_day)
                           }
                       }
                   } 
               }
```
````

---

<div class="post-metadata">

**Author:** ![nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@nino](https://discuss.elastic.co/u/nino)\
**Post date:** [June 4, 2020, 8:28pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/4 "2020-06-04T20:28:21Z")

</div>

This try it fails too:

```auto
query_body = {
            "size": 100,
            "query": {  
                "bool": {
                    "must": [
                        {
                            "match" : {
                                "account_value": "ATT11111"
                            }
                        },
                        {                       
                            "range": {
                                "@timestamp": {
                                    "gt":str(last_trackrecord_day)
                                }
                            }
                        }
                    ]
                }               
            }
        }

```

Error:

```auto
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query

```

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [June 4, 2020, 8:36pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/5 "2020-06-04T20:36:56Z")

</div>

then it is just matter of figureing out what is wrong.  
go to dev tool on kibana and post this there and work it out

[https://discuss.elastic.co/t/combining-bool-and-range-queries/82729](https://discuss.elastic.co/t/combining-bool-and-range-queries/82729)

---

<div class="post-metadata">

**Author:** ![nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@nino](https://discuss.elastic.co/u/nino)\
**Post date:** [June 4, 2020, 9:39pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/6 "2020-06-04T21:39:41Z")

</div>

Thanks Sachin,

it seems the text value, why?:

```auto
"type": "number_format_exception",
            "reason": "For input string: \"ATT11111\""

```

---

<div class="post-metadata">

**Author:** ![nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@nino](https://discuss.elastic.co/u/nino)\
**Post date:** [June 4, 2020, 9:52pm UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/7 "2020-06-04T21:52:53Z")

</div>

Solved, it was my fault. The name field was incorrect with another field with numeric type. Thank you so much

---

<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:** [November 4, 2022, 8:01am UTC](https://discuss.elastic.co/t/how-to-query-by-field-value-and-date/235835/8 "2022-11-04T08:01:12Z")

</div>


