# The cheapest price for every search result Elasticsearch

**URL:** <https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798>\
**Category:** Elasticsearch\
**Created:** [November 7, 2018, 10:17pm UTC](https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798 "2018-11-07T22:17:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![janderot](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@janderot](https://discuss.elastic.co/u/janderot)\
**Post date:** [November 7, 2018, 10:17pm UTC](https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798/1 "2018-11-07T22:17:40Z")

</div>

I'm making a price comparison website. Many products has multiple webshops (prices). For each result based on a match query, I want to return the cheapest price of each result.

For example: the match query returns 30 results based on the title: "test". Every single results show the cheapest price.

Now I have this code:

GET /products/\_search?  
{  
"query": {  
"match": {  
"title": "test"  
}  
},  
"aggs": {  
"Min": {  
"min": {  
"field": "price"  
}  
}  
}  
}

It returns now 1 aggregation over all results.

---

<div class="post-metadata">

**Author:** ![Jaspreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaspreet_singh/32/58296_2.png) [@Jaspreet\_Singh](https://discuss.elastic.co/u/Jaspreet_Singh)\
**Post date:** [November 8, 2018, 5:12am UTC](https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798/2 "2018-11-08T05:12:14Z")

</div>

I havent tried this but this seems to be doing what you need - [https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-pipeline-min-bucket-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-pipeline-min-bucket-aggregation.html)

---

<div class="post-metadata">

**Author:** ![janderot](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@janderot](https://discuss.elastic.co/u/janderot)\
**Post date:** [November 11, 2018, 3:58pm UTC](https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798/4 "2018-11-11T15:58:40Z")

</div>

Now I have a top\_hits aggregation performed on a query. The pipeline aggregation only works when a parent aggregation exists.

This top\_hits query works fine, but it's still 1 overall aggregation result:  
GET /books/\_search?  
{  
"query": {  
"match": {  
"title": "judas"  
}  
},  
"aggs": {  
"cheapest price": {  
"top\_hits": {  
"size": 1,  
"\_source": {  
"includes": ["webshopName", "price", "productUrl"]  
},  
"sort": [{ "price": { "order": "asc" } }]  
}  
}  
}  
}

---

<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:** [December 9, 2018, 3:58pm UTC](https://discuss.elastic.co/t/the-cheapest-price-for-every-search-result-elasticsearch/155798/5 "2018-12-09T15:58:42Z")

</div>

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