couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Patrick Barnes <mrtr...@gmail.com>
Subject Re: Mapping multiple entries in an array field? (like tags)
Date Sun, 22 May 2011 09:12:35 GMT
Hi He,

The view query api supports a 'keys' parameter.
http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options

So your view can emit a parameter for every tag.

function(doc) {
   for (var i in doc.tags)
     emit(doc.tags[i],null);
}

(so the view results would look like:
{key:"python",id:"doc1",value:null},
{key:"couchdb",id:"doc1",value:null},
{key:"web",id:"doc1,value:null},
...

And to fetch results for a given set of tags (say 'couchdb' and 
'python') you can call:

curl -X POST http://localhost:5984/db/ \
-H "Content-Type: application/json" -d '{"keys":["couchdb","python"]}'

Hope that helps,
-Patrick Barnes

On 22/05/2011 4:59 PM, He Shiming wrote:
> Dear Community,
>
> I'm working with documents like this:
> {
>      "tags": ["python", "couchdb", "web"]
> }
>
> I'd like to be able to query this document, by all of the following keys:
> "python"
> ["python", "web"]
> ["couchdb", "python"]
> ["web", "python", "couchdb"]
>
> but not by keys such as:
> "redis"
> ["python", "redis"]
>
> It's a tag-like situation. I can have the parameter and the field
> sorted in alphabetical order if necessary. But it should support any
> number of "tags" as the key.
>
> The only solution I figured, is to compose a list of possible key
> combinations in the map function, then emit all of them. And this way,
> one view will only be able to handle a fixed number of keys. For that
> matter, the function gets really complicated for the 3-key (or above)
> query. So I'm wondering if there are better ways to do this.
>
> Thanks!
> He Shiming
>

Mime
View raw message