incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: getting unique set of document id's
Date Tue, 07 Jul 2009 09:05:24 GMT
On Sun, Jul 05, 2009 at 03:18:33PM -0500, Ross Bates wrote:
> Hi Paul - thank you for the pointers. Something I'm unclear on though...
> using a sum in the reduce returns something like this for all the tags:
> 
> foo, 3
> bar, 5
> baz, 7
> 
> When I use the multi-key fetch against the view it doesn't return specific
> docid's for each tag, just a subset of tags and their counts.
> 
> POST {"keys": ["foo", "bar"]}
> 
> foo, 3
> bar, 5
> 
> How can I get access to the list of docid's which make up the total?

You query the view again with reduce=false, which turns it back into a
regular map view.

The idea is you'd query in this case only for key "foo", because this
returns the smallest number of documents, and then filter the result set
client-side to documents which also include key "bar".

You can read the view using include_docs=true to get the whole docs, or in
your view you can emit the parts of doc you're interested in as the value.

Your original code was:

function(doc) {
  for(i in doc.tags) {
      emit(doc.tags[i], doc._id);
    }
}

Note that emitting doc._id is not useful (every K/V pair also emits the id
as well), so you could have

// Query with include_docs=true
function(doc) {
  for(i in doc.tags) {
      emit(doc.tags[i], null);
    }
}

Or:

// Index is bigger but faster to read
function(doc) {
  for(i in doc.tags) {
      emit(doc.tags[i], doc);
    }
}

Or:

// Just emit the pieces you need when processing the view
function(doc) {
  for(i in doc.tags) {
      emit(doc.tags[i], {tags: doc.tags});
    }
}

Mime
View raw message