# Searching for documents only if user has it

**URL:** <https://discuss.elastic.co/t/searching-for-documents-only-if-user-has-it/4073>\
**Category:** Elasticsearch\
**Created:** [March 10, 2011, 9:30am UTC](https://discuss.elastic.co/t/searching-for-documents-only-if-user-has-it/4073 "2011-03-10T09:30:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Oh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeff_oh/32/3256_2.png) [@Jeff\_Oh](https://discuss.elastic.co/u/Jeff_Oh)\
**Post date:** [March 10, 2011, 9:30am UTC](https://discuss.elastic.co/t/searching-for-documents-only-if-user-has-it/4073/1 "2011-03-10T09:30:59Z")

</div>

I want to know how to achieve this scenario as efficient as possible.

In a following situation,

- There are many documents.
- Users can choose documents to have. (just like facebook's like feature)

I'd like to search only from documents that a certain user has chosen. Because this is N:M mapping, I stored document index and (document id - user id) in a separate index.

What is the best possible way to achieve this? I've been spending a lot of time thinking for a good solution, but didn't come up with a good one. Can anyone enlighten me?

Thanks in forward.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 12, 2011, 8:10am UTC](https://discuss.elastic.co/t/searching-for-documents-only-if-user-has-it/4073/2 "2011-03-12T08:10:33Z")

</div>

Since there is no join functionality in elasticsearch, then storing this relationship in a different index will not help. You can either reindex the doc with an array of not\_analyzed field that has the user ids on a document that represents users that liked it. This will require you to reindex the whole doc again.

Another option is to have a child document associated with the main doc that represents a like, and then use a has\_child filter to filter those out. This means that a like is just an index of a new doc that is a child doc to the "main" doc. Has child is a bit expensive, but will do the work.

I plan to try and tackle this "changing fields" problem in later versions. Its a big one to tackle though... 🙂  
On Thursday, March 10, 2011 at 11:30 AM, Jeff Oh wrote:

> I want to know how to achieve this scenario as efficient as possible.
> 
> In a following situation,
> 
> - There are many documents.
> - Users can choose documents to have. (just like facebook's like feature)
> 
> I'd like to search only from documents that a certain user has chosen.  
> Because this is N:M mapping, I stored document index and (document id - user  
> id) in a separate index.
> 
> What is the best possible way to achieve this? I've been spending a lot of  
> time thinking for a good solution, but didn't come up with a good one. Can  
> anyone enlighten me?
> 
> Thanks in forward.
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Searching-for-documents-only-if-user-has-it-tp2659739p2659739.html](http://elasticsearch-users.115913.n3.nabble.com/Searching-for-documents-only-if-user-has-it-tp2659739p2659739.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:10am UTC](https://discuss.elastic.co/t/searching-for-documents-only-if-user-has-it/4073/3 "2017-07-06T04:10:14Z")

</div>


