Thursday, July 3, 2008

It's all about document design with CouchDB

Hello everyone, been stalking the mailing list for a while and thought this might be worthy of a post as I was asked to solve it, yet, I couldn't!

Let's take the classic blog example document with the following fields/values:

"_id": "1f2fc3955b91aed5e7369f0b0ba8214e",
"_rev": "1226709986",
"Author": "Bradford",
"Type": "Post",
"Body": "Just mentioning this for a sample blog post.",
"PostedDate": "2008-07-02T23:22:12-04:00",
"Subject": "My Fine Blog Post",
"Tags": ["octopus","hockey","squidward","bradford","recreation"]

Next, I'd like to find each blog post that contains ANY of the following tags ["octopus","hockey"]. Now, generally speaking this isn't so bad. We could write a simple view:

function (doc) {
if (doc.Type == 'Post') {
for (var i = 0;i < doc.tags.length; i++) {
emit(doc.tags[i],doc);
}
}
}

We would get back each one of our tags as a key, yea? Only if we supplied one at a time. So how does one go about supplying a range, array (not sure what we'd call it here) of keys to be searched on? http://...?key=["octopus","hockey"] maybe? I'm unsure of the plan of attack for such a thing. Maybe I'm just going about it in the wrong direction. Any thoughts?

3 comments:

Anonymous said...

I'm pretty certain the answer here is a dynamic view. Since you can't pre-compute the result set (since the tag list is variable) you won't be taking any kind of performance hit.

Essentially, abstract that view snippet into a method, and then just call it with the right arguments, compose the view, and submit it to couchdb for immediate processing.

I think. :)

JanL said...

Adam, dynamic views are never a good idea except for development. The solution is either to use multiple queries to that view or use a fulltext search.

BradfordW said...

Thanks for the comments guys. I decided to bite off far more than I can chew and attempt to implement the multiple key query we spoke of on the mailing list. I've just returned from a weekend away so I'll follow up with the proposed fix.