# Numeric Range Query

**URL:** <https://discuss.elastic.co/t/numeric-range-query/2806>\
**Category:** Elasticsearch\
**Created:** [February 11, 2010, 2:54pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806 "2010-02-11T14:54:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabbit\_fr](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rabbit\_fr](https://discuss.elastic.co/u/rabbit_fr)\
**Post date:** [February 11, 2010, 2:54pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/1 "2010-02-11T14:54:27Z")

</div>

Hi,

I'd like to do combined range query on document like

document {  
start: 10,  
stop: 20  
}

Querying using '{ query : { range : {start: { from :null, to:  
15 } } }' works fine,  
but how to do something like :

'{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
15, to: null} }'

I tried to combine them using boolean query without success.

Any clue ?

Thank you

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 11, 2010, 3:00pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/2 "2010-02-11T15:00:31Z")

</div>

Your thoughts are correct, you should use a boolean query with (I am  
guessing) two must clauses which will return an AND basically of the two  
clauses, here is an example:

{  
bool : {  
must : {  
range : { ... }  
},  
must: {  
range : { ... }  
}  
}  
}

-shay.banon

On Thu, Feb 11, 2010 at 4:54 PM, Rabbit [antonin.bonte@gmail.com](mailto:antonin.bonte@gmail.com) wrote:

> Hi,
> 
> I'd like to do combined range query on document like
> 
> document {  
> start: 10,  
> stop: 20  
> }
> 
> Querying using '{ query : { range : {start: { from :null, to:  
> 15 } } }' works fine,  
> but how to do something like :
> 
> '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
> 15, to: null} }'
> 
> I tried to combine them using boolean query without success.
> 
> Any clue ?
> 
> Thank you

---

<div class="post-metadata">

**Author:** ![rabbit\_fr](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rabbit\_fr](https://discuss.elastic.co/u/rabbit_fr)\
**Post date:** [February 11, 2010, 3:17pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/3 "2010-02-11T15:17:35Z")

</div>

curl -X GET "[http://localhost:9200/ip/\_search?pretty=true](http://localhost:9200/ip/_search?pretty=true)" -d  
\ (~/workspace/  
RestServer/dat)  
'{  
bool : {  
must : {  
range : { start: { from :null, to: 15 } }  
},  
must: {  
range : { stop: { from :15, to: null } }  
}  
}  
}'  
{  
"\_shards" : {  
"total" : 5,  
"successful" : 0,  
"failed" : 5  
},  
"hits" : {  
"total" : 0,  
"hits" :   
}  
}%

do not return any result.

match\_all returns :

"hits" : {  
"total" : 2,  
"hits" : [ {  
"\_index" : "ip",  
"\_type" : "range",  
"\_id" : "1", "\_source" : {  
start: 1511729664,  
stop: 1511729919,  
location: 210743  
}

```
}, {
  "_index" : "ip",
  "_type" : "range",
  "_id" : "2", "_source" : {
    start: 10,
    stop: 20,
    location: 210743

```

}

I must be omitting something obvious 🙂

On Feb 11, 4:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Your thoughts are correct, you should use a boolean query with (I am  
> guessing) two must clauses which will return an AND basically of the two  
> clauses, here is an example:
> 
> {  
> bool : {  
> must : {  
> range : { ... }  
> },  
> must: {  
> range : { ... }  
> }  
> }
> 
> }
> 
> -shay.banon
> 
> On Thu, Feb 11, 2010 at 4:54 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com) wrote:
> 
> > Hi,
> 
> > I'd like to do combined range query on document like
> 
> > document {  
> > start: 10,  
> > stop: 20  
> > }
> 
> > Querying using '{ query : { range : {start: { from :null, to:  
> > 15 } } }' works fine,  
> > but how to do something like :
> 
> > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
> > 15, to: null} }'
> 
> > I tried to combine them using boolean query without success.
> 
> > Any clue ?
> 
> > Thank you

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 11, 2010, 3:21pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/4 "2010-02-11T15:21:16Z")

</div>

