You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Pavan Kumar <tv...@gmail.com> on 2010/04/13 06:03:09 UTC

MultiKey sorting in views

Hi,

  I have documents with author and submitted time ( utc value of date )
fields . Now i need to sort descending ( i.e latest first ) on time and
filter by author in a view.

I tried a few things like given below but they don't work. My "view"
knowledge needs lot of catching up.. Any pointers would help..

1) Filters by author but doesn't sort by time

startkey: [ author , 0 ]
endkey : [ author, now ]
descending : true

Map:
emit([doc.author,doc.submit_at],doc )


2) Doesn't filter by author !! :(

startkey: [ 0 , author ]
endkey : [ now, author ]
descending : true

Map: emit([doc.submit_at,doc.author],doc )

regards,
Pavan

RE: MultiKey sorting in views

Posted by Nils Breunese <N....@vpro.nl>.
Not within CouchDB AFAIK.

Solutions:

1) Sort on the client
2) Get couchdb-footrest to work (I tried it once, but failed. It promises 'Order Docs By Value Fields', but doesn't seem to be actively maintained.)
3) Use couchdb-lucene

Nils.
________________________________________
Van: Adam Wolff [awolff@gmail.com]
Verzonden: dinsdag 13 april 2010 19:14
Aan: user@couchdb.apache.org
Onderwerp: Re: MultiKey sorting in views

I understood the issue to be that if you query for multiple authors, the
results are sorted by author, then by date. You'd need to merge the results
for each author to get a chronological list of documents by date. I don't
think there's a simple fix for this, right?

A

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: MultiKey sorting in views

Posted by Adam Wolff <aw...@gmail.com>.
I understood the issue to be that if you query for multiple authors, the
results are sorted by author, then by date. You'd need to merge the results
for each author to get a chronological list of documents by date. I don't
think there's a simple fix for this, right?

A

On Tue, Apr 13, 2010 at 8:11 AM, Nils Breunese <N....@vpro.nl> wrote:

> Pavan Kumar wrote:
>
>   I have documents with author and submitted time ( utc value of date )
>> fields . Now i need to sort descending ( i.e latest first ) on time and
>> filter by author in a view.
>>
>> I tried a few things like given below but they don't work. My "view"
>> knowledge needs lot of catching up.. Any pointers would help..
>>
>> 1) Filters by author but doesn't sort by time
>>
>> startkey: [ author , 0 ]
>> endkey : [ author, now ]
>> descending : true
>>
>> Map:
>> emit([doc.author,doc.submit_at],doc )
>>
>
> What is the format of your submit_at field? If you use a format that
> alfabetically sorts like it would do if you sorted chronologically, then
> this would work, right? I use something like that myself using
> timestamps in RFC 3339 format ("Date and Time on the Internet:
> Timestamps") [0] which look like this:
>
>       2010-04-13T17:00:00+01:00
>
>       (year-month-dayThours:minutes:seconds and optional offset)
>
> The view keys will automatically be sorted alfabetically, but for RFC
> 3339 timestamps this will also be chronologically. I guess things like
> UNIX timestamps stored as a JSON number would also work, but they don't
> score as high on the human readability scale.
>
> Nils Breunese.
>
> [0] http://www.ietf.org/rfc/rfc3339.txt
>
> De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend
> bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie
> bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking
> van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO
> staat niet in voor de juiste en volledige overbrenging van de inhoud van een
> verzonden e-mail, noch voor tijdige ontvangst daarvan.
>

Re: MultiKey sorting in views

Posted by Nils Breunese <N....@vpro.nl>.
Pavan Kumar wrote:

>   I have documents with author and submitted time ( utc value of date )
> fields . Now i need to sort descending ( i.e latest first ) on time and
> filter by author in a view.
>
> I tried a few things like given below but they don't work. My "view"
> knowledge needs lot of catching up.. Any pointers would help..
>
> 1) Filters by author but doesn't sort by time
>
> startkey: [ author , 0 ]
> endkey : [ author, now ]
> descending : true
>
> Map:
> emit([doc.author,doc.submit_at],doc )

What is the format of your submit_at field? If you use a format that
alfabetically sorts like it would do if you sorted chronologically, then
this would work, right? I use something like that myself using
timestamps in RFC 3339 format ("Date and Time on the Internet:
Timestamps") [0] which look like this:

        2010-04-13T17:00:00+01:00

        (year-month-dayThours:minutes:seconds and optional offset)

The view keys will automatically be sorted alfabetically, but for RFC
3339 timestamps this will also be chronologically. I guess things like
UNIX timestamps stored as a JSON number would also work, but they don't
score as high on the human readability scale.

Nils Breunese.

[0] http://www.ietf.org/rfc/rfc3339.txt

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: MultiKey sorting in views

Posted by Kenneth Tyler <ke...@8thfold.com>.
>  I have documents with author and submitted time ( utc value of date )
> fields . Now i need to sort descending ( i.e latest first ) on time and
> filter by author in a view.

can you make a composite key:   author + ':' + day + month + year

where you use a reverse index for the day, month...
so for jan 1 you use 3112
for jan 10 you use 2112
day index = days in month - day
month index = 13 - month
for year index you have to pick a future year: 3000 say
then year index = 3000 - year
then jan 1, 2010 becomes 31121990
jan 1, 2009 becomes 31121991

if i posted both posting then the composite key would be
tylerken:31121990
tylerken:31121991

and that should sort in author date descending order

ken tyler

Re: MultiKey sorting in views

Posted by Michael Ludwig <mi...@gmx.de>.
Pavan Kumar schrieb am 13.04.2010 um 09:33:09 (+0530):

>   I have documents with author and submitted time ( utc value of
> date ) fields . Now i need to sort descending ( i.e latest first )
> on time and filter by author in a view.

I think the underlying issue which makes this impossible in CouchDB
without add-on is that the engine does just one pass [*] over an index
for either an identity or a range query, and there simply isn't any
provision for sorting other than by the very index the query uses.

[*] My understanding of the engine is very limited, but I guess that
a second pass must be performed to merge results from several instances
in order to produce a sorted result. Still, this is just a merge, so in
the context of sorting, it is a one-pass conceptually.

Please correct me if I'm mistaken in any of my half-blind assumptions.

-- 
Michael Ludwig

Re: MultiKey sorting in views

Posted by Adam Wolff <aw...@gmail.com>.
This isn't easy to handle in couch. Your best bet is to sort client-side if
possible.

A

On Mon, Apr 12, 2010 at 9:03 PM, Pavan Kumar <tv...@gmail.com> wrote:

> Hi,
>
>  I have documents with author and submitted time ( utc value of date )
> fields . Now i need to sort descending ( i.e latest first ) on time and
> filter by author in a view.
>
> I tried a few things like given below but they don't work. My "view"
> knowledge needs lot of catching up.. Any pointers would help..
>
> 1) Filters by author but doesn't sort by time
>
> startkey: [ author , 0 ]
> endkey : [ author, now ]
> descending : true
>
> Map:
> emit([doc.author,doc.submit_at],doc )
>
>
> 2) Doesn't filter by author !! :(
>
> startkey: [ 0 , author ]
> endkey : [ now, author ]
> descending : true
>
> Map: emit([doc.submit_at,doc.author],doc )
>
> regards,
> Pavan
>