You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Cesar Delgado <be...@ymail.com> on 2010/05/01 01:44:13 UTC

Question on selecting on reduce values

I've had a map/reduce view that will tell me how many of a certain name there are in the Database.  It's very simple:

Map:
function(doc) {
fileVer = doc._id + "?rev=" + doc._rev emit(doc.filename, 1);
}

Reduce:
function(key, values) { return sum(values) ;
}

Now what I want to do is be able to select by the count that is returned to me.  Basicaly I want to be able to ask the database to show me all the doc.filename where sum(values) > arbitrary_number.

I've tried hard-coding it:
function(key, values) {
if (sum(values) > 2) { return sum(values) ;
}
}

But that gives me an error.

I'd be perfectly happy hardoding the value if needed but would love to have it be a variable.

What subtlety am I missing here? 

Thanks all,

-Cesar

Re: Question on selecting on reduce values

Posted by Jarrod Roberson <ja...@vertigrated.com>.
On Sat, May 1, 2010 at 3:04 PM, Cesar Delgado <be...@ymail.com> wrote:

> That's awesome Randall, thanks.  I have to play around with it a little
> more but at least I got something useful now.
>
> I just created a list that looks like this:
>
> function(head, req) {
>    var row ;
>    while (row = getRow()) {
>        if (row.value > 1){
>      send(row.key + "\n") ;
>    }
>  }
> }
>
> True that what I'm getting returned is just the name of the duplicated
> entries, but it's a good start.
>
> Cheers!
>
> -Cesar
>
> here is an example of a list function that does something like what you are
trying to do
http://www.vertigrated.com/blog/2010/04/generic-ad-hoc-queries-in-couchdb/

-- 
Jarrod Roberson
678.551.2852

Re: Question on selecting on reduce values

Posted by Cesar Delgado <be...@ymail.com>.
That's awesome Randall, thanks.  I have to play around with it a little more but at least I got something useful now.

I just created a list that looks like this:

function(head, req) {
    var row ;
    while (row = getRow()) {
        if (row.value > 1){      
      send(row.key + "\n") ;    
    }  
  }
}

True that what I'm getting returned is just the name of the duplicated entries, but it's a good start.

Cheers!

-Cesar



________________________________
From: Randall Leeds <ra...@gmail.com>
To: user@couchdb.apache.org
Sent: Fri, April 30, 2010 5:52:48 PM
Subject: Re: Question on selecting on reduce values

Reading all the way through I'm convinced it should work as I explained.
Pass an extra query parameter not recognized by the view server and inspect
it in the list function.

Voila!

On Apr 30, 2010 5:49 PM, "Randall Leeds" <ra...@gmail.com> wrote:

I believe you could accomplish what you want with a list function:

http://books.couchdb.org/relax/design-documents/lists

I've never used one myself, but my impression is that you could simply not
send() any data for rows that have less than some number for the value.

Of course, I don't know if you can do group reduce with a list function. Can
any devs chime in on whether couch still obeys query strings and allows a
group reduce to be sent to a list?

Ideally you could send in your variable threshold as an additional query
parameter, available to the list function in the request object.

Something to play with and if it can't be made to work now my gut says it
should be possible and there's a patch to be made.

Again, disclaimer: never used a list function and answering from my phone.

-Randall

Sent from my mind control space transport super-dome.


>
> On Apr 30, 2010 4:44 PM, "Cesar Delgado" <be...@ymail.com> wrote:
>
> I've had a map/reduce ...

Re: Question on selecting on reduce values

Posted by Randall Leeds <ra...@gmail.com>.
Reading all the way through I'm convinced it should work as I explained.
Pass an extra query parameter not recognized by the view server and inspect
it in the list function.

Voila!

On Apr 30, 2010 5:49 PM, "Randall Leeds" <ra...@gmail.com> wrote:

I believe you could accomplish what you want with a list function:

http://books.couchdb.org/relax/design-documents/lists

I've never used one myself, but my impression is that you could simply not
send() any data for rows that have less than some number for the value.

Of course, I don't know if you can do group reduce with a list function. Can
any devs chime in on whether couch still obeys query strings and allows a
group reduce to be sent to a list?

Ideally you could send in your variable threshold as an additional query
parameter, available to the list function in the request object.

Something to play with and if it can't be made to work now my gut says it
should be possible and there's a patch to be made.

Again, disclaimer: never used a list function and answering from my phone.

-Randall

Sent from my mind control space transport super-dome.


>
> On Apr 30, 2010 4:44 PM, "Cesar Delgado" <be...@ymail.com> wrote:
>
> I've had a map/reduce ...

Re: Question on selecting on reduce values

Posted by Randall Leeds <ra...@gmail.com>.
I believe you could accomplish what you want with a list function:

http://books.couchdb.org/relax/design-documents/lists

I've never used one myself, but my impression is that you could simply not
send() any data for rows that have less than some number for the value.

Of course, I don't know if you can do group reduce with a list function. Can
any devs chime in on whether couch still obeys query strings and allows a
group reduce to be sent to a list?

Ideally you could send in your variable threshold as an additional query
parameter, available to the list function in the request object.

Something to play with and if it can't be made to work now my gut says it
should be possible and there's a patch to be made.

Again, disclaimer: never used a list function and answering from my phone.

-Randall

Sent from my mind control space transport super-dome.

On Apr 30, 2010 4:44 PM, "Cesar Delgado" <be...@ymail.com> wrote:

I've had a map/reduce view that will tell me how many of a certain name
there are in the Database.  It's very simple:

Map:
function(doc) {
fileVer = doc._id + "?rev=" + doc._rev emit(doc.filename, 1);
}

Reduce:
function(key, values) { return sum(values) ;
}

Now what I want to do is be able to select by the count that is returned to
me.  Basicaly I want to be able to ask the database to show me all the
doc.filename where sum(values) > arbitrary_number.

I've tried hard-coding it:
function(key, values) {
if (sum(values) > 2) { return sum(values) ;
}
}

But that gives me an error.

I'd be perfectly happy hardoding the value if needed but would love to have
it be a variable.

What subtlety am I missing here?

Thanks all,

-Cesar