# How to implement custom security results trimming as plugin

**URL:** <https://discuss.elastic.co/t/how-to-implement-custom-security-results-trimming-as-plugin/118409>\
**Category:** Elasticsearch\
**Created:** [February 5, 2018, 8:49am UTC](https://discuss.elastic.co/t/how-to-implement-custom-security-results-trimming-as-plugin/118409 "2018-02-05T08:49:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rail](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rail/32/27363_2.png) [@Rail](https://discuss.elastic.co/u/Rail)\
**Post date:** [February 5, 2018, 8:49am UTC](https://discuss.elastic.co/t/how-to-implement-custom-security-results-trimming-as-plugin/118409/1 "2018-02-05T08:49:14Z")

</div>

Hello, Elastic community!

I need to implement trimming for any query results according to some logic. Ideally, it would be great to have some integration point in Elasticsearch using plugin which will be called for each item in query result where we can check is it allowed to return the item or not based on some document Id ('Id' field).

Something like this

```
foreach(item in queryResult)
{
    var id = item.getField("Id')
    if (CanUserViewThisDocument(id))
    {
         AddToQueryResult(item)
    }
}

```

`CanUserViewThisDocument` function suppose to be quite fast.

One way implement results trimming is to express trimming rule as a Query and use BitSet operations on initial query results (like here [https://stackoverflow.com/a/41034500](https://stackoverflow.com/a/41034500)). Unfortunately, it's not possible to express the trimming rule as Query or List of Ids.

---

<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:** [March 5, 2018, 9:01am UTC](https://discuss.elastic.co/t/how-to-implement-custom-security-results-trimming-as-plugin/118409/2 "2018-03-05T09:01:43Z")

</div>

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