# Nested boolean range query returning values outside of its range

**URL:** https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870
**Category:** Elasticsearch
**Created:** [November 14, 2011, 7:49pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870 "2011-11-14T19:49:32Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 14, 2011, 7:49pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/1 "2011-11-14T19:49:32Z")

</div>

Hi, I have a bunch of records in an index called item, with varying prices.  
I'm trying to construct a query that will allow me to retrieve items under  
a particular category\_id, and in one or more price ranges.  
The query I've come up with is this:

{  
"size": 18,  
"sort": [  
{  
"listed\_at": "desc"  
}  
],  
"from": 0,  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"category\_id": 1  
}  
},  
{  
"bool": {  
"should": [  
{  
"range": {  
"price": {  
"to": 4.99,  
"from": 0.0  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}

I was hoping that it would return only items in the category\_id 1, with a  
price between 0.0 and 4.99 -- however, when I run it, I get only items in  
category\_id 1 well enough, but I also get _all_ items under that  
category\_id, including those with a price of over 4.99.  
Could someone advise me as to where I am going wrong?  
Thankyou for any & all assistance.  
Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 11:12am UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/2 "2011-11-15T11:12:43Z")

</div>

Hi -- I've been trying to get this query to work, and tried a filtered  
query (as another post suggested), but I still have exactly the same  
problem.  
The current attempt looks like this:  
{  
"size": 18,  
"sort": [  
{  
"listed\_at": "desc"  
}  
],  
"from": 0,  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"or": [  
{  
"range": {  
"price": {  
"to": 4.99,  
"from": 0  
}  
}  
}  
]  
}  
]  
},  
"query": {  
"term": {  
"category\_id": 1  
}  
}  
}  
}  
}

Basically, I want to select all items under a given category\_id, and filter  
them on (potentially more than one) price range, as well as a number of  
other terms filters (for things like colour\_id, etc.).  
Am I at least heading in sort-of the right direction with this approach?  
Could anyone offer me any insight into what I'm missing about how this  
works that makes me surprised that my efforts aren't returning what I  
expect them to?  
Thanks for any & all help,  
Doug.

On 14 November 2011 19:49, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:

