# Combine range query with term

**URL:** <https://discuss.elastic.co/t/combine-range-query-with-term/202773>\
**Category:** Elasticsearch\
**Created:** [October 9, 2019, 6:53am UTC](https://discuss.elastic.co/t/combine-range-query-with-term/202773 "2019-10-09T06:53:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stella\_Sunshine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stella_sunshine/32/55593_2.png) [@Stella\_Sunshine](https://discuss.elastic.co/u/Stella_Sunshine)\
**Post date:** [October 9, 2019, 6:53am UTC](https://discuss.elastic.co/t/combine-range-query-with-term/202773/1 "2019-10-09T06:53:33Z")

</div>

Hey everybody,  
the title seems as if the solution is easy... but my problem is a bit more tricky.  
I have several products which can have several articles and each article has a price list with different price list ids.

This is a snippet of my elastic document:

"article": [  
{  
"price\_list": [  
{  
"price\_list\_id": 1,  
"price\_net": 756.26  
},  
{  
"price\_list\_id": 2,  
"price\_net": 688.2  
},  
{  
"price\_list\_id": 3,  
"price\_net": 688.2  
},  
{  
"price\_list\_id": 4,  
"price\_net": 739.45  
}  
]  
}  
]

And I need to get all products which have at least one article with the price\_list\_id 4 AND a price\_net range with gte = 413 and lte = 702.

If I try this :

"bool": {  
"must": [  
{  
"range": {  
"article.price\_list.price\_net": {  
"gte": 413,  
"lte": 702  
}  
}  
},  
{  
"term": {  
"article.price\_list.price\_list\_id": 4  
}  
}  
]  
}

the above snippet matches. But it should not because the price\_net with the price\_list\_id = 4 is not between 413 and 702

Has anyone a solution for my problem?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 9, 2019, 7:00am UTC](https://discuss.elastic.co/t/combine-range-query-with-term/202773/2 "2019-10-09T07:00:13Z")

</div>

If you are searching for multiple fields within an array, then you need to take a look at the [nested datatype](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/nested.html) and the [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-nested-query.html)

hope this helps!

--Alex

---

<div class="post-metadata">

**Author:** ![Stella\_Sunshine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stella_sunshine/32/55593_2.png) [@Stella\_Sunshine](https://discuss.elastic.co/u/Stella_Sunshine)\
**Post date:** [October 9, 2019, 7:36am UTC](https://discuss.elastic.co/t/combine-range-query-with-term/202773/3 "2019-10-09T07:36:40Z")

</div>

Thanks a lot. That helped. 🙂

For all others:

I adapted the mapping

"price\_list": {  
**"type": "nested",**  
"properties": {  
"price\_list\_id": {  
"type": "keyword"  
},  
"price\_net": {  
"type": "float"  
}  
}  
}

and my query looks like this now:

"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "article.price\_list",  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"article.price\_list.price\_net": {  
"gte": 413,  
"lte": 702  
}  
}  
},  
{  
"term": {  
"article.price\_list.price\_list\_id": 4  
}  
}  
]  
}  
}  
}  
}  
]  
}

---

<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:** [November 6, 2019, 7:36am UTC](https://discuss.elastic.co/t/combine-range-query-with-term/202773/4 "2019-11-06T07:36:41Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
