I am working on a social networking project with a post system.
Here is my current post table
CREATE TABLE afterstr.posts (
user_id text,
date timeuuid,
content text,
PRIMARY KEY (user_id, date)
) WITH CLUSTERING ORDER BY (date DESC)
Here is the user follow table in which I retrieve a list of the user's subscriptions. It is with this list that I will retrieve the last post of the user's subscriptions.
CREATE TABLE afterstr.follows (
from_id text,
to_id text,
followed_at timestamp,
PRIMARY KEY (from_id, to_id)
)
there is also a users_infos table which contains all the information about the user but which we do not need here.
My problem is that users can make multiple posts.
example of the table posts with a
SELECT * FROM posts;
920022 | a5cf4950-d46a-11ec-a4e5-19251c2b96e1 (19/03/2022) | Thank you for the new sub !!!
920022 | a5cf4950-d46a-11ec-a4e5-190382029828 (16/03/2022) | meeting tomorow
920022 | a5cf4950-d46a-11ec-a4e5-19251c2b96e1 (28/02/2022) | I will orgenise a meeting during the next moth
234235440 | a5cf4950-d46a-11ec-a4e5-190382029828 (23/03/2022) | Hey !!!
234235440 | a5cf4950-d46a-11ec-a4e5-190382029828 (21/03/2022) | See you later
992777 | a5cf4950-d46a-11ec-a4e5-202093838892 (22/03/2022) | Hey
In the context of loading a feed by date of publication, I would like to retrieve the posts of two people with whom the user is subscribed to in our case the user '234235440' and the user '992777'. And retrieve them according to the publication date of their post with a limit of 3.
Here is the select command:
SELECT * FROM posts where user_id in ('234235440', '992777') limit 3;
but cassandra does not send me the last 3 posts of all users but only the last 2 of the first id in the "IN" without sorting by date.
Cassandra responce (the numbers 1, 2 and 3 are the contents and represent the order of sending):
234235440 | a5cf4950-d46a-11ec-a4e5-19251c2b96e1 (23/03/2022) | Hey every one
234235440 | a5cf4950-d46a-11ec-a4e5-190382029828 (21/03/2022) | See you later
992777 | a5cf4950-d46a-11ec-a4e5-202093838892 (22/03/2022) | Hey
Here is the expected answer
234235440 | a5cf4950-d46a-11ec-a4e5-19251c2b96e1 (23/03/2022) | Hey every one
992777 | a5cf4950-d46a-11ec-a4e5-202093838892 (22/03/2022) | Hey
234235440 | a5cf4950-d46a-11ec-a4e5-190382029828 (21/03/2022) | See you later
My aim is to make a feed based on a user's subscription. So the goal is to get the last 3 posts from the list of people he is subscribed to. My problem is that for the moment Cassandra returns me only the 3 last post of only one user (tier by date) without taking into account the dates of publication of posts of other person to who he is subscribed which are more recent.
Do you have any idea how to solve the problem by avoiding ORDER BY which is not recommended for this type of request ? Do you think that the use of a search engine could be an optimised solution?