# Post filter to grasp only a % of items in the primary results set?

**URL:** https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766
**Category:** Elasticsearch
**Created:** [July 18, 2016, 1:37pm UTC](https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766 "2016-07-18T13:37:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![christophe\_spielmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christophe_spielmann/32/49567_2.png) [@christophe\_spielmann](https://discuss.elastic.co/u/christophe_spielmann)
#### Post date: [July 18, 2016, 1:37pm UTC](https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766/1 "2016-07-18T13:37:23Z")

</div>

Hello,

For QC purpose, my users want to randomly select a % of found documents of the a query they built within elasticsearch.  
I was think of using Post filter feature with a script that does random fetching.  
Is it the correct approach ?  
I don't find any pointer in the documentation about that .

regards.

---

<div class="post-metadata">

### Author: ![christophe\_spielmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christophe_spielmann/32/49567_2.png) [@christophe\_spielmann](https://discuss.elastic.co/u/christophe_spielmann)
#### Post date: [July 18, 2016, 2:11pm UTC](https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766/2 "2016-07-18T14:11:11Z")

</div>

I saw native scripting as well, [https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html)  
Not clear to me the invocation stack of this class/object as I will use "global" variables.  
Can I hook the first execution of that script in a filter scope ?

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [July 19, 2016, 1:23pm UTC](https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766/3 "2016-07-19T13:23:04Z")

</div>

Honestly, I think this is something best accomplished in your application code. You _could_ use something like a script query in the `post_filter` which randomly includes/excludes documents, but this won't be terribly efficient since it will traverse over all of the matching results.

E.g. if your query matches 1000 documents, but you only ask for the top-10 results, the `post_filter` will still run over all 1000 docs.

This is easily handled in your application code though. Just ask for `n` documents and randomly filter out some of them.

---

<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:34pm UTC](https://discuss.elastic.co/t/post-filter-to-grasp-only-a-of-items-in-the-primary-results-set/55766/4 "2017-07-05T22:34:24Z")

</div>