Yep, the actual Json should include the query tag as explained here:  
[http://www.elasticsearch.com/docs/elasticsearch/json\_api/search/](http://www.elasticsearch.com/docs/elasticsearch/json_api/search/). In your  
case:

{  
query : {  
bool : {  
must : {  
range : { start: { from :null, to: 15 } }  
},  
must: {  
range : { stop: { from :15, to: null } }  
}  
}  
}  
}

The reason why its wrapped in query is because there are other parameters  
that you can pass on the search request JSON.

-shay.banon

On Thu, Feb 11, 2010 at 5:17 PM, Rabbit [antonin.bonte@gmail.com](mailto:antonin.bonte@gmail.com) wrote:

> curl -X GET "[http://localhost:9200/ip/\_search?pretty=true](http://localhost:9200/ip/_search?pretty=true)" -d  
> \ (~/workspace/  
> RestServer/dat)  
> '{  
> bool : {  
> must : {  
> range : { start: { from :null, to: 15 } }  
> },  
> must: {  
> range : { stop: { from :15, to: null } }  
> }  
> }  
> }'  
> {  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 0,  
> "failed" : 5  
> },  
> "hits" : {  
> "total" : 0,  
> "hits" :   
> }  
> }%
> 
> do not return any result.
> 
> match\_all returns :
> 
> "hits" : {  
> "total" : 2,  
> "hits" : [ {  
> "\_index" : "ip",  
> "\_type" : "range",  
> "\_id" : "1", "\_source" : {  
> start: 1511729664,  
> stop: 1511729919,  
> location: 210743  
> }
> 
> }, {  
> "\_index" : "ip",  
> "\_type" : "range",  
> "\_id" : "2", "\_source" : {  
> start: 10,  
> stop: 20,  
> location: 210743  
> }
> 
> I must be omitting something obvious 🙂
> 
> On Feb 11, 4:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Your thoughts are correct, you should use a boolean query with (I am  
> > guessing) two must clauses which will return an AND basically of the two  
> > clauses, here is an example:
> > 
> > {  
> > bool : {  
> > must : {  
> > range : { ... }  
> > },  
> > must: {  
> > range : { ... }  
> > }  
> > }
> > 
> > }
> > 
> > -shay.banon
> > 
> > On Thu, Feb 11, 2010 at 4:54 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com) wrote:
> > 
> > > Hi,
> > 
> > > I'd like to do combined range query on document like
> > 
> > > document {  
> > > start: 10,  
> > > stop: 20  
> > > }
> > 
> > > Querying using '{ query : { range : {start: { from :null, to:  
> > > 15 } } }' works fine,  
> > > but how to do something like :
> > 
> > > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
> > > 15, to: null} }'
> > 
> > > I tried to combine them using boolean query without success.
> > 
> > > Any clue ?
> > 
> > > Thank you

---

<div class="post-metadata">

**Author:** ![rabbit\_fr](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@rabbit\_fr](https://discuss.elastic.co/u/rabbit_fr)\
**Post date:** [February 11, 2010, 3:40pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/5 "2010-02-11T15:40:18Z")

</div>

thank you, works fine now

On Feb 11, 4:21 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Yep, the actual Json should include the query tag as explained here:[http://www.elasticsearch.com/docs/elasticsearch/json\_api/search/](http://www.elasticsearch.com/docs/elasticsearch/json_api/search/). In your  
> case:
> 
> {  
> query : {  
> bool : {  
> must : {  
> range : { start: { from :null, to: 15 } }  
> },  
> must: {  
> range : { stop: { from :15, to: null } }  
> }  
> }  
> }
> 
> }
> 
> The reason why its wrapped in query is because there are other parameters  
> that you can pass on the search request JSON.
> 
> -shay.banon
> 
> On Thu, Feb 11, 2010 at 5:17 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com) wrote:
> 
> > curl -X GET "[http://localhost:9200/ip/\_search?pretty=true](http://localhost:9200/ip/_search?pretty=true)" -d  
> > \ (~/workspace/  
> > RestServer/dat)  
> > '{  
> > bool : {  
> > must : {  
> > range : { start: { from :null, to: 15 } }  
> > },  
> > must: {  
> > range : { stop: { from :15, to: null } }  
> > }  
> > }  
> > }'  
> > {  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 0,  
> > "failed" : 5  
> > },  
> > "hits" : {  
> > "total" : 0,  
> > "hits" :   
> > }  
> > }%
> 
> > do not return any result.
> 
> > match\_all returns :
> 
> > "hits" : {  
> > "total" : 2,  
> > "hits" : [ {  
> > "\_index" : "ip",  
> > "\_type" : "range",  
> > "\_id" : "1", "\_source" : {  
> > start: 1511729664,  
> > stop: 1511729919,  
> > location: 210743  
> > }
> 
> > }, {  
> > "\_index" : "ip",  
> > "\_type" : "range",  
> > "\_id" : "2", "\_source" : {  
> > start: 10,  
> > stop: 20,  
> > location: 210743  
> > }
> 
> > I must be omitting something obvious 🙂
> 
> > On Feb 11, 4:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > Your thoughts are correct, you should use a boolean query with (I am  
> > > guessing) two must clauses which will return an AND basically of the two  
> > > clauses, here is an example:
> 
> > > {  
> > > bool : {  
> > > must : {  
> > > range : { ... }  
> > > },  
> > > must: {  
> > > range : { ... }  
> > > }  
> > > }
> 
> > > }
> 
> > > -shay.banon
> 
> > > On Thu, Feb 11, 2010 at 4:54 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com) wrote:
> > > 
> > > > Hi,
> 
> > > > I'd like to do combined range query on document like
> 
> > > > document {  
> > > > start: 10,  
> > > > stop: 20  
> > > > }
> 
> > > > Querying using '{ query : { range : {start: { from :null, to:  
> > > > 15 } } }' works fine,  
> > > > but how to do something like :
> 
> > > > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
> > > > 15, to: null} }'
> 
> > > > I tried to combine them using boolean query without success.
> 
> > > > Any clue ?
> 
> > > > Thank you

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 11, 2010, 3:59pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/6 "2010-02-11T15:59:12Z")

