# Bool + range query performance

**URL:** <https://discuss.elastic.co/t/bool-range-query-performance/21255>\
**Category:** Elasticsearch\
**Created:** [December 15, 2014, 9:09am UTC](https://discuss.elastic.co/t/bool-range-query-performance/21255 "2014-12-15T09:09:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [December 15, 2014, 9:09am UTC](https://discuss.elastic.co/t/bool-range-query-performance/21255/1 "2014-12-15T09:09:26Z")

</div>

Hi,  
I'm trying to execute a rather complicated nested bool query:  
((string match and range) OR (string match and range)) AND((string match and range) OR (string match and range)) AND ...

and I see that the range query has a tremendous effect on performance (~400ms per range).  
Been making an effort to run this under nested filter but haven't been able to build a valid query.

Currently the query looks like this:

{  
"size": 0,  
"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "obj1",  
"query": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"match": {  
"var": "example4"  
}  
},  
{  
"range": {  
"absolutePvs": {  
"gte": 1  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"match": {  
"var": "example3"  
}  
},  
{  
"range": {  
"absolutePvs": {  
"gte": 1  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}  
},  
{  
"nested": {  
"path": "obj2",  
"query": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"match": {  
"var": "example1"  
}  
},  
{  
"range": {  
"absolutePvs": {  
"gte": 1  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"match": {  
"var": "example2"  
}  
},  
{  
"range": {  
"absolutePvs": {  
"gte": 1  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}  
}  
]  
}  
}  
}

currently this query takes ~3 seconds to run. When removing the range conditions, the query runs  
in ~300ms.  
Does anyone know how can I improve this???

Thanks in advanced,  
Oren

---

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [December 15, 2014, 10:34am UTC](https://discuss.elastic.co/t/bool-range-query-performance/21255/2 "2014-12-15T10:34:29Z")

</div>

In case someone else encounters this problem, I found the solution: using filtered query + and filters:

Runs really really fast now 🙂

POST \_search  
{  
"size": 1,  
"query": {  
"filtered": {  
"filter": {  
"and": {  
"filters": [  
{  
"nested": {  
"path": "obj1",  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"fquery": {  
"query": {  
"match": {  
"var": "example4"  
}  
},  
"\_cache": true  
}  
},  
{  
"range": {  
"Pvs": {  
"gte": 1  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"fquery": {  
"query": {  
"match": {  
"var": "example3"  
}  
},  
"\_cache": true  
}  
},  
{  
"range": {  
"Pvs": {  
"gte": 1  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}  
}  
}  
},  
{  
"nested": {  
"path": "obj2",  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"should": [  
{  
"bool": {  
"must": [  
{  
"fquery": {  
"query": {  
"match": {  
"var": "example2"  
}  
},  
"\_cache": true  
}  
},  
{  
"range": {  
"Pvs": {  
"gte": 1  
}  
}  
}  
]  
}  
},  
{  
"bool": {  
"must": [  
{  
"fquery": {  
"query": {  
"match": {  
"var": "example1"  
}  
},  
"\_cache": true  
}  
},  
{  
"range": {  
"Pvs": {  
"gte": 1  
}  
}  
}  
]  
}  
}  
]  
}  
}  
}  
}  
}  
}  
]  
}  
}  
}  
}  
}

---

<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, 12:43am UTC](https://discuss.elastic.co/t/bool-range-query-performance/21255/3 "2017-07-06T00:43:44Z")

</div>


