You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Patrick Aljord <pa...@gmail.com> on 2008/08/25 06:59:42 UTC

how do I get the total number of rows returned for a given key?

Hey all,

I'm trying to get the number of docs returned by my view given a
certain key such as:

myview?key=foo&count=0

I thought total_rows would give the numbers of docs that have this
specific key but instead it gave me the total rows for all keys. I was
told by "jeffd" on IRC this is the normal behavior but, I quote:

"there was talk in this channel at some point of adding another piece,
but I dunno how far that went".

So my question is: are you going to add another piece like total_rows
but that would return the number of rows for a given key? Or is there
a better way to do so? (with reduce I guess, but still it would be
nice to have it included). This is really needed for pagination.
I spotted an issue for that https://issues.apache.org/jira/browse/COUCHDB-82


Thanks in advance,

Pat

Re: how do I get the total number of rows returned for a given key?

Posted by Patrick Aljord <pa...@gmail.com>.
Ok, thanks a lot for the great advice as usual Paul. I will try it out
and post my couchdb mapper online, it is based on couchrest  (the
mapper is actually written by a friend but we're both using it :).

Cheers,

Pat

Re: how do I get the total number of rows returned for a given key?

Posted by Paul Davis <pa...@gmail.com>.
Its gonna depend on the exact keys you're going to be paging through.
But for the base case where you only want to page over exact keys, you
would add in a reduce view with a function like:

function(keys,values,rereduce)
{
    return values.length ;
}

And then when you query the reduce for the row count for a given key,
make sure you use the group=true parameter.

If instead you're doing sub key matches, you'll have to run through
and calculate the row count for each type of query (read as: You have
to make a new reduce view).

Also remember that if your map functions are byte identical, then only
one btree is generated (which is good). Chris Anderson has an
extremely useful script couchview in his couchrest github repo for
managing views on disk and pushing them into a database. You should be
able to find them at [1].

Paul

[1] http://github.com/jchris/couchrest



On Mon, Aug 25, 2008 at 12:10 PM, Patrick Aljord <pa...@gmail.com> wrote:
> On Mon, Aug 25, 2008 at 10:58 AM, Paul Davis
> <pa...@gmail.com> wrote:
>>So while it may look like a pain to get the
>> counts for a specific subset of a view, it really is one of the better
>> solutions.
>
> ok thanks, could you give me any hint on how to write this reduce in a
> generic way for pagination? I'm writing a mapper for couchdb, is there
> a way to this in a general way so that it can be used for any view or
> is it too specific to each query/view?
>
> Thanks in advance,
>
> Pat
>

Re: how do I get the total number of rows returned for a given key?

Posted by Patrick Aljord <pa...@gmail.com>.
On Mon, Aug 25, 2008 at 10:58 AM, Paul Davis
<pa...@gmail.com> wrote:
>So while it may look like a pain to get the
> counts for a specific subset of a view, it really is one of the better
> solutions.

ok thanks, could you give me any hint on how to write this reduce in a
generic way for pagination? I'm writing a mapper for couchdb, is there
a way to this in a general way so that it can be used for any view or
is it too specific to each query/view?

Thanks in advance,

Pat

Re: how do I get the total number of rows returned for a given key?

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

The total_rows can be a bit misleading. As you've noticed its just the
total number of rows in the entire view. The main case for this
behavior is that its quick. To report the rows we can just read the
view file for an attribute. Calculating the number of rows for a
specific key would require one of two things, either we scan the view
as we would for docs to be returned, or we create some method that
would count the number of times a key exists as docs are
added/updated/deleted from the view.

The thing is, keys are complex. There's no way couch could keep track
of all the different possible start/end key combinations for arbitrary
key structures. Which leaves us with scanning the view. This is fairly
inefficient in terms of implementation. Which leaves us at, just
report total rows in the view.

Now that upside is that you can create your own methods that will
track the count of a given key combination using reduce. And the
awesome part is that you can do it however you want with as many
combinations as you want. So while it may look like a pain to get the
counts for a specific subset of a view, it really is one of the better
solutions.

HTH,
Paul

On Mon, Aug 25, 2008 at 12:59 AM, Patrick Aljord <pa...@gmail.com> wrote:
> Hey all,
>
> I'm trying to get the number of docs returned by my view given a
> certain key such as:
>
> myview?key=foo&count=0
>
> I thought total_rows would give the numbers of docs that have this
> specific key but instead it gave me the total rows for all keys. I was
> told by "jeffd" on IRC this is the normal behavior but, I quote:
>
> "there was talk in this channel at some point of adding another piece,
> but I dunno how far that went".
>
> So my question is: are you going to add another piece like total_rows
> but that would return the number of rows for a given key? Or is there
> a better way to do so? (with reduce I guess, but still it would be
> nice to have it included). This is really needed for pagination.
> I spotted an issue for that https://issues.apache.org/jira/browse/COUCHDB-82
>
>
> Thanks in advance,
>
> Pat
>