# Restrict Concurrent Users getting same results for same search creiteria

**URL:** <https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373>\
**Category:** Elasticsearch\
**Created:** [September 14, 2022, 6:33am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373 "2022-09-14T06:33:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anish\_a\_nair](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anish_a_nair/32/110842_2.png) [@anish\_a\_nair](https://discuss.elastic.co/u/anish_a_nair)\
**Post date:** [September 14, 2022, 6:33am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/1 "2022-09-14T06:33:37Z")

</div>

Hi,

I have requirement to restrict the concurrent users viewing same results if they use same search criteria. The example of my use case is listed below

let's assume i have below numbers in a field called "Numbers"

8474884399  
7747748499  
8477488899  
3737747399  
4747747799  
3663663699  
1282883899  
8484884899  
8474848899  
3737737799

If the limit is set to five and user 1 attempts to find all numbers that end in "99," the output seen below will be produced.

8474884399  
7747748499  
8477488899  
3737747399  
4747747799

If the limit is set to 5, the same output will appear if user 2 attempts to search all numbers that end in "99."

My requirement is, The first five records must be hidden from User 2 because they have already been given to User 1, and I must grant him access to the last five records, which are mentioned below

3663663699  
1282883899  
8484884899  
8474848899  
3737737799

Is it possible to achieve this in Elasticsearch ? if yes can someone help me to achieve the same ?

---

<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:** [September 14, 2022, 7:33am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/2 "2022-09-14T07:33:06Z")

</div>

There is as far as i know nothing in Elasticsearch that allows you to do that, so I suspect you will need to handle this in your application code.

---

<div class="post-metadata">

**Author:** ![anish\_a\_nair](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anish_a_nair/32/110842_2.png) [@anish\_a\_nair](https://discuss.elastic.co/u/anish_a_nair)\
**Post date:** [September 14, 2022, 10:37am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/3 "2022-09-14T10:37:21Z")

</div>

Hi @Christian_Dahlqvist

I appreciate your reply. I am using hibernate search for this implementation from Elasticsearch.

As a result, I made an attempt at a fix by adding a new field named "lock state" and using that state value in addition to the primary search criteria. As a result, once a response is given to a user, I immediately update the "lock state" column in the database, which is then automatically synced with the elastic cluster. So that the second user won't receive the same response.

The issue with this strategy is that hibernation search requires a minimum of 1 second to complete this auto-syncing to the elastic cluster; however, if there was a way for me to complete this immediately without degrading performance, the issue would be resolved.

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [September 14, 2022, 11:07am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/4 "2022-09-14T11:07:37Z")

</div>

There is a slightly different way of handling this. Instead of having your "lock state" update Elasticsearch, you could have the lock state be an additional "NOT" filter on your Elasticsearch query.

In your example use case.

Assuming the default "lock state" is empty. User 1, would run their search to find the first 5 numbers that end in "99", resulting in:

8474884399  
7747748499  
8477488899  
3737747399  
4747747799

Your app could then update the "lock state". User 2 could then run their same query, but this time the query that gets run, includes the values in the "lock state" as a NOT clause in the Elasticsearch query. This should in theory result in User 2 getting the results:

3663663699  
1282883899  
8484884899  
8474848899  
3737737799

And this should effectively have no real "performance issues", as this is mainly just an adjustment to the query, you're running rather than the actual underlying data.

---

<div class="post-metadata">

**Author:** ![anish\_a\_nair](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anish_a_nair/32/110842_2.png) [@anish\_a\_nair](https://discuss.elastic.co/u/anish_a_nair)\
**Post date:** [September 14, 2022, 11:58am UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/5 "2022-09-14T11:58:41Z")

</div>

Hi @BenB196

Ok. Even with this approach , there is a delay to update to Elasticsearch about "lock state" value to use in NOT query. Due to this delay the user 2 will have the chance to get the same response given to user 1. Is there a way to avoid this 1 second delay ?

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [September 14, 2022, 12:18pm UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/6 "2022-09-14T12:18:56Z")

</div>

Hmm, I'm not sure how you're implementing your "lock state", but in theory, if you're app is a single deployment, you can just track the "lock state" in the app itself, and thus really not have a delay. If the app is distributed, I don't think there is any real "solution" here. No, matter the approach, there will always be a chance that there will be some time gap between the "lock state" being updated and users querying the data. But, you should also really consider the use case. Will a 1 second gap really matter? Will your app have usage that will consistently see multiple users running similar queries within 1 second of each other? Will it really matter if occasionally users see similar results? The issue seems more like an application implementation issue at this point (I'm not really aware of any sort of database that offers this functionality out of the box).

---

<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:** [October 12, 2022, 12:19pm UTC](https://discuss.elastic.co/t/restrict-concurrent-users-getting-same-results-for-same-search-creiteria/314373/7 "2022-10-12T12:19:42Z")

</div>

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