-
-
Save markharwood/de777ac67c7690baa70aaeb3331aff58 to your computer and use it in GitHub Desktop.
Likes index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE likes_index | |
PUT likes_index | |
{ | |
"mappings": { | |
"properties": { | |
"user_id": { | |
"type": "keyword" | |
}, | |
"comment_id": { | |
"type": "keyword" | |
} | |
} | |
} | |
} | |
# Note the doc id is a combination of user and comment ID. | |
# This is the equivalent of primary key and can be used for 2 things | |
# 1) Prevents a user liking a comment more than once | |
# 2) Allows efficient "un-liking" - simply delete using the id | |
POST likes_index/_doc/1_1 | |
{ | |
"user_id": 1, | |
"comment_id":1 | |
} | |
POST likes_index/_doc/2_2 | |
{ | |
"user_id": 2, | |
"comment_id":2 | |
} | |
POST likes_index/_doc/3_2 | |
{ | |
"user_id": 3, | |
"comment_id":2 | |
} | |
# Show comments 1, 2 and 3 with number of total likes and knowing which | |
# were specifically liked by user 1. Comments naturally sorted by number of like docs | |
# so comment 2 appears before comment 1. | |
GET likes_index/_search | |
{ | |
"query": { | |
"terms": { | |
"comment_id": [ | |
1, | |
2, | |
3 | |
] | |
} | |
}, | |
"size": 0, | |
"aggs": { | |
"likeCounts": { | |
"terms": { | |
"field": "comment_id", | |
"size": 10 | |
} | |
}, | |
"likedByCurrentUser": { | |
"filter": { | |
"term": { | |
"user_id": 1 | |
} | |
}, | |
"aggs": { | |
"likeCounts": { | |
"terms": { | |
"field": "comment_id", | |
"size": 10 | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment