# Best design for managing 'likes' on each comment?

**URL:** https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607
**Category:** Elasticsearch
**Created:** [March 18, 2021, 7:17am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607 "2021-03-18T07:17:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [March 18, 2021, 7:17am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/1 "2021-03-18T07:17:43Z")

</div>

Hi, I'm new to Elasticsearch and struggling with the design.

I have 2 indices. One is ' **comment**' index which contains comments users wrote, the other is ' **ex\_comment**' which also contains comments but crawling data.

What I want to make is this.  
Like ' **Facebook like**', I want to show how many users like of each comment.  
Since the user should know if the user hit like on each comment or not,  
Just adding 'counter' field is not enough, and I thought there should be the mapping tables as below.

(index)  
like\_comment, like\_ex\_comment

(fields - both are same)  
"comment\_id" // id from parent index  
"user\_id" // user who hits like on the comment

These two indices will be childs of each parent indices.

I've heard that Elasticsearch can't have multiple parents. So I thought I need 2 indices respectively.

And I'll be using 'has-child' query if I decide to use this design.

I'm not sure if this would be the best design for managing 'likes'. I'm also worried about the performance since 'hit like' will be frequently executed.

Thank you.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 18, 2021, 9:43am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/2 "2021-03-18T09:43:20Z")

</div>

Hi Jenny,  
Much depends on the types of query you want to do.  
If you only need to know if a particular user has liked a particular comment (e.g. to colour a comment's heart icon accordingly when rendering a web page) then you only need a regular index with comment\_id and user\_id and query that alone.  
If you want to get fancy and query properties of both comments and users in the same request (e.g. find users from London who liked comments about cheese) then you'll need something more either by denormalising data or using parent/child routing of related data to the same machines.

Best to consider the questions you want to ask of the data up-front

---

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [March 18, 2021, 10:08am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/3 "2021-03-18T10:08:44Z")

</div>

Hi Mark, thanks for the quick reply.

I want to show a list of comment, and each of it has the number of how many users have liked as below.

❤ 999 like this comment (empty heart)  
❤ 999 like this comment (full heart if the user has liked this comment)

For now, I only need to get how many likes on every comment and whether the user has liked it or not.

So is it okay to make just one index (comment\_id, user\_id) and calculate sum on every single comment?

Since one page has 10 rows(comments) I think calculating sum from the newly created index not so costly.

Thank you.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 18, 2021, 10:32am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/4 "2021-03-18T10:32:37Z")

</div>

> [@Jenny1](#):
>
> Since one page has 10 rows(comments) I think calculating sum from the newly created index not so costly.

True. I put an example in [this gist](https://gist.github.com/markharwood/de777ac67c7690baa70aaeb3331aff58)

---

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [March 18, 2021, 3:27pm UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/5 "2021-03-18T15:27:08Z")

</div>

I really appreciate that you wrote an example script which covers cases I've mentioned perfectly. I got a lot out of it.  
Thank you 🙂

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 18, 2021, 3:31pm UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/6 "2021-03-18T15:31:58Z")

</div>

No problem. Glad it was useful.  
Some times it's the simple things that are hard. Makes me marvel at how things like Twitter scale, counting likes for all tweets and remembering which tweets we liked, going back years.

---

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [March 18, 2021, 3:48pm UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/7 "2021-03-18T15:48:13Z")

</div>

So true. Knowing and understanding the architecture of high scale system is marvelous. Anyways, thank you again. And hope you stay safe and well.

---

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [March 26, 2021, 1:44pm UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/8 "2021-03-26T13:44:06Z")

</div>

Hi, as you suggested a design for the 'like comments', I could make it perfectly as I expected.

But recently I've got stuck on another problem, and It's hard for me to figure out.  
User comments also need to be shown in descending order by user likes.

As you suggested earlier, I made 'likes\_index' to save the user likes.

I queried 'comment\_index/\_search', 'likes\_index/\_search' respectively, and then I merged the results to show the result as below. (fetch 10 rows per page)

Comment1  
OO liked this comment

Comment2  
OO liked this comment  
..

But how can I get the result in descending order? It's quite easy when I use RDB, but it's hard for me to think..

I'm not sure if I should think of another design or it can be solved by the current design.  
When I googled it, I've seen about 'denormalization', but I'm unsure that I need this here.

If you don't mind, could you give me some ideas that I can solve this problem?

Thank you.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 31, 2021, 9:53am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/9 "2021-03-31T09:53:31Z")

</div>

> [@Jenny1](#):
>
> User comments also need to be shown in descending order by user likes.

That should happen naturally in the example I gave. Terms are sorted by doc\_count descending so the more "liked" docs for a comment in the likes index the higher it appears in the array of top comment terms in the results. I amended my gist with extra like docs to show this effect.  
It's also possible to sort aggregation terms by other things like max date (to get the latest first) but the default sort order of number of docs should be working in your favour here.

---

<div class="post-metadata">

### Author: ![Jenny1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenny1/32/85732_2.png) [@Jenny1](https://discuss.elastic.co/u/Jenny1)
#### Post date: [April 1, 2021, 6:59am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/10 "2021-04-01T06:59:46Z")

</div>

As far as I understand, using sort aggregation terms can be possible only with the fields of 'likes\_index'. Is it right?  
Because I've heard that I cannot do join with elasticsearch. And what I might need is to sort index with fields from comment index.

For now, I only need to search for on 'title' of comments.  
ex) user search 'love' --\> fetch comment data, in descending order by user likes.

So I think just adding a 'title' field to likes\_index could be the possible answer in order to search for comments which include specific keywords.  
I guess this is the best way to think of, but please, tell me if there are better options.  
And in the near future, I might need more functions, such as elasticsearch query score.(on comment\_index)

---

<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, 2021, 7:00am UTC](https://discuss.elastic.co/t/best-design-for-managing-likes-on-each-comment/267607/11 "2021-04-29T07:00:23Z")

</div>

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