# Implicit Custom Filter?

**URL:** <https://discuss.elastic.co/t/implicit-custom-filter/17817>\
**Category:** Elasticsearch\
**Created:** [May 29, 2014, 6:09pm UTC](https://discuss.elastic.co/t/implicit-custom-filter/17817 "2014-05-29T18:09:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wshaib](https://avatars.discourse-cdn.com/v4/letter/w/ee59a6/32.png) [@wshaib](https://discuss.elastic.co/u/wshaib)\
**Post date:** [May 29, 2014, 6:09pm UTC](https://discuss.elastic.co/t/implicit-custom-filter/17817/1 "2014-05-29T18:09:50Z")

</div>

I am trying to set up document-level security for my index. The documents  
have fields which will be filtered on to enforce access permissions.

My question is: given a query, is it possible to set things up so that ES  
will invoke a custom script filter on _every_ clause in said query without  
having to munge the query myself to insert the filter explicitly?

For example, if a query is:

filtered: {  
query: {  
term: { foo: "bar" }  
},  
filter: {  
has\_parent: {  
type: "some\_type",  
query: {  
term: { blah: "xyz" }  
}  
}  
}  
}

then, I would want my custom filter invoked (implicitly) on both term  
queries above.  
Is there an alternative to doing the above without preprocessing the query  
and explicitly inserting my custom filter everywhere?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [May 29, 2014, 6:16pm UTC](https://discuss.elastic.co/t/implicit-custom-filter/17817/2 "2014-05-29T18:16:32Z")

</div>

Two options come to mind:

1. Filtered aliases:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered)

2. Search template and Template queries:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

The last feature is new and I have yet to try it out.

Cheers,

Ivan

On Thu, May 29, 2014 at 11:09 AM, W Shaib [wshaib@gmail.com](mailto:wshaib@gmail.com) wrote:

> I am trying to set up document-level security for my index. The documents  
> have fields which will be filtered on to enforce access permissions.
> 
> My question is: given a query, is it possible to set things up so that ES  
> will invoke a custom script filter on _every_ clause in said query without  
> having to munge the query myself to insert the filter explicitly?
> 
> For example, if a query is:
> 
> filtered: {  
> query: {  
> term: { foo: "bar" }  
> },  
> filter: {  
> has\_parent: {  
> type: "some\_type",  
> query: {  
> term: { blah: "xyz" }  
> }  
> }  
> }  
> }
> 
> then, I would want my custom filter invoked (implicitly) on both term  
> queries above.  
> Is there an alternative to doing the above without preprocessing the query  
> and explicitly inserting my custom filter everywhere?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBfZHPSRiRTiZJ%2BngcRXZ2PdJQFi8JBx-TK1MRHc81Y\_Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBfZHPSRiRTiZJ%2BngcRXZ2PdJQFi8JBx-TK1MRHc81Y_Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![wshaib](https://avatars.discourse-cdn.com/v4/letter/w/ee59a6/32.png) [@wshaib](https://discuss.elastic.co/u/wshaib)\
**Post date:** [May 29, 2014, 6:43pm UTC](https://discuss.elastic.co/t/implicit-custom-filter/17817/3 "2014-05-29T18:43:23Z")

</div>

Thanks for the quick reply. unless I am missing something, these  
suggestions do not quite do what I need...

1. alias filters would not work since the filters associated with them are  
static, whereas I would need a dynamic a filter (one with params, so each  
query will include the specific values to filter for, depending on who is  
querying)
2. not sure how templates work for this... an elaboration would be  
appreciated.

On Thursday, 29 May 2014 14:16:39 UTC-4, Ivan Brusic wrote:

> Two options come to mind:
> 
> 1. Filtered aliases:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered)
> 
> 2. Search template and Template queries:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-template-query.html)
> 
> The last feature is new and I have yet to try it out.
> 
> Cheers,
> 
> Ivan
> 
> On Thu, May 29, 2014 at 11:09 AM, W Shaib \<[wsh...@gmail.com](mailto:wsh...@gmail.com) \<javascript:\>\>wrote:
> 
> > I am trying to set up document-level security for my index. The documents  
> > have fields which will be filtered on to enforce access permissions.
> > 
> > My question is: given a query, is it possible to set things up so that ES  
> > will invoke a custom script filter on _every_ clause in said query without  
> > having to munge the query myself to insert the filter explicitly?
> > 
> > For example, if a query is:
> > 
> > filtered: {  
> > query: {  
> > term: { foo: "bar" }  
> > },  
> > filter: {  
> > has\_parent: {  
> > type: "some\_type",  
> > query: {  
> > term: { blah: "xyz" }  
> > }  
> > }  
> > }  
> > }
> > 
> > then, I would want my custom filter invoked (implicitly) on both term  
> > queries above.  
> > Is there an alternative to doing the above without preprocessing the  
> > query and explicitly inserting my custom filter everywhere?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/80f569b4-efca-4c46-a028-0220cfb61375%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e8fd192f-0d6b-4227-bcd5-64458ad29042%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e8fd192f-0d6b-4227-bcd5-64458ad29042%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:26am UTC](https://discuss.elastic.co/t/implicit-custom-filter/17817/4 "2017-07-06T01:26:02Z")

</div>


