# Way to filtering search result by user/dept role?

**URL:** <https://discuss.elastic.co/t/way-to-filtering-search-result-by-user-dept-role/225842>\
**Category:** Elasticsearch\
**Created:** [March 31, 2020, 11:30am UTC](https://discuss.elastic.co/t/way-to-filtering-search-result-by-user-dept-role/225842 "2020-03-31T11:30:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![111307](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111307/32/65379_2.png) [@111307](https://discuss.elastic.co/u/111307)\
**Post date:** [March 31, 2020, 11:30am UTC](https://discuss.elastic.co/t/way-to-filtering-search-result-by-user-dept-role/225842/1 "2020-03-31T11:30:05Z")

</div>

Hi Experts,

I'm configuring intranet search page. One of the requirements of this project is that users can get the search result only in their department's data. I have "user id" & "dept id" fields but dont know how to show only for their department's.  
should I filter search result after search query or filter user/department first and send query? Please give me some advice.

---

<div class="post-metadata">

**Author:** ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)\
**Post date:** [April 1, 2020, 3:47pm UTC](https://discuss.elastic.co/t/way-to-filtering-search-result-by-user-dept-role/225842/2 "2020-04-01T15:47:18Z")

</div>

@giani do you have the user's ID and dept ID values when you run the query?

With a subscription, you would typically accomplish this with [document level security](https://www.elastic.co/guide/en/elasticsearch/reference/current/document-level-security.html) which allows you to build a filter template referencing user metadata (like ID and department) that applies to every query a user runs. See the docs on that [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/field-and-document-access-control.html#templating-role-query)

---

<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:** [April 29, 2020, 3:47pm UTC](https://discuss.elastic.co/t/way-to-filtering-search-result-by-user-dept-role/225842/3 "2020-04-29T15:47:18Z")

</div>

Closed on April 29, 2020, 3:47pm UTC

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