incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark J. Reed" <markjr...@gmail.com>
Subject Re: Cooking Views
Date Fri, 15 Oct 2010 13:34:49 GMT
On Thu, Oct 14, 2010 at 7:00 PM, Terry Brownell
<terry.brownell@trinitypower.com> wrote:
> Hi, attempting to port my semantic network DB to CouchDB, but I'm not
> grokking the more complicated views, and in particular passing variables.

Depending on the types of query you anticipate, Couch (at least
without Solr) might not be the best choice for this problem domain.
In my experience it works best with canned queries that don't vary
much; if you have a bunch of ad-hoc queries with different types of
logic being composed on the fly, you may want to look at either adding
Solr to the mix or switching to another storage technology like
MongoDB.

Having said that, here's how I would solve your specific problem in bare Couch.

> Looking for a cookbook recipe for creating a view (against the documents
> below), that...
>
> - Selects all documents where "isa" = "bird", that has a nemesis, and color
> is a variable ie: all yellow?

Create a view with a map function that emits the variable part as the
key, but only for documents matching the non-variable criteria.  For
example,

"map": "function(doc) { if (doc.nemesis && doc.isa == 'bird') {
emit(doc.color, doc.nemesis) }}"

Then query that view with ?key="yellow".

You can make it more flexible by emitting a complex key consisting of
[doc.isa, doc.color], instead of making it conditional on isa; then
you would query for key=["bird","yellow"].  What that gains you is the
ability not only to query for different combinations of animal and
color, but also to use a range query to look for specific animal types
regardless of color (e.g. startkey=["bird"]&endkey=["bird",{}] to get
all the nemesis-having birds). But order matters; with isa first, this
view doesn't help you if you want to query for all yellow
nemesis-having animals whether they're a bird or not.  You would have
to construct a different view for that.

-- 
Mark J. Reed <markjreed@gmail.com>

Mime
View raw message