</div>

Great!, would love to hear your experiance with elasticsearch once you  
get enough milage on it 😉

-shay.banon

On 2/11/10, Rabbit [antonin.bonte@gmail.com](mailto:antonin.bonte@gmail.com) wrote:

> thank you, works fine now
> 
> On Feb 11, 4:21 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Yep, the actual Json should include the query tag as explained  
> > here:[http://www.elasticsearch.com/docs/elasticsearch/json\_api/search/](http://www.elasticsearch.com/docs/elasticsearch/json_api/search/). In  
> > your  
> > case:
> > 
> > {  
> > query : {  
> > bool : {  
> > must : {  
> > range : { start: { from :null, to: 15 } }  
> > },  
> > must: {  
> > range : { stop: { from :15, to: null } }  
> > }  
> > }  
> > }
> > 
> > }
> > 
> > The reason why its wrapped in query is because there are other parameters  
> > that you can pass on the search request JSON.
> > 
> > -shay.banon
> > 
> > On Thu, Feb 11, 2010 at 5:17 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com) wrote:
> > 
> > > curl -X GET "[http://localhost:9200/ip/\_search?pretty=true](http://localhost:9200/ip/_search?pretty=true)" -d  
> > > \ (~/workspace/  
> > > RestServer/dat)  
> > > '{  
> > > bool : {  
> > > must : {  
> > > range : { start: { from :null, to: 15 } }  
> > > },  
> > > must: {  
> > > range : { stop: { from :15, to: null } }  
> > > }  
> > > }  
> > > }'  
> > > {  
> > > "\_shards" : {  
> > > "total" : 5,  
> > > "successful" : 0,  
> > > "failed" : 5  
> > > },  
> > > "hits" : {  
> > > "total" : 0,  
> > > "hits" :   
> > > }  
> > > }%
> > 
> > > do not return any result.
> > 
> > > match\_all returns :
> > 
> > > "hits" : {  
> > > "total" : 2,  
> > > "hits" : [ {  
> > > "\_index" : "ip",  
> > > "\_type" : "range",  
> > > "\_id" : "1", "\_source" : {  
> > > start: 1511729664,  
> > > stop: 1511729919,  
> > > location: 210743  
> > > }
> > 
> > > }, {  
> > > "\_index" : "ip",  
> > > "\_type" : "range",  
> > > "\_id" : "2", "\_source" : {  
> > > start: 10,  
> > > stop: 20,  
> > > location: 210743  
> > > }
> > 
> > > I must be omitting something obvious 🙂
> > 
> > > On Feb 11, 4:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > > 
> > > > Your thoughts are correct, you should use a boolean query with (I am  
> > > > guessing) two must clauses which will return an AND basically of the  
> > > > two  
> > > > clauses, here is an example:
> > 
> > > > {  
> > > > bool : {  
> > > > must : {  
> > > > range : { ... }  
> > > > },  
> > > > must: {  
> > > > range : { ... }  
> > > > }  
> > > > }
> > 
> > > > }
> > 
> > > > -shay.banon
> > 
> > > > On Thu, Feb 11, 2010 at 4:54 PM, Rabbit [antonin.bo...@gmail.com](mailto:antonin.bo...@gmail.com)  
> > > > wrote:
> > > > 
> > > > > Hi,
> > 
> > > > > I'd like to do combined range query on document like
> > 
> > > > > document {  
> > > > > start: 10,  
> > > > > stop: 20  
> > > > > }
> > 
> > > > > Querying using '{ query : { range : {start: { from :null, to:  
> > > > > 15 } } }' works fine,  
> > > > > but how to do something like :
> > 
> > > > > '{ query : { range : {start: { from :null, to: 15 } }, stop: { from:  
> > > > > 15, to: null} }'
> > 
> > > > > I tried to combine them using boolean query without success.
> > 
> > > > > Any clue ?
> > 
> > > > > Thank you

