John Smith Optional
John Smith Optional

Reputation: 24816

What is in the reduce function arguments in CouchDB?

I understand that the reduce function is supposed to somewhat combine the results of the map function but what exactly is passed to the reduce function?

function(keys, values){
  // what's in keys?
  // what's in values?
}

I tried to explore this in the Futon temporary view builder but all I got were reduce_overflow_errors. So I can't even print the keys or values arguments to try to understand what they look like.

Thanks for your help.

Edit:

My problem is the following. I'm using the temporary view builder of Futon.

I have a set of document representing text files (it's for a script I want to use to make translation of documents easier).

text_file:
    id   // the id of the text file is its path on the file system

I also have some documents that represent text fragments appearing in the said files, and their position in each file.

text_fragment:
    id
    file_id   // correspond to a text_file document 
    position

I'd like to get for each text_file, a list of the text fragments that appear in the said file.

Upvotes: 1

Views: 255

Answers (1)

dignifiedquire
dignifiedquire

Reputation: 1370

Update

Note on JavaScript API change: Prior to Tue, 20 May 2008 (Subversion revision r658405) the function to emit a row to the map index, was named "map". It has now been changed to "emit".

That's the reason why there is mapused instead of emitit was renamed. Sorry I corrected my code to be valid in the recent version of CouchDB.

Edit

I think what you are looking for is a has-many relationship or a join in sql db language. Here is a blog article by Christopher Lenz that describes exactly what your options are for this kind of scenario in CouchDB.

In the last part there is a technique described that you can use for the list you want. You need a map function of the following format

function(doc) {
  if (doc.type == "text_file") {
    emit([doc._id, 0], doc);
  } else if (doc.type == "text_fragment") {
    emit([doc.file_id, 1], doc);
  }
}

Now you can query the view in the following way:

my_view?startkey=["text_file_id"]&endkey;=["text_file_id", 2]

This gives you a list of the form

  • text_file
  • text_fragement_1
  • text_fragement_2
  • ..

Old Answer

Directly from the CouchDB Wiki

function (key, values, rereduce) {
    return sum(values);
}

Reduce functions are passed three arguments in the order key, values and rereduce

Reduce functions must handle two cases:

  1. When rereduce is false:

    • key will be an array whose elements are arrays of the form [key,id], where key is a key emitted by the map function and id is that of the document from which the key was generated.
    • values will be an array of the values emitted for the respective elements in keys i.e. reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)
  2. When rereduce is true:

    • key will be null
    • values will be an array of values returned by previous calls to the reduce function i.e. reduce(null, [intermediate1,intermediate2,intermediate3], true) Reduce functions should return a single value, suitable for both the value field of the final view and as a member of the values array passed to the reduce function.

Upvotes: 4

Related Questions