Reputation: 594
I've been reading up on the documentation for couch db views (http://wiki.apache.org/couchdb/HTTP_view_API#HTTP_View_API, http://www.barkingiguana.com/2009/01/22/filtering-and-ordering-couchdb-view-results/) however I'm not finding what I'm looking for and I'm beginning to think its not supported.
I have two records in a couch database
{
"_id": "UUID",
"_rev": "rev",
"status": "complete",
"csv": [
{
"Lower": 0.09,
"Upper": 0.31
}
],
"tags": {
"get_info": {
"duration": "24",
"location": "south"
}
}
}
and
{
"_id": "2-UUID",
"_rev": "2-rev",
"status": "complete",
"csv": [
{
"Lower": 0.01,
"Upper": 0.70
}
],
"tags": {
"different_info": {
"duration": "60",
"location": "south"
}
}
}
Is it possible to create a view and add query parameters that would return all records that have the tag "key":"value" (e.g I want all records tagged "duration":24 or I want all records tagged "location":"south"). I don't want to hard code any key/values into a view - they should be passed with the query.
Is there another way of thinking about it that's not a view?
Upvotes: 0
Views: 1085
Reputation: 28429
One way to accomplish this is to emit all tags in your view as an array using [ key, value ]
:
function (doc) {
for (var type in doc.tags) {
for (var tag in doc.tags[type]) {
emit([ tag, doc.tags[type][tag] ]);
}
}
}
You can query your view with a specific pair: key=["duration","24"]
. You can also do ranged searches with startkey
and endkey
.
You can specify list of keys if you opt to POST
instead: keys[]=["duration","24"]&keys[]=["location","south"]
. However, with this method you lose the ability to do ranged searches. (at least atm)
When querying multiple keys like this, you'll be getting all documents that match any of the keys, so you may need to deduplicate the results on the client side.
CouchDB views are more about computation and algorithms, and not the best for arbitrary queries and searching. For that, I would highly recommend adding a search layer, such as couchdb-lucene or elasticsearch.
Upvotes: 1