incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesse Hallett <halle...@gmail.com>
Subject Re: Help with complex key range query and map/reduce
Date Mon, 28 Sep 2009 16:31:17 GMT
It is entirely possible to do what you want.  The reduce function required
is reasonably complicated.

First I want to point out that in the example query you gave the results
will not be filtered by engine.  If you want results matching only a
specific category_id and engine you will have to move `engine` to the second
or first position of the key.  If you want to get all engine values for a
given date range then the last position in the key is appropriate.
Otherwise the map function is looking good.

Here is a reduce function that should work:

    function (keys, values, rereduce) {
        var hist = {};
        if (rereduce) {
            values.forEach(function(v) {
                // v is the intermediate output of a previous reduce call.
                for (var k in v) {
                    hist[k] = (hist[k] || 0) + v[k];
                 }
            }
        } else {
            // v is a domain.
            values.forEach(function(v) {
                hist[v] = (hist[v] || 0) + 1;
            }
        }
        return hist;
    }

Make sure to run your queries with `group=true`.  Futon uses `group=false`
in the interactive view editor - so results may not look correct there.

The 'Introduction to Views' wiki page is very helpful to me for getting a
good idea of how reduce functions work:

    http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views

On Sep 28, 2009 7:21 AM, "Jeremy Wall" <jwall@google.com> wrote:

Typing from my phone so I can't give a code example. But from what you are
describing I think you want a key that looks something like this.
[CategoryId, Date, domain]. You can then use reduce to return counts by
category, date, and domain. If you want counts that span categories and
dates though then you will have to merge them in your application code.

Sent from my G1 google phone

On Sep 28, 2009 12:42 AM, "Glenn Rempe" <glenn@rempe.us> wrote: Hello, I am
hoping the group can ...

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message