# Force query on field

**URL:** <https://discuss.elastic.co/t/force-query-on-field/130051>\
**Category:** Elasticsearch\
**Created:** [April 30, 2018, 5:58pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051 "2018-04-30T17:58:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucastex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucastex/32/30619_2.png) [@lucastex](https://discuss.elastic.co/u/lucastex)\
**Post date:** [April 30, 2018, 5:58pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/1 "2018-04-30T17:58:43Z")

</div>

Hi guys,

Is there any config in ElasticSearch, any plugin, any feature in x-pack or something on elastic cloud, that forces users to have one condition on its query automatically?

I'm trying to force that user add one condition "customer\_id: " to identify my customer data among all data from all customers. I wish I could use x-pack feature to automatically add this query based on user metadata, but in my database I have 5k customers and growing, and I do not want to intercept my queries in the backend, add this condition and query ES. I'd like to query from my javascript application.

So, if the user makes a query without this condition, I'd return no results for him.

Thank you,

[]s,

Lucas

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 30, 2018, 6:21pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/2 "2018-04-30T18:21:59Z")

</div>

Using [document-level security](https://www.elastic.co/guide/en/x-pack/current/field-and-document-access-control.html) in X-Pack, you can create roles that contain this criteria. You can then assign a separate role to each customer so that they only can see the appropriate data. You can also use templating to simplify this mechanism. This is [described in this blog post](https://www.elastic.co/blog/securing-gdpr-personal-data-with-access-controls).

---

<div class="post-metadata">

**Author:** ![lucastex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucastex/32/30619_2.png) [@lucastex](https://discuss.elastic.co/u/lucastex)\
**Post date:** [May 2, 2018, 7:45pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/3 "2018-05-02T19:45:51Z")

</div>

Hi Christian! Thanks for replying. Yes, this was my first approach and probably the best one! 🙂

The fact is that I'll have one customer user in elasticsearch for each one in my database. Will this scale? I'm talking about 3k users today and ramping up to reach 5k in the next 12~18 months.

Do you think this can be an issue? I'm using ElasticSearch in Elastic Cloud (SaaS).

Thank you!

---

<div class="post-metadata">

**Author:** ![lucastex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucastex/32/30619_2.png) [@lucastex](https://discuss.elastic.co/u/lucastex)\
**Post date:** [May 10, 2018, 2:41pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/4 "2018-05-10T14:41:04Z")

</div>

Hi @Christian_Dahlqvist, any thoughts on this?

Thank you,

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 10, 2018, 2:43pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/5 "2018-05-10T14:43:01Z")

</div>

As far as I know that should work.

---

<div class="post-metadata">

**Author:** ![lucastex](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucastex/32/30619_2.png) [@lucastex](https://discuss.elastic.co/u/lucastex)\
**Post date:** [May 10, 2018, 5:50pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/6 "2018-05-10T17:50:45Z")

</div>

Thanks a lot! 🙂

Will continue on this.

[]s,

Lucas

---

<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:** [June 7, 2018, 5:50pm UTC](https://discuss.elastic.co/t/force-query-on-field/130051/7 "2018-06-07T17:50:54Z")

</div>

Closed on June 7, 2018, 5:50pm UTC

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