# Search by field percent

**URL:** <https://discuss.elastic.co/t/search-by-field-percent/52864>\
**Category:** Elasticsearch\
**Created:** [June 15, 2016, 1:08pm UTC](https://discuss.elastic.co/t/search-by-field-percent/52864 "2016-06-15T13:08:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enp/32/3825_2.png) [@enp](https://discuss.elastic.co/u/enp)\
**Post date:** [June 15, 2016, 1:08pm UTC](https://discuss.elastic.co/t/search-by-field-percent/52864/1 "2016-06-15T13:08:10Z")

</div>

Hi

Suppose, I have such objects in index:

{'name':'John','age':25},  
{'name':'Mary','age':27},  
{'name':'Jack','age':22}

How to search objects with age \> 90% of max age?

Percentage for John is 25_100/27=92, for jack is 22_100/27=81, for Mary is 100. So, 90% filter must select Mary and John, but it must pass Jack.

What is the best way to implement such search logic?

---

<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:** [June 16, 2016, 8:37am UTC](https://discuss.elastic.co/t/search-by-field-percent/52864/2 "2016-06-16T08:37:25Z")

</div>

Hey,

how about: First execute an aggregation to get the max value, then calculate the 90% out of that (to get the absolute value, might work with some scripting as well), and use this absolute value in a second query.

--Alex

---

<div class="post-metadata">

**Author:** ![enp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enp/32/3825_2.png) [@enp](https://discuss.elastic.co/u/enp)\
**Post date:** [June 16, 2016, 8:50am UTC](https://discuss.elastic.co/t/search-by-field-percent/52864/3 "2016-06-16T08:50:47Z")

</div>

Already done, but I was searching for solution with one request (maybe with some groovy scripting on server side)

---

<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:** [June 16, 2016, 9:21am UTC](https://discuss.elastic.co/t/search-by-field-percent/52864/4 "2016-06-16T09:21:55Z")

</div>

Hey,

no, the execution order of search and aggregation is opposite as needed for your use-case (as the aggregations built in the search results and you need the search results built on top the aggs result - which would require more roundtrips).

--Alex

---

<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 5, 2017, 10:43pm UTC](https://discuss.elastic.co/t/search-by-field-percent/52864/5 "2017-07-05T22:43:23Z")

</div>