--  
Sent from my mobile device

---

<div class="post-metadata">

**Author:** ![Simon\_Willison](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_willison/32/3364_2.png) [@Simon\_Willison](https://discuss.elastic.co/u/Simon_Willison)\
**Post date:** [February 12, 2010, 12:34pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/7 "2010-02-12T12:34:51Z")

</div>

On Feb 11, 3:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Your thoughts are correct, you should use a boolean query with (I am  
> guessing) two must clauses which will return an AND basically of the two  
> clauses, here is an example:
> 
> {  
> bool : {  
> must : {  
> range : { ... }  
> },  
> must: {  
> range : { ... }  
> }  
> }  
> }

I'm a bit worried about that example - it isn't valid JSON according  
to the JSON spec (which requires double quotes around all strings),  
but more importantly JSON objects shouldn't have two keys with the  
same names. If you feed the above in to many (most?) JSON libraries  
you'll lose data. Here's what the Python JSON library does with it,  
for example:

In [45]: s = """{  
....: "bool" : {  
....: "must" : {  
....: "range" : 1  
....: },  
....: "must": {  
....: "range" : 2  
....: }  
....: }  
....: }"""

In [46]: json.loads(s)  
Out[46]: {u'bool': {u'must': {u'range': 2}}}

One fix would be to use the following syntax instead:

{  
"bool" : {  
"must" : [  
{  
"range" : { ... }  
},  
{  
"range": { ... }  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 12, 2010, 1:22pm UTC](https://discuss.elastic.co/t/numeric-range-query/2806/8 "2010-02-12T13:22:51Z")

</div>

You are absolutely right, it format supported should be supported as  
well.Though I think the current format is a valid JSON, I think there is the  
underlying assumption that field names should be unique. So, I created the  
following issue:  
[Issues · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/#issue/6) and it should  
be fixed by now in master 🙂

-shay.banon

On Fri, Feb 12, 2010 at 2:34 PM, Simon Willison [simon@simonwillison.net](mailto:simon@simonwillison.net)wrote:

> On Feb 11, 3:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > Your thoughts are correct, you should use a boolean query with (I am  
> > guessing) two must clauses which will return an AND basically of the two  
> > clauses, here is an example:
> > 
> > {  
> > bool : {  
> > must : {  
> > range : { ... }  
> > },  
> > must: {  
> > range : { ... }  
> > }  
> > }  
> > }
> 
> I'm a bit worried about that example - it isn't valid JSON according  
> to the JSON spec (which requires double quotes around all strings),  
> but more importantly JSON objects shouldn't have two keys with the  
> same names. If you feed the above in to many (most?) JSON libraries  
> you'll lose data. Here's what the Python JSON library does with it,  
> for example:
> 
> In [45]: s = """{  
> ....: "bool" : {  
> ....: "must" : {  
> ....: "range" : 1  
> ....: },  
> ....: "must": {  
> ....: "range" : 2  
> ....: }  
> ....: }  
> ....: }"""
> 
> In [46]: json.loads(s)  
> Out[46]: {u'bool': {u'must': {u'range': 2}}}
> 
> One fix would be to use the following syntax instead:
> 
> {  
> "bool" : {  
> "must" : [  
> {  
> "range" : { ... }  
> },  
> {  
> "range": { ... }  
> }  
> ]  
> }  
> }

---

<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, 4:25am UTC](https://discuss.elastic.co/t/numeric-range-query/2806/9 "2017-07-06T04:25:49Z")

</div>