> Hi, I have a bunch of records in an index called item, with varying prices.  
> I'm trying to construct a query that will allow me to retrieve items under  
> a particular category\_id, and in one or more price ranges.  
> The query I've come up with is this:
> 
> {  
> "size": 18,  
> "sort": [  
> {  
> "listed\_at": "desc"  
> }  
> ],  
> "from": 0,  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "category\_id": 1  
> }  
> },  
> {  
> "bool": {  
> "should": [  
> {  
> "range": {  
> "price": {  
> "to": 4.99,  
> "from": 0.0  
> }  
> }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> I was hoping that it would return only items in the category\_id 1, with a  
> price between 0.0 and 4.99 -- however, when I run it, I get only items in  
> category\_id 1 well enough, but I also get _all_ items under that  
> category\_id, including those with a price of over 4.99.  
> Could someone advise me as to where I am going wrong?  
> Thankyou for any & all assistance.  
> Doug.

---

<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: [November 15, 2011, 11:27am UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/3 "2011-11-15T11:27:21Z")

</div>

Both search requests look good. Can you gist a simple recreation that shows  
this? A few curl requests that index fresh data into a fresh index, and the  
search request that shows wrong results returning.

On Tue, Nov 15, 2011 at 1:12 PM, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:

> Hi -- I've been trying to get this query to work, and tried a filtered  
> query (as another post suggested), but I still have exactly the same  
> problem.  
> The current attempt looks like this:  
> {  
> "size": 18,  
> "sort": [  
> {  
> "listed\_at": "desc"  
> }  
> ],  
> "from": 0,  
> "query": {  
> "filtered": {  
> "filter": {  
> "and": [  
> {  
> "or": [  
> {  
> "range": {  
> "price": {  
> "to": 4.99,  
> "from": 0  
> }  
> }  
> }  
> ]  
> }  
> ]  
> },  
> "query": {  
> "term": {  
> "category\_id": 1  
> }  
> }  
> }  
> }  
> }
> 
> Basically, I want to select all items under a given category\_id, and  
> filter them on (potentially more than one) price range, as well as a number  
> of other terms filters (for things like colour\_id, etc.).  
> Am I at least heading in sort-of the right direction with this approach?  
> Could anyone offer me any insight into what I'm missing about how this  
> works that makes me surprised that my efforts aren't returning what I  
> expect them to?  
> Thanks for any & all help,  
> Doug.
> 
> On 14 November 2011 19:49, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> 
> > Hi, I have a bunch of records in an index called item, with varying  
> > prices.  
> > I'm trying to construct a query that will allow me to retrieve items  
> > under a particular category\_id, and in one or more price ranges.  
> > The query I've come up with is this:
> > 
> > {  
> > "size": 18,  
> > "sort": [  
> > {  
> > "listed\_at": "desc"  
> > }  
> > ],  
> > "from": 0,  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "term": {  
> > "category\_id": 1  
> > }  
> > },  
> > {  
> > "bool": {  
> > "should": [  
> > {  
> > "range": {  
> > "price": {  
> > "to": 4.99,  
> > "from": 0.0  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > I was hoping that it would return only items in the category\_id 1, with a  
> > price between 0.0 and 4.99 -- however, when I run it, I get only items in  
> > category\_id 1 well enough, but I also get _all_ items under that  
> > category\_id, including those with a price of over 4.99.  
> > Could someone advise me as to where I am going wrong?  
> > Thankyou for any & all assistance.  
> > Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 2:59pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/4 "2011-11-15T14:59:30Z")

</div>

Hi, sure -- the indexing happens automatically to a fresh index as part of  
testing -- the test process automatically clears the index before tests  
commence.  
The output of the search is here: [es-search-results.json · GitHub](https://gist.github.com/1367263)  
This is for the second search, although it looks the same for the first.  
& thanks for looking at this.

On 15 November 2011 11:27, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> Both search requests look good. Can you gist a simple recreation that  
> shows this? A few curl requests that index fresh data into a fresh index,  
> and the search request that shows wrong results returning.
> 
> On Tue, Nov 15, 2011 at 1:12 PM, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> 
> > Hi -- I've been trying to get this query to work, and tried a filtered  
> > query (as another post suggested), but I still have exactly the same  
> > problem.  
> > The current attempt looks like this:  
> > {  
> > "size": 18,  
> > "sort": [  
> > {  
> > "listed\_at": "desc"  
> > }  
> > ],  
> > "from": 0,  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "and": [  
> > {  
> > "or": [  
> > {  
> > "range": {  
> > "price": {  
> > "to": 4.99,  
> > "from": 0  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > ]  
> > },  
> > "query": {  
> > "term": {  
> > "category\_id": 1  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Basically, I want to select all items under a given category\_id, and  
> > filter them on (potentially more than one) price range, as well as a number  
> > of other terms filters (for things like colour\_id, etc.).  
> > Am I at least heading in sort-of the right direction with this approach?  
> > Could anyone offer me any insight into what I'm missing about how this  
> > works that makes me surprised that my efforts aren't returning what I  
> > expect them to?  
> > Thanks for any & all help,  
> > Doug.
> > 
> > On 14 November 2011 19:49, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> > 
> > > Hi, I have a bunch of records in an index called item, with varying  
> > > prices.  
> > > I'm trying to construct a query that will allow me to retrieve items  
> > > under a particular category\_id, and in one or more price ranges.  
> > > The query I've come up with is this:
> > > 
> > > {  
> > > "size": 18,  
> > > "sort": [  
> > > {  
> > > "listed\_at": "desc"  
> > > }  
> > > ],  
> > > "from": 0,  
> > > "query": {  
> > > "bool": {  
> > > "must": [  
> > > {  
> > > "term": {  
> > > "category\_id": 1  
> > > }  
> > > },  
> > > {  
> > > "bool": {  
> > > "should": [  
> > > {  
> > > "range": {  
> > > "price": {  
> > > "to": 4.99,  
> > > "from": 0.0  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > I was hoping that it would return only items in the category\_id 1, with  
> > > a price between 0.0 and 4.99 -- however, when I run it, I get only items in  
> > > category\_id 1 well enough, but I also get _all_ items under that  
> > > category\_id, including those with a price of over 4.99.  
> > > Could someone advise me as to where I am going wrong?  
> > > Thankyou for any & all assistance.  
> > > Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 3:40pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/5 "2011-11-15T15:40:37Z")

</div>

Sorry -- just realised why you need the put requests, too -- having a long  
day, sorry!  
Here they are:

curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/1](http://localhost:9200/folksy_test_search/items/1)" -d  
'{"price":10.0,"material\_ids":[1],"listed\_at":"20110915T152837","place\_id":null,"title":"Red  
Jumper","section\_id":1,"user\_login":"user000001","id":1,"category\_id":1,"colour\_ids":[1],"description":"A  
beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
Clothes Jumpers Blue Cotton","subcategory\_id":1}'

curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/2](http://localhost:9200/folksy_test_search/items/2)" -d  
'{"price":10.0,"material\_ids":[1],"listed\_at":"20110915T152837","place\_id":null,"title":"Red  
Jumper","section\_id":1,"user\_login":"user000003","id":2,"category\_id":1,"colour\_ids":[1],"description":"A  
beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
Clothes Jumpers Blue Cotton","subcategory\_id":1}'

curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/3](http://localhost:9200/folksy_test_search/items/3)" -d  
'{"price":3.0,"material\_ids":[1],"listed\_at":"20110915T152838","place\_id":null,"title":"Red  
Jumper","section\_id":1,"user\_login":"user000005","id":3,"category\_id":1,"colour\_ids":[1],"description":"A  
beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
Clothes Jumpers Blue Cotton","subcategory\_id":1}'

curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/4](http://localhost:9200/folksy_test_search/items/4)" -d  
'{"price":3.0,"material\_ids":[1],"listed\_at":"20110915T152839","place\_id":null,"title":"Red  
Jumper","section\_id":1,"user\_login":"user000007","id":4,"category\_id":1,"colour\_ids":[1],"description":"A  
beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
Clothes Jumpers Blue Cotton","subcategory\_id":1}'

Obviously, I only want the latter two items to appear in the query.  
Thanks again,  
Doug.

On 15 November 2011 14:59, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:

> Hi, sure -- the indexing happens automatically to a fresh index as part of  
> testing -- the test process automatically clears the index before tests  
> commence.  
> The output of the search is here: [es-search-results.json · GitHub](https://gist.github.com/1367263)  
> This is for the second search, although it looks the same for the first.  
> & thanks for looking at this.
> 
> On 15 November 2011 11:27, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:
> 
> > Both search requests look good. Can you gist a simple recreation that  
> > shows this? A few curl requests that index fresh data into a fresh index,  
> > and the search request that shows wrong results returning.
> > 
> > On Tue, Nov 15, 2011 at 1:12 PM, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> > 
> > > Hi -- I've been trying to get this query to work, and tried a filtered  
> > > query (as another post suggested), but I still have exactly the same  
> > > problem.  
> > > The current attempt looks like this:  
> > > {  
> > > "size": 18,  
> > > "sort": [  
> > > {  
> > > "listed\_at": "desc"  
> > > }  
> > > ],  
> > > "from": 0,  
> > > "query": {  
> > > "filtered": {  
> > > "filter": {  
> > > "and": [  
> > > {  
> > > "or": [  
> > > {  
> > > "range": {  
> > > "price": {  
> > > "to": 4.99,  
> > > "from": 0  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > ]  
> > > },  
> > > "query": {  
> > > "term": {  
> > > "category\_id": 1  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Basically, I want to select all items under a given category\_id, and  
> > > filter them on (potentially more than one) price range, as well as a number  
> > > of other terms filters (for things like colour\_id, etc.).  
> > > Am I at least heading in sort-of the right direction with this approach?  
> > > Could anyone offer me any insight into what I'm missing about how this  
> > > works that makes me surprised that my efforts aren't returning what I  
> > > expect them to?  
> > > Thanks for any & all help,  
> > > Doug.
> > > 
> > > On 14 November 2011 19:49, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> > > 
> > > > Hi, I have a bunch of records in an index called item, with varying  
> > > > prices.  
> > > > I'm trying to construct a query that will allow me to retrieve items  
> > > > under a particular category\_id, and in one or more price ranges.  
> > > > The query I've come up with is this:
> > > > 
> > > > {  
> > > > "size": 18,  
> > > > "sort": [  
> > > > {  
> > > > "listed\_at": "desc"  
> > > > }  
> > > > ],  
> > > > "from": 0,  
> > > > "query": {  
> > > > "bool": {  
> > > > "must": [  
> > > > {  
> > > > "term": {  
> > > > "category\_id": 1  
> > > > }  
> > > > },  
> > > > {  
> > > > "bool": {  
> > > > "should": [  
> > > > {  
> > > > "range": {  
> > > > "price": {  
> > > > "to": 4.99,  
> > > > "from": 0.0  
> > > > }  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > I was hoping that it would return only items in the category\_id 1, with  
> > > > a price between 0.0 and 4.99 -- however, when I run it, I get only items in  
> > > > category\_id 1 well enough, but I also get _all_ items under that  
> > > > category\_id, including those with a price of over 4.99.  
> > > > Could someone advise me as to where I am going wrong?  
> > > > Thankyou for any & all assistance.  
> > > > Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 4:26pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/6 "2011-11-15T16:26:38Z")

</div>

Damn' -- even this fails:  
$ curl -XGET "  
[http://localhost:9200/folksy\_test\_search/items/\_search?pretty=true](http://localhost:9200/folksy_test_search/items/_search?pretty=true)" -d  
'{"size":18,"sort":[{"listed\_at":"desc"}],"from":0,"query":{"filtered":{"filter":{"range":{"price":{"to":4.99,"from":0.0}}},"query":{"term":{"category\_id":1}}}}}'

I've removed the "and" filter and just put the "range" filter in directly,  
and still it returns items with a price outside the range.

On 15 November 2011 15:40, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:

> Sorry -- just realised why you need the put requests, too -- having a long  
> day, sorry!  
> Here they are:
> 
> curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/1](http://localhost:9200/folksy_test_search/items/1)" -d  
> '{"price":10.0,"material\_ids":[1],"listed\_at":"20110915T152837","place\_id":null,"title":"Red  
> Jumper","section\_id":1,"user\_login":"user000001","id":1,"category\_id":1,"colour\_ids":[1],"description":"A  
> beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
> Clothes Jumpers Blue Cotton","subcategory\_id":1}'
> 
> curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/2](http://localhost:9200/folksy_test_search/items/2)" -d  
> '{"price":10.0,"material\_ids":[1],"listed\_at":"20110915T152837","place\_id":null,"title":"Red  
> Jumper","section\_id":1,"user\_login":"user000003","id":2,"category\_id":1,"colour\_ids":[1],"description":"A  
> beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
> Clothes Jumpers Blue Cotton","subcategory\_id":1}'
> 
> curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/3](http://localhost:9200/folksy_test_search/items/3)" -d  
> '{"price":3.0,"material\_ids":[1],"listed\_at":"20110915T152838","place\_id":null,"title":"Red  
> Jumper","section\_id":1,"user\_login":"user000005","id":3,"category\_id":1,"colour\_ids":[1],"description":"A  
> beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
> Clothes Jumpers Blue Cotton","subcategory\_id":1}'
> 
> curl -XPUT "[http://localhost:9200/folksy\_test\_search/items/4](http://localhost:9200/folksy_test_search/items/4)" -d  
> '{"price":3.0,"material\_ids":[1],"listed\_at":"20110915T152839","place\_id":null,"title":"Red  
> Jumper","section\_id":1,"user\_login":"user000007","id":4,"category\_id":1,"colour\_ids":[1],"description":"A  
> beautiful red jumper hand-knitted with quality wool.","keywords":"Men  
> Clothes Jumpers Blue Cotton","subcategory\_id":1}'
> 
> Obviously, I only want the latter two items to appear in the query.  
> Thanks again,  
> Doug.
> 
> On 15 November 2011 14:59, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> 
> > Hi, sure -- the indexing happens automatically to a fresh index as part  
> > of testing -- the test process automatically clears the index before tests  
> > commence.  
> > The output of the search is here: [es-search-results.json · GitHub](https://gist.github.com/1367263)  
> > This is for the second search, although it looks the same for the first.  
> > & thanks for looking at this.
> > 
> > On 15 November 2011 11:27, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:
> > 
> > > Both search requests look good. Can you gist a simple recreation that  
> > > shows this? A few curl requests that index fresh data into a fresh index,  
> > > and the search request that shows wrong results returning.
> > > 
> > > On Tue, Nov 15, 2011 at 1:12 PM, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> > > 
> > > > Hi -- I've been trying to get this query to work, and tried a filtered  
> > > > query (as another post suggested), but I still have exactly the same  
> > > > problem.  
> > > > The current attempt looks like this:  
> > > > {  
> > > > "size": 18,  
> > > > "sort": [  
> > > > {  
> > > > "listed\_at": "desc"  
> > > > }  
> > > > ],  
> > > > "from": 0,  
> > > > "query": {  
> > > > "filtered": {  
> > > > "filter": {  
> > > > "and": [  
> > > > {  
> > > > "or": [  
> > > > {  
> > > > "range": {  
> > > > "price": {  
> > > > "to": 4.99,  
> > > > "from": 0  
> > > > }  
> > > > }  
> > > > }  
> > > > ]  
> > > > }  
> > > > ]  
> > > > },  
> > > > "query": {  
> > > > "term": {  
> > > > "category\_id": 1  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > Basically, I want to select all items under a given category\_id, and  
> > > > filter them on (potentially more than one) price range, as well as a number  
> > > > of other terms filters (for things like colour\_id, etc.).  
> > > > Am I at least heading in sort-of the right direction with this approach?  
> > > > Could anyone offer me any insight into what I'm missing about how this  
> > > > works that makes me surprised that my efforts aren't returning what I  
> > > > expect them to?  
> > > > Thanks for any & all help,  
> > > > Doug.
> > > > 
> > > > On 14 November 2011 19:49, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:
> > > > 
> > > > > Hi, I have a bunch of records in an index called item, with varying  
> > > > > prices.  
> > > > > I'm trying to construct a query that will allow me to retrieve items  
> > > > > under a particular category\_id, and in one or more price ranges.  
> > > > > The query I've come up with is this:
> > > > > 
> > > > > {  
> > > > > "size": 18,  
> > > > > "sort": [  
> > > > > {  
> > > > > "listed\_at": "desc"  
> > > > > }  
> > > > > ],  
> > > > > "from": 0,  
> > > > > "query": {  
> > > > > "bool": {  
> > > > > "must": [  
> > > > > {  
> > > > > "term": {  
> > > > > "category\_id": 1  
> > > > > }  
> > > > > },  
> > > > > {  
> > > > > "bool": {  
> > > > > "should": [  
> > > > > {  
> > > > > "range": {  
> > > > > "price": {  
> > > > > "to": 4.99,  
> > > > > "from": 0.0  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > }  
> > > > > ]  
> > > > > }  
> > > > > }  
> > > > > }
> > > > > 
> > > > > I was hoping that it would return only items in the category\_id 1,  
> > > > > with a price between 0.0 and 4.99 -- however, when I run it, I get only  
> > > > > items in category\_id 1 well enough, but I also get _all_ items under that  
> > > > > category\_id, including those with a price of over 4.99.  
> > > > > Could someone advise me as to where I am going wrong?  
> > > > > Thankyou for any & all assistance.  
> > > > > Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 4:40pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/7 "2011-11-15T16:40:24Z")

</div>

So the full gist of actions is here: [https://gist.github.com/1367538](https://gist.github.com/1367538)  
The last two lines are search queries, that I believe should only return  
the items with a price of 3.00.  
Sorry I keep posting as I realise I've not given a full description --  
we're getting close to our big deploy, and my head's fragmenting! 🙂  
Cheers,  
Doug.

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [November 15, 2011, 4:44pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/8 "2011-11-15T16:44:27Z")

</div>

Hi Doug

On Tue, 2011-11-15 at 16:26 +0000, doug livesey wrote:

> Damn' -- even this fails:  
> $ curl -XGET  
> "[http://localhost:9200/folksy\_test\_search/items/\_search?pretty=true](http://localhost:9200/folksy_test_search/items/_search?pretty=true)"  
> -d  
> '{"size":18,"sort":[{"listed\_at":"desc"}],"from":0,"query":{"filtered":{"filter":{"range":{"price":{"to":4.99,"from":0.0}}},"query":{"term":{"category\_id":1}}}}}'

It is really much easier to help if these code examples are gisted and  
formatted properly. There are just too many {{{{{}}}} to check  
visually.

Also, if you create a complete example, you will probably find your  
error.

I think that, if you check the mapping for 'item' you will find that the  
'price' field is mapped as a string, so your "from" and "to" parameters  
are being treated as strings, not as numbers.

When I try your example docs plus your example search, then it works for  
me. If I change the mapping for "price" to "string", then I get all  
results back.

You probably indexed a price as "0.50" early on, which messed up your  
mapping.

> 

good luck 🙂

clint

> I've removed the "and" filter and just put the "range" filter in  
> directly, and still it returns items with a price outside the range.
> 
> On 15 November 2011 15:40, doug livesey [biot023@gmail.com](mailto:biot023@gmail.com) wrote:  
> Sorry -- just realised why you need the put requests, too --  
> having a long day, sorry!  
> Here they are:
> 
> ```
> curl -XPUT "http://localhost:9200/folksy_test_search/items/1"
> -d
> '{"price":10.0,"material_ids":[1],"listed_at":"20110915T152837","place_id":null,"title":"Red Jumper","section_id":1,"user_login":"user000001","id":1,"category_id":1,"colour_ids":[1],"description":"A beautiful red jumper hand-knitted with quality wool.","keywords":"Men Clothes Jumpers Blue Cotton","subcategory_id":1}' 
>     
>     
> curl -XPUT "http://localhost:9200/folksy_test_search/items/2"
> -d
> '{"price":10.0,"material_ids":[1],"listed_at":"20110915T152837","place_id":null,"title":"Red Jumper","section_id":1,"user_login":"user000003","id":2,"category_id":1,"colour_ids":[1],"description":"A beautiful red jumper hand-knitted with quality wool.","keywords":"Men Clothes Jumpers Blue Cotton","subcategory_id":1}' 
>     
>     
> curl -XPUT "http://localhost:9200/folksy_test_search/items/3"
> -d
> '{"price":3.0,"material_ids":[1],"listed_at":"20110915T152838","place_id":null,"title":"Red Jumper","section_id":1,"user_login":"user000005","id":3,"category_id":1,"colour_ids":[1],"description":"A beautiful red jumper hand-knitted with quality wool.","keywords":"Men Clothes Jumpers Blue Cotton","subcategory_id":1}' 
>     
>     
> curl -XPUT "http://localhost:9200/folksy_test_search/items/4"
> -d
> '{"price":3.0,"material_ids":[1],"listed_at":"20110915T152839","place_id":null,"title":"Red Jumper","section_id":1,"user_login":"user000007","id":4,"category_id":1,"colour_ids":[1],"description":"A beautiful red jumper hand-knitted with quality wool.","keywords":"Men Clothes Jumpers Blue Cotton","subcategory_id":1}' 
>     
>     
> Obviously, I only want the latter two items to appear in the
> query. 
> Thanks again, 
> Doug. 
>     
> On 15 November 2011 14:59, doug livesey <biot023@gmail.com>
> wrote:
> Hi, sure -- the indexing happens automatically to a
> fresh index as part of testing -- the test process
> automatically clears the index before tests commence. 
> The output of the search is
> here: https://gist.github.com/1367263 
> This is for the second search, although it looks the
> same for the first. 
> & thanks for looking at this. 
>             
>             
> On 15 November 2011 11:27, Shay Banon
> <kimchy@gmail.com> wrote: 
> Both search requests look good. Can you gist a
> simple recreation that shows this? A few curl
> requests that index fresh data into a fresh
> index, and the search request that shows wrong
> results returning. 
>                     
>                     
> On Tue, Nov 15, 2011 at 1:12 PM, doug livesey
> <biot023@gmail.com> wrote:
> Hi -- I've been trying to get this
> query to work, and tried a filtered
> query (as another post suggested), but
> I still have exactly the same
> problem. 
> The current attempt looks like this: 
> { 
> "size": 18, 
> "sort": [ 
> { 
> "listed_at": "desc" 
> } 
> ], 
> "from": 0, 
> "query": { 
> "filtered": { 
> "filter": { 
> "and": [ 
> { 
> "or": [ 
> { 
>                             
> "range": { 
>                             
> "price": { 
>                             
> "to": 4.99, 
>                             
> "from": 0 
> } 
> } 
> } 
> ] 
> } 
> ] 
> }, 
> "query": { 
> "term": { 
> "category_id": 1 
> } 
> } 
> } 
> } 
> } 
>                             
>                             
> Basically, I want to select all items
> under a given category_id, and filter
> them on (potentially more than one)
> price range, as well as a number of
> other terms filters (for things like
> colour_id, etc.). 
> Am I at least heading in sort-of the
> right direction with this approach? 
> Could anyone offer me any insight into
> what I'm missing about how this works
> that makes me surprised that my
> efforts aren't returning what I expect
> them to? 
> Thanks for any & all help, 
> Doug. 
>                             
> On 14 November 2011 19:49, doug
> livesey <biot023@gmail.com> wrote:
> Hi, I have a bunch of records
> in an index called item, with
> varying prices. 
> I'm trying to construct a
> query that will allow me to
> retrieve items under a
> particular category_id, and in
> one or more price ranges. 
> The query I've come up with is
> this: 
>                                     
>                                     
> { 
> "size": 18, 
> "sort": [ 
> { 
> "listed_at":
> "desc" 
> } 
> ], 
> "from": 0, 
> "query": { 
> "bool": { 
> "must": [ 
> { 
> "term": { 
>                                     
> "category_id": 1 
> } 
> }, 
> { 
> "bool": { 
>                                     
> "should": [ 
> { 
>                                     
> "range": { 
>                                     
> "price": { 
>                                     
> "to": 4.99, 
>                                     
> "from": 0.0 
>                                     
> } 
>                                     
> } 
> } 
> ] 
> } 
> } 
> ] 
> } 
> } 
> } 
>                                     
>                                     
> I was hoping that it would
> return only items in the
> category_id 1, with a price
> between 0.0 and 4.99 --
> however, when I run it, I get
> only items in category_id 1
> well enough, but I also get
> *all* items under that
> category_id, including those
> with a price of over 4.99. 
> Could someone advise me as to
> where I am going wrong? 
> Thankyou for any & all
> assistance. 
> Doug. 
> 
> ```

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [November 15, 2011, 4:51pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/9 "2011-11-15T16:51:36Z")

</div>

On Tue, 2011-11-15 at 16:40 +0000, doug livesey wrote:

> So the full gist of actions is here: [gist:1367538 · GitHub](https://gist.github.com/1367538)  
> The last two lines are search queries, that I believe should only  
> return the items with a price of 3.00.

I've tried out your examples, and bot queries return 2 results.

Have you deleted your index and run your examples as you have laid them  
out?

If so, and you're still getting wrong results, what version of ES are  
you using?

clint

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [November 15, 2011, 5:14pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/10 "2011-11-15T17:14:05Z")

</div>

Deleting the index did it -- thankyou very much!  
& sorry about the fragmentary posting -- I think I may have overdone  
the caffeine, today!  
One final question regarding the two queries I put together to try to solve  
this -- which of them would be the more efficient?  
The first, that used a boolean query?  
Or the second, that used a filtered query?  
(Please say the second -- that's where the code's at, now! 😉 )  
& thankyou very much, once again,  
Doug.

On 15 November 2011 16:51, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> On Tue, 2011-11-15 at 16:40 +0000, doug livesey wrote:
> 
> > So the full gist of actions is here: [gist:1367538 · GitHub](https://gist.github.com/1367538)  
> > The last two lines are search queries, that I believe should only  
> > return the items with a price of 3.00.
> 
> I've tried out your examples, and bot queries return 2 results.
> 
> Have you deleted your index and run your examples as you have laid them  
> out?
> 
> If so, and you're still getting wrong results, what version of ES are  
> you using?
> 
> clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [November 15, 2011, 5:33pm UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/11 "2011-11-15T17:33:48Z")

</div>

On Tue, 2011-11-15 at 17:14 +0000, doug livesey wrote:

> Deleting the index did it -- thankyou very much!

glad it helped

> & sorry about the fragmentary posting -- I think I may have overdone  
> the caffeine, today!

I could hear the desperation 🙂

> One final question regarding the two queries I put together to try to  
> solve this -- which of them would be the more efficient?  
> The first, that used a boolean query?  
> Or the second, that used a filtered query?  
> (Please say the second -- that's where the code's at, now! 😉 )

The filtered query. Also, your category\_id is also just a filter - no  
need to run it as a query.

You could do this instead:

curl -XGET  
'[http://127.0.0.1:9200/folksy\_test\_search/items/\_search?pretty=1](http://127.0.0.1:9200/folksy_test_search/items/_search?pretty=1)' -d '  
{  
"sort" : [  
{  
"listed\_at" : "desc"  
}  
],  
"query" : {  
"constant\_score" : {  
"filter" : {  
"and" : [  
{  
"term" : {  
"category\_id" : 1  
}  
},  
{  
"numeric\_range" : {  
"price" : {  
"lte" : 4.99,  
"gte" : 0  
}  
}  
}  
]  
}  
}  
},  
"from" : 0,  
"size" : 18  
}  
'

Note:

- I used "numeric\_range" instead of "range" which is more efficient  
for fields that have many many terms (eg datetime). Your price  
field may only have a small number of terms, in which case, change  
it back to "range"

- I used "gte" and "lte" rather than "to" and "from" because I think  
you want 0 to 4.99 INCLUSIVE. "from" would stop before 4.99.

clint

---

<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:48am UTC](https://discuss.elastic.co/t/nested-boolean-range-query-returning-values-outside-of-its-range/5870/12 "2017-07-06T03:48:33Z")

</div>


