how to select multi keys with pagination


I have a simple social platform where users can create channels, within these channels they can then make multiple posts. Other users then follow these channels and ultimately see the posts made within them. As a user I have a screen where I can see all the posts from all the channels I am following, the posts being ordered either by an internal "score" attribute or by the creation date of the post. Since the total number of posts returned when I follow several channels can be very large I want to implement pagination.

hence the post Doc structure is something like:
type: "post",
userId: "user::123",
channelId: "channel::1234",
score: 500,
creationDate: "2014-01-19 16:45:16"

I have another doc for following channels that just ties the following userId to the channelId being followed, thus I have one map/reduce to find the set of channelIds the user is following. Then I want to call a view to obtain the posts of the channels being followed, ordered by either score or date.

To just get all posts (irrespective of channels) ordered by score then I can simply use the following view:

function (doc, meta) {
if (doc.type && doc.type == "post") {
emit([doc.postScore,], null);
I can then user startKey & endKey with offset and limit to do pagination, nice and simple.

However now when I want to restrict this to a set of channelIds that I am following within the view how do I do this? First thought was to emit([doc.postScore, doc.channelId,], null) but couchbase is a simple string match on keys so doc.channelId will match any channels between the start and end key so I will pick up channels that the user is not following.

Of course I can simply select all docs and then within the php code handle the ordering, so I would change the emit to emit(doc.channelId, doc.score) and pass in keys as an array of channelIds, but this doesn't seem optimal when I have to a lot of documents.

I've also read some posts that recommend using elasticsearch and using that to find the right docs but this seems overkill for what would be a very simple SQL query.

If I know the channelIds of the channels the user is following how do I select the posts from those channels ordering by score or by creationDate directly from within the map/reduce function in couchbase?


0 Answers

No answers yet