Thanks for your answer. Unfortunately the post date can change, because a 
document can be updated or revert to a draft state and then published again.
Anyway I can still map with emit(post.publishingDate, NULL) all the posts, then 
I can get the posts of the last week, using startkey parameter. Finally I can 
query another view emit(hit.postId, NULL) using reduce = _count using as keys 
all the doc ids from the first query and stale=ok. I can sort the results and 
limits them to 20. In a week I will have maybe 300 posts, so I think should be 
fast enough.

-Filippo

On Jul 23, 2013, at 12:11 AM, Nathan Vander Wilt wrote:

> If your "hit" document includes the post date, you could map with 
> `emit([hit.post_date, hit.post_id])` and reduce with `_count`. Then you when 
> queried with `?group=exact` you would have total hits per post and you could 
> prune the results using startkey to last 7 days. You would still need to 
> order all the recent matches yourself before limiting to 20 posts however. 
> Assuming the number of posts in a week is not enormous it seems reasonable.
> 
> hth,
> -nvw
> 
> 
> On Jul 22, 2013, at 10:32 AM, Filippo Fadda wrote:
> 
>> OK, this answered to my question:
>> 
>> http://stackoverflow.com/questions/5571459/couchdb-view-query-with-multiple-key-values
>> 
>> It is never possible to query by criteria A and then sort by criteria B in 
>> CouchDB.
>> 
>> So I have to get posts in a data range, then sort them by number of hits 
>> myself.
>> 
>> -Filippo
>> 
>> 
>> 
>> On Jul 22, 2013, at 6:25 PM, Filippo Fadda wrote:
>> 
>>> Let's suppose I have a blog and I want display the popular posts, where 
>>> popular means: the 20 posts, of the last seven days, ordered by number of 
>>> hits.
>>> To avoid conflicts I don't save the number of hits in the post itself, but 
>>> I emit a new document every time a generic user displays a post.
>>> Even using view collation I don't get a solution for this problem.
>>> 
>>> -Filippo
>> 
> 

Reply via email to