You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Rajkumar S <ra...@gmail.com> on 2008/12/17 16:43:20 UTC

Arguments for Reduce function

Hi,

I have a (simplified) reduce function like


function(key,values){
    var a,b;
        for (var i=0; i< values.length; ++i) {
            if (values[i] < 6){
                a++;
            } else {
                b++;
            }
        }
    }
    return [values.length,a,b];
}

The value 6 here is hard coded, is it possible to pass this via some
sort of arguments? Also how can I retrieve all results of a view ?

Thanks and regards,

raj

Re: Arguments for Reduce function

Posted by Paul Carey <pa...@gmail.com>.
> No, actually, I am trying to pass an arbitrary argument so that I get
> the count of elements < that argument and > that argument. kind of
> like select count(*) from foo where bar<6;

I don't think I misunderstood. I've posted an example where you can do just this
http://friendpaste.com/6VWDrZU07ZXK8iwyv7XtNp

>> If you wanted to see all the docs, simply query with reduce=false
>
> But I want to see all the results after reduce function has run. Right
> now I am using a command like
>
> http://localhost:5984/users/_view/test1/test1?count=50&group=true
>
> I would like to do some thing like
>
> http://localhost:5984/users/_view/test1/test1?count=all&group=true

But maybe I have, as I don't understand what you're asking here.

Paul

Re: Arguments for Reduce function

Posted by Rajkumar S <ra...@gmail.com>.
Hi,

Thanks a lot for the reply!

On Wed, Dec 17, 2008 at 9:36 PM, Paul Carey <pa...@gmail.com> wrote:
> At a guess you're trying to get the doc count based on a particular
> characteristic of the doc.

No, actually, I am trying to pass an arbitrary argument so that I get
the count of elements < that argument and > that argument. kind of
like select count(*) from foo where bar<6;

> If you wanted to see all the docs, simply query with reduce=false

But I want to see all the results after reduce function has run. Right
now I am using a command like

http://localhost:5984/users/_view/test1/test1?count=50&group=true

I would like to do some thing like

http://localhost:5984/users/_view/test1/test1?count=all&group=true

> Also your reduce function probably wouldn't behave as you'd expect
> when invoked as a rereduce - at some point the values param would be
> [values.length, a, b] array returned from a previous invocation.

Actually I have stripped out the rereduce part when I posted the
example, to focus on the problem at hand and simplify the example. I
am taking care of rereduce.

with regards,

raj

Re: Arguments for Reduce function

Posted by Paul Carey <pa...@gmail.com>.
> /my_db/_view/app/by_i?startkey=null&endkey=pivot
> /my_db/_view/app/by_i?startkey=pivot&endkey={}

D'oh. Probably obvious, but if you don't want both queries to be
inclusive of the pivot, you'd need to decrement or increment the pivot
in one query or the other.

Re: Arguments for Reduce function

Posted by Paul Carey <pa...@gmail.com>.
Hi Raj

At a guess you're trying to get the doc count based on a particular
characteristic of the doc.

You might want to try something like

function (doc) {
  emit(doc.i, doc);
}

function (keys, vals) {
 return sum(vals);
}

and then query with something like

/my_db/_view/app/by_i?startkey=null&endkey=pivot
/my_db/_view/app/by_i?startkey=pivot&endkey={}

varying pivot as you wish.

If you wanted to see all the docs, simply query with reduce=false

Also your reduce function probably wouldn't behave as you'd expect
when invoked as a rereduce - at some point the values param would be
[values.length, a, b] array returned from a previous invocation.

Hope this helps

Paul

On Wed, Dec 17, 2008 at 3:43 PM, Rajkumar S <ra...@gmail.com> wrote:
> Hi,
>
> I have a (simplified) reduce function like
>
>
> function(key,values){
>    var a,b;
>        for (var i=0; i< values.length; ++i) {
>            if (values[i] < 6){
>                a++;
>            } else {
>                b++;
>            }
>        }
>    }
>    return [values.length,a,b];
> }
>
> The value 6 here is hard coded, is it possible to pass this via some
> sort of arguments? Also how can I retrieve all results of a view ?
>
> Thanks and regards,
>
> raj
>

Re: Arguments for Reduce function

Posted by Paul Davis <pa...@gmail.com>.
Raj,

On Wed, Dec 17, 2008 at 10:43 AM, Rajkumar S <ra...@gmail.com> wrote:
> Hi,
>
> I have a (simplified) reduce function like
>
>
> function(key,values){
>    var a,b;
>        for (var i=0; i< values.length; ++i) {
>            if (values[i] < 6){
>                a++;
>            } else {
>                b++;
>            }
>        }
>    }
>    return [values.length,a,b];
> }
>
> The value 6 here is hard coded, is it possible to pass this via some
> sort of arguments?

Unfortunately, no. And this isn't even a feature that *could* be
implemented. In order to provide the ability for Map/Reduce views to
be computed incrementally, it must be guranteed that given identical
input, the output must also be identical. The reduce also needs to
account for consuming it's own output and produce exactly the same
output for a given input.

Also how can I retrieve all results of a view ?

If you mean access a map in a view that also specifies a reduce
function, there's a ?reduce=false query string parameter that will do
that.

>
> Thanks and regards,
>
> raj
>

HTH,
Paul Davis