# Elasticsearch filter query with time range using java api

**URL:** <https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904>\
**Category:** Elasticsearch\
**Created:** [June 4, 2014, 11:43am UTC](https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904 "2014-06-04T11:43:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [June 4, 2014, 11:43am UTC](https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904/1 "2014-06-04T11:43:12Z")

</div>

Hi,

I have a document like below

{  
"\_index": "cpu\_usage\_metrics",  
"\_type": "cpu\_usage\_metrics",  
"\_id": "CKAAs1n8TKiR6FncC5NLGA",  
"\_score": 1,  
"\_source": {  
"status": 0,  
"occurrences": 1,  
"value": "33",  
"key": "vm.server2.cpu.usage",  
"client": "vm.server2",  
"@timestamp": "2014-06-03T20:18:19+05:30",  
"check\_name": "cpu\_usage\_metrics",  
"address": "10.203.238.138",  
"command": "cpu-usage-metrics.sh"  
}  
}

I want to do a filtered query with time range using java api like below

"filter": {  
"range": {  
"@timestamp": {  
"to": "now",  
"from": "now - 5mins"  
}  
}  
}

Please suggest how to form the Filter in java api.

Thanks,  
Subhadip

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [June 16, 2014, 12:13pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904/2 "2014-06-16T12:13:35Z")

</div>

Hey,

did you try something along the  
lines FilterBuilders.rangeFilter("@timestamp").from("now").to("now - 5d") -  
did that pose problems?

make sure you are reading this paragraph about date range filters:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--Alex

On Wed, Jun 4, 2014 at 1:43 PM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Hi,
> 
> I have a document like below
> 
> {  
> "\_index": "cpu\_usage\_metrics",  
> "\_type": "cpu\_usage\_metrics",  
> "\_id": "CKAAs1n8TKiR6FncC5NLGA",  
> "\_score": 1,  
> "\_source": {  
> "status": 0,  
> "occurrences": 1,  
> "value": "33",  
> "key": "vm.server2.cpu.usage",  
> "client": "vm.server2",  
> "@timestamp": "2014-06-03T20:18:19+05:30",  
> "check\_name": "cpu\_usage\_metrics",  
> "address": "10.203.238.138",  
> "command": "cpu-usage-metrics.sh"  
> }  
> }
> 
> I want to do a filtered query with time range using java api like below
> 
> "filter": {  
> "range": {  
> "@timestamp": {  
> "to": "now",  
> "from": "now - 5mins"  
> }  
> }  
> }
> 
> Please suggest how to form the Filter in java api.
> 
> Thanks,  
> Subhadip
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f596658e-5ec4-42ac-abc1-4f99416be101%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGCwEM\_YymPLyoy2nVmk9VAeq2RFHu7mJtufhBHCGd5Q6u9ZUw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM_YymPLyoy2nVmk9VAeq2RFHu7mJtufhBHCGd5Q6u9ZUw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [June 16, 2014, 12:27pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904/3 "2014-06-16T12:27:02Z")

</div>

Hi Alex,

Yes I tried that and it's working. Thanks 🙂

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8addde47-a57b-4a4a-aa7a-68b419bcf90e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8addde47-a57b-4a4a-aa7a-68b419bcf90e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:22am UTC](https://discuss.elastic.co/t/elasticsearch-filter-query-with-time-range-using-java-api/17904/4 "2017-07-06T01:22:11Z")

</div>


