# DSL: query + numeric range filter + sort

**URL:** <https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731>\
**Category:** Elasticsearch\
**Created:** [November 15, 2012, 6:57pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731 "2012-11-15T18:57:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [November 15, 2012, 6:57pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/1 "2012-11-15T18:57:03Z")

</div>

Hi there,

How exactly would a DSL query look if it contained both a query (that  
contains anything, e.g. "dt:123456 user:john login -200") and a  
numeric\_range on the dt field.

There's no need for scoring, but there is for sorting (on the "dt" field  
descending).

Should I use this one?  
[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)

If so, could anyone provide an example of my use case?

Thank you in advance!

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

[http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

--

---

<div class="post-metadata">

**Author:** ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)\
**Post date:** [November 15, 2012, 9:43pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/2 "2012-11-15T21:43:25Z")

</div>

Hi Robin,

Couple of options for how to do this.

If your numeric\_range is a pretty static value (and consequently would  
benefit from caching), I recommend you make use  
of [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
Put your query\_string as the query, and the numeric\_range as a range  
filter, much like the example shows.

Alternatively if you don't want to use a filter, you could simple use  
a [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
with your query as one clause and your range as another.

At any stage if you want to skip scoring, you can wrap a query in  
a [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html).

On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:

> Hi there,
> 
> How exactly would a DSL query look if it contained both a query (that  
> contains anything, e.g. "dt:123456 user:john login -200") and a  
> numeric\_range on the dt field.
> 
> There's no need for scoring, but there is for sorting (on the "dt" field  
> descending).
> 
> Should I use this one?  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> 
> If so, could anyone provide an example of my use case?
> 
> Thank you in advance!
> 
> Best regards,
> 
> Robin Verlangen  
> _Software engineer_  
> \*  
> \*  
> W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> E ro...@us2.nl \<javascript:\>
> 
> [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> 
> Disclaimer: The information contained in this message and attachments is  
> intended solely for the attention and use of the named addressee and may be  
> confidential. If you are not the intended recipient, you are reminded that  
> the information remains the property of the sender. You must not use,  
> disclose, distribute, copy, print or rely on this e-mail. If you have  
> received this message in error, please contact the sender immediately and  
> irrevocably delete this message and any copies.

--

---

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [November 19, 2012, 8:40pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/3 "2012-11-19T20:40:06Z")

</div>

Hi Chris,

Have been struggling with this for a couple of days, but can't get it  
right. Could you please give me an example (something that works when I  
fire it copy/past into a req. body)? I think I miss something fundamental.

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

[http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

On Thu, Nov 15, 2012 at 10:43 PM, Chris Male [gento0nz@gmail.com](mailto:gento0nz@gmail.com) wrote:

> Hi Robin,
> 
> Couple of options for how to do this.
> 
> If your numeric\_range is a pretty static value (and consequently would  
> benefit from caching), I recommend you make use of  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
> Put your query\_string as the query, and the numeric\_range as a range  
> filter, much like the example shows.
> 
> Alternatively if you don't want to use a filter, you could simple use a  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.htmlwith) your query as one clause and your range as another.
> 
> At any stage if you want to skip scoring, you can wrap a query in a  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> .
> 
> On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:
> 
> > Hi there,
> > 
> > How exactly would a DSL query look if it contained both a query (that  
> > contains anything, e.g. "dt:123456 user:john login -200") and a  
> > numeric\_range on the dt field.
> > 
> > There's no need for scoring, but there is for sorting (on the "dt" field  
> > descending).
> > 
> > Should I use this one? [http://www.elasticsearch](http://www.elasticsearch).\*\*  
> > org/guide/reference/query-dsl/\*\*query-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> > 
> > If so, could anyone provide an example of my use case?
> > 
> > Thank you in advance!
> > 
> > Best regards,
> > 
> > Robin Verlangen  
> > _Software engineer_  
> > \*  
> > \*  
> > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > E ro...@us2.nl
> > 
> > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > 
> > Disclaimer: The information contained in this message and attachments is  
> > intended solely for the attention and use of the named addressee and may be  
> > confidential. If you are not the intended recipient, you are reminded that  
> > the information remains the property of the sender. You must not use,  
> > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > received this message in error, please contact the sender immediately and  
> > irrevocably delete this message and any copies.
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [November 19, 2012, 10:18pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/4 "2012-11-19T22:18:07Z")

</div>

The numeric range is like this:

{"query":{"constant\_score":{"filter":{"numeric\_range":{"dt":{"from":0,"to":1353363346}}}}}}

However I have no idea how to add the query string to this structure: when  
I change something it doesn't turn any response and the log is empty. For  
example:

{"query":{"constant\_score":{"filter":{"numeric\_range":{"dt":{"from":0,"to":1353363459}}},"query":{"query\_string":{"\_all":"\*"}}}}}

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

[http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

On Mon, Nov 19, 2012 at 9:40 PM, Robin Verlangen [robin@us2.nl](mailto:robin@us2.nl) wrote:

> Hi Chris,
> 
> Have been struggling with this for a couple of days, but can't get it  
> right. Could you please give me an example (something that works when I  
> fire it copy/past into a req. body)? I think I miss something fundamental.
> 
> Best regards,
> 
> Robin Verlangen  
> _Software engineer_  
> \*  
> \*  
> W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> E robin@us2.nl
> 
> [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> 
> Disclaimer: The information contained in this message and attachments is  
> intended solely for the attention and use of the named addressee and may be  
> confidential. If you are not the intended recipient, you are reminded that  
> the information remains the property of the sender. You must not use,  
> disclose, distribute, copy, print or rely on this e-mail. If you have  
> received this message in error, please contact the sender immediately and  
> irrevocably delete this message and any copies.
> 
> On Thu, Nov 15, 2012 at 10:43 PM, Chris Male [gento0nz@gmail.com](mailto:gento0nz@gmail.com) wrote:
> 
> > Hi Robin,
> > 
> > Couple of options for how to do this.
> > 
> > If your numeric\_range is a pretty static value (and consequently would  
> > benefit from caching), I recommend you make use of  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
> > Put your query\_string as the query, and the numeric\_range as a range  
> > filter, much like the example shows.
> > 
> > Alternatively if you don't want to use a filter, you could simple use a  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.htmlwith) your query as one clause and your range as another.
> > 
> > At any stage if you want to skip scoring, you can wrap a query in a  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> > .
> > 
> > On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:
> > 
> > > Hi there,
> > > 
> > > How exactly would a DSL query look if it contained both a query (that  
> > > contains anything, e.g. "dt:123456 user:john login -200") and a  
> > > numeric\_range on the dt field.
> > > 
> > > There's no need for scoring, but there is for sorting (on the "dt" field  
> > > descending).
> > > 
> > > Should I use this one? [http://www.elasticsearch](http://www.elasticsearch).\*\*  
> > > org/guide/reference/query-dsl/\*\*query-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> > > 
> > > If so, could anyone provide an example of my use case?
> > > 
> > > Thank you in advance!
> > > 
> > > Best regards,
> > > 
> > > Robin Verlangen  
> > > _Software engineer_  
> > > \*  
> > > \*  
> > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > E ro...@us2.nl
> > > 
> > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > 
> > > Disclaimer: The information contained in this message and attachments is  
> > > intended solely for the attention and use of the named addressee and may be  
> > > confidential. If you are not the intended recipient, you are reminded that  
> > > the information remains the property of the sender. You must not use,  
> > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > received this message in error, please contact the sender immediately and  
> > > irrevocably delete this message and any copies.
> > > 
> > > --

--

---

<div class="post-metadata">

**Author:** ![Chris\_Male](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_male/32/2607_2.png) [@Chris\_Male](https://discuss.elastic.co/u/Chris_Male)\
**Post date:** [November 20, 2012, 3:27am UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/5 "2012-11-20T03:27:20Z")

</div>

Hi Robin,

I just threw together an example which you'll need to edit to meet your  
needs, [ElasticSearch DSL and NumericRangeQuery · GitHub](https://gist.github.com/4115758).

On Tuesday, November 20, 2012 11:18:14 AM UTC+13, Robin Verlangen wrote:

> The numeric range is like this:
> 
> {"query":{"constant\_score":{"filter":{"numeric\_range":{"dt":{"from":0,"to":1353363346}}}}}}
> 
> However I have no idea how to add the query string to this structure: when  
> I change something it doesn't turn any response and the log is empty. For  
> example:
> 
> {"query":{"constant\_score":{"filter":{"numeric\_range":{"dt":{"from":0,"to":1353363459}}},"query":{"query\_string":{"\_all":"\*"}}}}}
> 
> Best regards,
> 
> Robin Verlangen  
> _Software engineer_  
> \*  
> \*  
> W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> E ro...@us2.nl \<javascript:\>
> 
> [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> 
> Disclaimer: The information contained in this message and attachments is  
> intended solely for the attention and use of the named addressee and may be  
> confidential. If you are not the intended recipient, you are reminded that  
> the information remains the property of the sender. You must not use,  
> disclose, distribute, copy, print or rely on this e-mail. If you have  
> received this message in error, please contact the sender immediately and  
> irrevocably delete this message and any copies.
> 
> On Mon, Nov 19, 2012 at 9:40 PM, Robin Verlangen \<ro...@us2.nl\<javascript:\>
> 
> > wrote:
> 
> > Hi Chris,
> > 
> > Have been struggling with this for a couple of days, but can't get it  
> > right. Could you please give me an example (something that works when I  
> > fire it copy/past into a req. body)? I think I miss something fundamental.
> > 
> > Best regards,
> > 
> > Robin Verlangen  
> > _Software engineer_  
> > \*  
> > \*  
> > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > E ro...@us2.nl \<javascript:\>
> > 
> > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > 
> > Disclaimer: The information contained in this message and attachments is  
> > intended solely for the attention and use of the named addressee and may be  
> > confidential. If you are not the intended recipient, you are reminded that  
> > the information remains the property of the sender. You must not use,  
> > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > received this message in error, please contact the sender immediately and  
> > irrevocably delete this message and any copies.
> > 
> > On Thu, Nov 15, 2012 at 10:43 PM, Chris Male \<[gent...@gmail.com](mailto:gent...@gmail.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > Hi Robin,
> > > 
> > > Couple of options for how to do this.
> > > 
> > > If your numeric\_range is a pretty static value (and consequently would  
> > > benefit from caching), I recommend you make use of  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
> > > Put your query\_string as the query, and the numeric\_range as a range  
> > > filter, much like the example shows.
> > > 
> > > Alternatively if you don't want to use a filter, you could simple use a  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.htmlwith) your query as one clause and your range as another.
> > > 
> > > At any stage if you want to skip scoring, you can wrap a query in a  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> > > .
> > > 
> > > On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:
> > > 
> > > > Hi there,
> > > > 
> > > > How exactly would a DSL query look if it contained both a query (that  
> > > > contains anything, e.g. "dt:123456 user:john login -200") and a  
> > > > numeric\_range on the dt field.
> > > > 
> > > > There's no need for scoring, but there is for sorting (on the "dt"  
> > > > field descending).
> > > > 
> > > > Should I use this one? [http://www.elasticsearch](http://www.elasticsearch).\*\*  
> > > > org/guide/reference/query-dsl/\*\*query-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> > > > 
> > > > If so, could anyone provide an example of my use case?
> > > > 
> > > > Thank you in advance!
> > > > 
> > > > Best regards,
> > > > 
> > > > Robin Verlangen  
> > > > _Software engineer_  
> > > > \*  
> > > > \*  
> > > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > > E ro...@us2.nl
> > > > 
> > > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > > 
> > > > Disclaimer: The information contained in this message and attachments  
> > > > is intended solely for the attention and use of the named addressee and may  
> > > > be confidential. If you are not the intended recipient, you are reminded  
> > > > that the information remains the property of the sender. You must not use,  
> > > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > > received this message in error, please contact the sender immediately and  
> > > > irrevocably delete this message and any copies.
> > > > 
> > > > --

--

---

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [November 20, 2012, 5:18pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/6 "2012-11-20T17:18:04Z")

</div>

Hi Chris,

That doesn't work for me:  
{  
"filtered": {  
"query": {  
"query\_string": {  
"query": "test and asdf"  
}  
},  
"filter": {  
"numeric\_range": {  
"from": "0",  
"to": "1353431826"  
}  
}  
}  
}

Error in the log:

[2012-11-20 18:14:13,265][DEBUG][action.search.type] [Aardwolf]  
[2012-11-20][3], node[l8EnaTstTMeyBZfGXL3viA], [P], s[STARTED]: Failed to  
execute [org.elasticsearch.action.search.SearchRequest@7fde81b5]  
org.elasticsearch.search.SearchParseException: [2012-11-20][3]:  
from[-1],size[-1]: Parse Failure [Failed to parse source [{  
"filtered" : {  
"query" : {  
"query\_string" : {  
"query" : "\*"  
}  
},  
"filter" : {  
"numeric\_range" : {  
"from" : "0",  
"to" : "1353431653"  
}  
}  
}  
}  
]]  
at  
org.elasticsearch.search.SearchService.parseSource(SearchService.java:554)  
at  
org.elasticsearch.search.SearchService.createContext(SearchService.java:469)  
at  
org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:228)  
at  
org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteQuery(SearchServiceTransportAction.java:140)  
at  
org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryThenFetchAction.java:80)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  
at java.lang.Thread.run(Unknown Source)  
Caused by: org.elasticsearch.search.SearchParseException: [2012-11-20][3]:  
from[-1],size[-1]: Parse Failure [No parser for element [filtered]]  
at  
org.elasticsearch.search.SearchService.parseSource(SearchService.java:540)  
... 10 more

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

[http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

On Tue, Nov 20, 2012 at 4:27 AM, Chris Male [gento0nz@gmail.com](mailto:gento0nz@gmail.com) wrote:

> Hi Robin,
> 
> I just threw together an example which you'll need to edit to meet your  
> needs, [ElasticSearch DSL and NumericRangeQuery · GitHub](https://gist.github.com/4115758).
> 
> On Tuesday, November 20, 2012 11:18:14 AM UTC+13, Robin Verlangen wrote:
> 
> > The numeric range is like this:
> > 
> > {"query":{"constant\_score":{" **filter":{"numeric\_range":{"dt"**  
> > :{"from":0,"to":1353363346}}}}\*\*}}
> > 
> > However I have no idea how to add the query string to this structure:  
> > when I change something it doesn't turn any response and the log is empty.  
> > For example:
> > 
> > {"query":{"constant\_score":{" **filter":{"numeric\_range":{"dt"**  
> > :{"from":0,"to":1353363459}}}, **"query":{"query\_string":{"\_**  
> > all":"\*"}}}}}
> > 
> > Best regards,
> > 
> > Robin Verlangen  
> > _Software engineer_  
> > \*  
> > \*  
> > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > E ro...@us2.nl
> > 
> > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > 
> > Disclaimer: The information contained in this message and attachments is  
> > intended solely for the attention and use of the named addressee and may be  
> > confidential. If you are not the intended recipient, you are reminded that  
> > the information remains the property of the sender. You must not use,  
> > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > received this message in error, please contact the sender immediately and  
> > irrevocably delete this message and any copies.
> > 
> > On Mon, Nov 19, 2012 at 9:40 PM, Robin Verlangen [ro...@us2.nl](mailto:ro...@us2.nl) wrote:
> > 
> > > Hi Chris,
> > > 
> > > Have been struggling with this for a couple of days, but can't get it  
> > > right. Could you please give me an example (something that works when I  
> > > fire it copy/past into a req. body)? I think I miss something fundamental.
> > > 
> > > Best regards,
> > > 
> > > Robin Verlangen  
> > > _Software engineer_  
> > > \*  
> > > \*  
> > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > E ro...@us2.nl
> > > 
> > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > 
> > > Disclaimer: The information contained in this message and attachments is  
> > > intended solely for the attention and use of the named addressee and may be  
> > > confidential. If you are not the intended recipient, you are reminded that  
> > > the information remains the property of the sender. You must not use,  
> > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > received this message in error, please contact the sender immediately and  
> > > irrevocably delete this message and any copies.
> > > 
> > > On Thu, Nov 15, 2012 at 10:43 PM, Chris Male [gent...@gmail.com](mailto:gent...@gmail.com) wrote:
> > > 
> > > > Hi Robin,
> > > > 
> > > > Couple of options for how to do this.
> > > > 
> > > > If your numeric\_range is a pretty static value (and consequently would  
> > > > benefit from caching), I recommend you make use of  
> > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > filtered-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
> > > > Put your query\_string as the query, and the numeric\_range as a range  
> > > > filter, much like the example shows.
> > > > 
> > > > Alternatively if you don't want to use a filter, you could simple use a  
> > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > bool-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)with your query as one clause and your range as another.
> > > > 
> > > > At any stage if you want to skip scoring, you can wrap a query in a  
> > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > constant-score-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> > > > .
> > > > 
> > > > On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:
> > > > 
> > > > > Hi there,
> > > > > 
> > > > > How exactly would a DSL query look if it contained both a query (that  
> > > > > contains anything, e.g. "dt:123456 user:john login -200") and a  
> > > > > numeric\_range on the dt field.
> > > > > 
> > > > > There's no need for scoring, but there is for sorting (on the "dt"  
> > > > > field descending).
> > > > > 
> > > > > Should I use this one? [http://www.elasticsearch](http://www.elasticsearch).\*\*\*\*  
> > > > > org/guide/reference/query-dsl/\*\*\*\*query-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> > > > > 
> > > > > If so, could anyone provide an example of my use case?
> > > > > 
> > > > > Thank you in advance!
> > > > > 
> > > > > Best regards,
> > > > > 
> > > > > Robin Verlangen  
> > > > > _Software engineer_  
> > > > > \*  
> > > > > \*  
> > > > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > > > E ro...@us2.nl
> > > > > 
> > > > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > > > 
> > > > > Disclaimer: The information contained in this message and attachments  
> > > > > is intended solely for the attention and use of the named addressee and may  
> > > > > be confidential. If you are not the intended recipient, you are reminded  
> > > > > that the information remains the property of the sender. You must not use,  
> > > > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > > > received this message in error, please contact the sender immediately and  
> > > > > irrevocably delete this message and any copies.
> > > > > 
> > > > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [November 20, 2012, 5:32pm UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/7 "2012-11-20T17:32:11Z")

</div>

Finally fixed it, thank you for your time Chris!

{  
"query": {  
"filtered": {  
"filter": {  
"numeric\_range": {  
"dt": {  
"from": 0,  
"to": 1353432703  
}  
}  
},  
"query": {  
"query\_string": {  
"query": "\*"  
}  
}  
}  
}  
}

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

[http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

On Tue, Nov 20, 2012 at 6:18 PM, Robin Verlangen [robin@us2.nl](mailto:robin@us2.nl) wrote:

> Hi Chris,
> 
> That doesn't work for me:  
> {  
> "filtered": {  
> "query": {  
> "query\_string": {  
> "query": "test and asdf"  
> }  
> },  
> "filter": {  
> "numeric\_range": {  
> "from": "0",  
> "to": "1353431826"  
> }  
> }  
> }  
> }
> 
> Error in the log:
> 
> [2012-11-20 18:14:13,265][DEBUG][action.search.type] [Aardwolf]  
> [2012-11-20][3], node[l8EnaTstTMeyBZfGXL3viA], [P], s[STARTED]: Failed to  
> execute [org.elasticsearch.action.search.SearchRequest@7fde81b5]  
> org.elasticsearch.search.SearchParseException: [2012-11-20][3]:  
> from[-1],size[-1]: Parse Failure [Failed to parse source [{  
> "filtered" : {  
> "query" : {  
> "query\_string" : {  
> "query" : "\*"  
> }  
> },  
> "filter" : {  
> "numeric\_range" : {  
> "from" : "0",  
> "to" : "1353431653"  
> }  
> }  
> }  
> }  
> ]]  
> at  
> org.elasticsearch.search.SearchService.parseSource(SearchService.java:554)  
> at  
> org.elasticsearch.search.SearchService.createContext(SearchService.java:469)  
> at  
> org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:228)  
> at  
> org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteQuery(SearchServiceTransportAction.java:140)  
> at  
> org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryThenFetchAction.java:80)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)  
> at java.lang.Thread.run(Unknown Source)  
> Caused by: org.elasticsearch.search.SearchParseException: [2012-11-20][3]:  
> from[-1],size[-1]: Parse Failure [No parser for element [filtered]]  
> at  
> org.elasticsearch.search.SearchService.parseSource(SearchService.java:540)  
> ... 10 more
> 
> Best regards,
> 
> Robin Verlangen  
> _Software engineer_  
> \*  
> \*  
> W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> E robin@us2.nl
> 
> [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> 
> Disclaimer: The information contained in this message and attachments is  
> intended solely for the attention and use of the named addressee and may be  
> confidential. If you are not the intended recipient, you are reminded that  
> the information remains the property of the sender. You must not use,  
> disclose, distribute, copy, print or rely on this e-mail. If you have  
> received this message in error, please contact the sender immediately and  
> irrevocably delete this message and any copies.
> 
> On Tue, Nov 20, 2012 at 4:27 AM, Chris Male [gento0nz@gmail.com](mailto:gento0nz@gmail.com) wrote:
> 
> > Hi Robin,
> > 
> > I just threw together an example which you'll need to edit to meet your  
> > needs, [ElasticSearch DSL and NumericRangeQuery · GitHub](https://gist.github.com/4115758).
> > 
> > On Tuesday, November 20, 2012 11:18:14 AM UTC+13, Robin Verlangen wrote:
> > 
> > > The numeric range is like this:
> > > 
> > > {"query":{"constant\_score":{" **filter":{"numeric\_range":{"dt"**  
> > > :{"from":0,"to":1353363346}}}}\*\*}}
> > > 
> > > However I have no idea how to add the query string to this structure:  
> > > when I change something it doesn't turn any response and the log is empty.  
> > > For example:
> > > 
> > > {"query":{"constant\_score":{" **filter":{"numeric\_range":{"dt"**  
> > > :{"from":0,"to":1353363459}}}, **"query":{"query\_string":{"\_**  
> > > all":"\*"}}}}}
> > > 
> > > Best regards,
> > > 
> > > Robin Verlangen  
> > > _Software engineer_  
> > > \*  
> > > \*  
> > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > E ro...@us2.nl
> > > 
> > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > 
> > > Disclaimer: The information contained in this message and attachments is  
> > > intended solely for the attention and use of the named addressee and may be  
> > > confidential. If you are not the intended recipient, you are reminded that  
> > > the information remains the property of the sender. You must not use,  
> > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > received this message in error, please contact the sender immediately and  
> > > irrevocably delete this message and any copies.
> > > 
> > > On Mon, Nov 19, 2012 at 9:40 PM, Robin Verlangen [ro...@us2.nl](mailto:ro...@us2.nl) wrote:
> > > 
> > > > Hi Chris,
> > > > 
> > > > Have been struggling with this for a couple of days, but can't get it  
> > > > right. Could you please give me an example (something that works when I  
> > > > fire it copy/past into a req. body)? I think I miss something fundamental.
> > > > 
> > > > Best regards,
> > > > 
> > > > Robin Verlangen  
> > > > _Software engineer_  
> > > > \*  
> > > > \*  
> > > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > > E ro...@us2.nl
> > > > 
> > > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > > 
> > > > Disclaimer: The information contained in this message and attachments  
> > > > is intended solely for the attention and use of the named addressee and may  
> > > > be confidential. If you are not the intended recipient, you are reminded  
> > > > that the information remains the property of the sender. You must not use,  
> > > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > > received this message in error, please contact the sender immediately and  
> > > > irrevocably delete this message and any copies.
> > > > 
> > > > On Thu, Nov 15, 2012 at 10:43 PM, Chris Male [gent...@gmail.com](mailto:gent...@gmail.com) wrote:
> > > > 
> > > > > Hi Robin,
> > > > > 
> > > > > Couple of options for how to do this.
> > > > > 
> > > > > If your numeric\_range is a pretty static value (and consequently would  
> > > > > benefit from caching), I recommend you make use of  
> > > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > > filtered-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html).  
> > > > > Put your query\_string as the query, and the numeric\_range as a range  
> > > > > filter, much like the example shows.
> > > > > 
> > > > > Alternatively if you don't want to use a filter, you could simple use  
> > > > > a [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > > bool-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)with your query as one clause and your range as another.
> > > > > 
> > > > > At any stage if you want to skip scoring, you can wrap a query in a  
> > > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/query-dsl/**  
> > > > > constant-score-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query.html)  
> > > > > .
> > > > > 
> > > > > On Friday, November 16, 2012 7:57:11 AM UTC+13, Robin Verlangen wrote:
> > > > > 
> > > > > > Hi there,
> > > > > > 
> > > > > > How exactly would a DSL query look if it contained both a query (that  
> > > > > > contains anything, e.g. "dt:123456 user:john login -200") and a  
> > > > > > numeric\_range on the dt field.
> > > > > > 
> > > > > > There's no need for scoring, but there is for sorting (on the "dt"  
> > > > > > field descending).
> > > > > > 
> > > > > > Should I use this one? [http://www.elasticsearch](http://www.elasticsearch).\*\*\*\*  
> > > > > > org/guide/reference/query-dsl/\*\*\*\*query-filter.html[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)
> > > > > > 
> > > > > > If so, could anyone provide an example of my use case?
> > > > > > 
> > > > > > Thank you in advance!
> > > > > > 
> > > > > > Best regards,
> > > > > > 
> > > > > > Robin Verlangen  
> > > > > > _Software engineer_  
> > > > > > \*  
> > > > > > \*  
> > > > > > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > > > > > E ro...@us2.nl
> > > > > > 
> > > > > > [http://goo.gl/Lt7BC](http://goo.gl/Lt7BC)
> > > > > > 
> > > > > > Disclaimer: The information contained in this message and attachments  
> > > > > > is intended solely for the attention and use of the named addressee and may  
> > > > > > be confidential. If you are not the intended recipient, you are reminded  
> > > > > > that the information remains the property of the sender. You must not use,  
> > > > > > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > > > > > received this message in error, please contact the sender immediately and  
> > > > > > irrevocably delete this message and any copies.
> > > > > > 
> > > > > > --
> > > 
> > > --

--

---

<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, 3:03am UTC](https://discuss.elastic.co/t/dsl-query-numeric-range-filter-sort/9731/8 "2017-07-06T03:03:31Z")

</div>


