incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: Sorting items by number of votes
Date Thu, 05 Nov 2009 17:33:30 GMT
On Thu, Nov 5, 2009 at 6:36 AM, Devon Weller <dweller@devonweller.com> wrote:
>
> Thanks Nathan.
>
> Using a list is an interesting idea.  Although, I suspect that method would
> become inefficient for things like "give me the 10 resources with the most
> votes" when there are 10,000 resources in the database.
>
> I think my solution will be to create a map reduce which just counts the
> votes by resource_id.  And then use that information to do a bulk request
> for the top 10 documents by ID.
>
>

Yes. In order to do this you'll need to use a group reduce, to see,
for each resource in the db, the # of votes it has. Sorting by the #
of votes will have to come in the client.

If you have too many unique resources to sort in the client, you can
copy the view to a secondary db, (by looping over the rows and saving
docs, from your application code) and sort by value there.


>
>
> Regarding the example in the wiki:
>
> As a new user, I just followed an example from the wiki:
> http://wiki.apache.org/couchdb/View_Snippets#aggregate_sum
>
> Is that example an incorrect way of using a CouchDB view?  Should it be
> removed?
>
>
> - Devon
>
>
>
>
>
>
> On Nov 5, 2009, at 8:04 AM, Nathan Stott wrote:
>
>> Reduces are not for joins.  A lot of people try that when they first start
>> using couch.  I tried it.
>>
>> This is more appropriate for a list.  If you feed your view into a list,
>> then you can have the list do the processing that you want and create the
>> 'joined' objects and emit them as JSON (or however else you like)
>>
>>
>> On Thu, Nov 5, 2009 at 7:46 AM, Devon Weller
>> <dweller@devonweller.com>wrote:
>>
>>> Hi.  I'm new to CouchDB (and this list) and I am looking for some help.
>>>  I
>>> am trying to do something very similar to
>>> http://wiki.apache.org/couchdb/View_Snippets#aggregate_sum.
>>>
>>>
>>>
>>> I have a database with 2 types of documents, resources and votes.  The
>>> documents in the database look something like this:
>>>
>>> resource 1 {_id: 1, type:"resource", name:"resource 1", ...}
>>> resource 2 {_id: 2, type:"resource", name:"resource 2", ...}
>>> resource 3 {_id: 3, type:"resource", name:"resource 3", ...}
>>>
>>> vote for resource 3 {type:"vote", resource_id: 3, user_id: foo}
>>> vote for resource 2 {type:"vote", resource_id: 2, user_id: foo}
>>> vote for resource 3 {type:"vote", resource_id: 3, user_id: bar}
>>>
>>>
>>> I want to create a view that will give me the resources in order of the
>>> most votes along with the number of votes.  In other words, I want to
>>> write
>>> a view that gives me something like the following results:
>>>
>>> {id:3, key:3, value:{doc:(document for resource 3), score: 2}},
>>> {id:2, key:2, value:{doc:(document for resource 2), score: 1}},
>>> {id:1, key:1, value:{doc:(document for resource 1), score: 0}},
>>>
>>>
>>>
>>>
>>> Here is my map function:
>>>
>>>
>>> function(doc) {
>>>      if (doc.type == 'vote' && doc.resource_id) {
>>>              emit(doc.resource_id, doc);
>>>      } else if (doc.type == 'resource' && doc.name) {
>>>              emit(doc._id, doc);
>>>      }
>>> }
>>>
>>>
>>> Here is my reduce function:
>>>
>>>
>>>
>>> function(keys, values, rereduce) {
>>>      var score = 0;
>>>      var output_doc = {};
>>>
>>>      for (var i=0; i < values.length; i++) {
>>>              if (values[i].type == 'vote') {
>>>                      ++score;
>>>              } else if (values[i].type == 'resource') {
>>>                      output_doc = values[i];
>>>              }
>>>      }
>>>
>>>      return {doc:output_doc, score:score};
>>> }
>>>
>>>
>>>
>>> When I query this view in Futon I get the following error:
>>>
>>> "Reduce output must shrink more rapidly"
>>>
>>>
>>> Am I trying to do something that can't be done with CouchDB?  Or am I
>>> just
>>> missing something?
>>>
>>> Thanks so much for any help.
>>>
>>> - Devon
>>>
>>>
>
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message