tags:

views:

944

answers:

2

is there a way to send multiple startKey/endKey pairs to a view, akin to the keys: [] array that can be posted for keys?

the underlying problem - let's say my documents have "categories" and timestamps. if i want all documents in the "foo" category that have a timestamp that's within the last two hours, it's simple:

function (doc) {
  emit([doc.category, doc.timestamp], null);
}

and then query as

GET server:5894/.../myview?startKey=[foo, |now - 2 hours|]&endkey=[foo, |now|]

the problem comes when i want something in categories foo or bar, within the last two hours. if i didn't care about time, i could just pull directly by key through the keys collection. unfortunately, i have no such option with ranges.

what i ended up doing in the meantime is rounding the timestamp to two-hour blocks, and then multiplexing the query out:

POST server:5894/.../myview
keys=[[foo, 0 hours], [foo, 2 hours], [bar, 0 hours], [bar, 2 hours]]

it works, but will get messy if i want to go back a large amount of time (in relationship to the blocksize)

+1  A: 

Your probably better off just doing two queries. CouchDB can handle multiple simultaneous queries pretty well so spin off several processes/threads and query for foo and bar docs seperately.

CouchDB does not currently support multiple range queries. ORing and ANDing keys is pretty much not doable in one query.

Jeremy Wall
well you can OR keys through the keys post, but yeah. thanks.
kolosy
A: 

There is a CouchDB issue request to let you do just that. I've attached a simple, no guarantees patch to 0.10.1 to that ticket which may work for you. It works for me and lets me do things like:

{
    "keys": [
        {
            "startkey": ["0240286524","2010","03","01"],
            "endkey": ["0240286524","2010","03","07",{}]
        },
        {
            "startkey": ["0442257276","2010","03","01"],
            "endkey": ["0442257276","2010","03","07",{}]
        }
    ]
}

in the POST body, which lets me get all the data across multiple tracking ids, for a range of dates. I call with group=true&group_level=1 to have the results grouped by tracking id. Deeper group levels would allow me to group by tracking id|year, tracking id|year|month etc.

Multiple connections were an unscalable overhead for me as I'd be looking to make 2000 concurrently :) (No, a new view is not an option - we're already at 400GB for data plus one view!)

The issue and patch is at https://issues.apache.org/jira/browse/COUCHDB-523 .

majelbstoat