multi-key map reduce and calling the view with only 1 key

80 views
Skip to first unread message

mark

unread,
Apr 5, 2010, 5:13:23 AM4/5/10
to CouchDB-Python
I have a map/reduce set up and the result has 2 keys in an array and
I'm having some trouble getting the results with python-couchdb.

I thought that this:
http://localhost:5984/results/_design/counter/_view/treatment?group=true&keys="BlUTq"

which returns:
{"key":["BlUTq","control"],"value":20},
{"key":["BlUTq","treatment"],"value":14}

should be equivalent to this:
results = db.view("counter/treatment", group = 'true', keys =
[ "BlUTq"])

however this returns no results. the only way I can get the full
results is if "keys" contains an array like this:

results = db.view("counter/treatment", group = 'true', keys =
[ ["BlUTq", "treatment"], ["BlUTq", "control"] ])

am I missing something? I've tried 'keys = [ ["BlUTq"] ]' as well, but
still not what I want.

I would like to be able to just specify one of the keys like I can
with curl/browser.


Matt Goodall

unread,
Apr 9, 2010, 7:52:40 AM4/9/10
to couchdb...@googlegroups.com
On 5 April 2010 10:13, mark <mark.d...@gmail.com> wrote:
I have a map/reduce set up and the result has 2 keys in an array and
I'm having some trouble getting the results with python-couchdb.

I thought that this:
http://localhost:5984/results/_design/counter/_view/treatment?group=true&keys="BlUTq"

which returns:
{"key":["BlUTq","control"],"value":20},
{"key":["BlUTq","treatment"],"value":14}

I don't think that URL is doing what you expect.

AFAIK, CouchDB does not support a "keys" query parameter so it's probably getting ignored. You need to POST the list of keys to CouchDB. Also, the keys must be a list where each item in the list is an exact match (in JSON terms) for a key in your results.

For instance, to retrieve the results you're expecting, the curl command would look something like this:

    -X POST \
    -d '{"keys":[["BlUTq","control"],["BlUTq","treatment"]]}'

 

should be equivalent to this:
results = db.view("counter/treatment",  group = 'true', keys =
[ "BlUTq"])

however this returns no results.

That's actually equivalent to POST'ing -d '{"keys": ["BlUTq"]}' with curl. No keys match so nothing is returned.

 
the only way I can get the full
results is if "keys" contains an array like this:

results = db.view("counter/treatment",  group = 'true', keys =
[ ["BlUTq", "treatment"], ["BlUTq", "control"] ])

Yep, this is correct for your dataset and is equivalent to my first curl command and so should produce the same results.
 

am I missing something? I've tried 'keys = [ ["BlUTq"] ]' as well, but
still not what I want.

I would like to be able to just specify one of the keys like I can
with curl/browser.

I think you just misunderstood how to query CouchDB and that's caused you confusion when using couchdb-python.


<aside>
I suspect what you really want is all the results for BlUTq? In that case you want to get all the keys between ["BlUTq"] and ["BlUTq", {}].

In Python, that would be:

    db.view("counter/treatment",  group = 'true',
            startkey=["BlUTq"], endkey=["BlUTq", {}]

Which is equivalent to the following curl:

curl http://localhost:5984/results/_design/counter/_view/treatment?group=true&startkey=%5B%22BlUTq%22%5D&endkey=%5B%22BlUTq%22%2C+%7B%7D%5D

For reference: %5B == [, %5D == ], %7B == {, %7D == }, %22 == ", %
</aside>


Hope that helps and hasn't totally confused you!


- Matt

Reply all
Reply to author
Forward
0 new messages