You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Terry Brownell <te...@gmail.com> on 2010/10/15 01:07:33 UTC

Cooking up Views

Hi, attempting to port my semantic network DB to CouchDB, but I'm not
grokking the more complicated views, and in particular passing variables.

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?


{
   "_id": "tweety",
    "isa": "bird",
   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
   "color": "Yellow",
   "nemesis": "Sylvester"
}

{
   "_id": "Sweetie Pie",
    "isa": "bird",
   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
   "color": "Yellow"
}

{
   "_id": "Woody",
    "isa": "bird",
   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
   "color": "Blue",
"nemesis": "Buzz Buzzard"

}

Thanks

Re: Cooking up Views

Posted by "Eli Stevens (Gmail)" <wi...@gmail.com>.
Without knowing what, exactly, you're trying to accomplish, this might
be off the mark, but you could do something like:

function(doc) {
    if (doc.isa && doc.color) {
        for (i in doc.isa) {
            emit([doc.isa[i], doc.color], null);
        }
    }
}

With this you can have ?startkey=["bird", "yellow"]&endkey=["bird",
"yellow", {}] to get all of the yellow birds,
?startkey=["pet"]&endkey=["pet", {}] to get all of the pets of any
color, etc.

You could also make color a list:

{
  "_id": "Foghorn Leghorn",
  "_rev": "..."
  "isa": ["rooster", "blowhard", "target"],
  "color": ["Brown", "White", "Red"]
}

And have a map:

function(doc) {
    if (doc.isa && doc.color) {
        for (i in doc.isa) {
            for (j in doc.color) {
                emit([doc.isa[i], doc.color[j]], null);
            }
        }
    }
}

And then use keys just like before (the view will contain the cross
product of "isa" and "color").

HTH,
Eli

On Fri, Oct 15, 2010 at 12:10 AM, Terry Brownell
<te...@gmail.com> wrote:
> Thanks Paul,
>
> Here's another question.
> Let's say i expand the "isa" property to contain an array...
>
> {
>   "_id": "tweety",
>    "isa": ["bird", "pet", "target"],
>   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>   "color": "Yellow",
>   "nemesis": "Sylvester"
> }
>
> And say all my docs have various "isa" (or tags) values.. does this mean I
> need to put the values in specific order and know this order in advance to
> find them? ie:
> http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name?key=["bird",
> "pet" "target"]
>
>
> Terry
>
> On Thu, Oct 14, 2010 at 4:19 PM, Paul Davis <pa...@gmail.com>wrote:
>
>> Terry,
>>
>> // map.js
>> function(doc) {
>>    if(!doc.isa || !doc.color || !doc.nemesis) return;
>>    emit([doc.isa, doc.color], doc.nemesis);
>> }
>>
>> // Query URL
>> http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name?key=["bird",
>> "yellow"]
>>
>> HTH,
>> Paul Davis
>>
>> On Thu, Oct 14, 2010 at 7:07 PM, Terry Brownell <te...@gmail.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.
>> >
>> > 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?
>> >
>> >
>> > {
>> >   "_id": "tweety",
>> >    "isa": "bird",
>> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>> >   "color": "Yellow",
>> >   "nemesis": "Sylvester"
>> > }
>> >
>> > {
>> >   "_id": "Sweetie Pie",
>> >    "isa": "bird",
>> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>> >   "color": "Yellow"
>> > }
>> >
>> > {
>> >   "_id": "Woody",
>> >    "isa": "bird",
>> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>> >   "color": "Blue",
>> > "nemesis": "Buzz Buzzard"
>> >
>> > }
>> >
>> > Thanks
>> >
>>
>

Re: Cooking up Views

Posted by Simon Metson <si...@googlemail.com>.
Hi,

I think something like:

function(doc) {
   if(!doc.isa || !doc.color || !doc.nemesis) return;
   for (type in doc.isa) {
     emit([doc.isa[type], doc.color], doc.nemesis);
   }
}

that would emit something like:

{"total_rows":3,"offset":0,"rows":[
{"id":"448b2670dda335dcab5ee3952900068e","key": 
["bird","Yellow"],"value":"Sylvester"},
{"id":"448b2670dda335dcab5ee3952900068e","key": 
["pet","Yellow"],"value":"Sylvester"},
{"id":"448b2670dda335dcab5ee3952900068e","key": 
["target","Yellow"],"value":"Sylvester"}
]}
You could then find the nemeses of yellow pets by querying with

http://localhost:5984/demo/_design/demo/_view/target? 
key=["pet","Yellow"]

{"total_rows":3,"offset":1,"rows":[
{"id":"448b2670dda335dcab5ee3952900068e","key": 
["pet","Yellow"],"value":"Sylvester"}
]}
or for targets that are Yellow with

http://localhost:5984/demo/_design/demo/_view/target? 
key=["pet","Yellow"]

If you want to find pet birds who are targets you'd need a different  
view (and possibly Lucene or multiview, depending on how free your  
list of tags are).
Cheers
Simon

On 15 Oct 2010, at 08:10, Terry Brownell wrote:

> Thanks Paul,
>
> Here's another question.
> Let's say i expand the "isa" property to contain an array...
>
> {
>   "_id": "tweety",
>    "isa": ["bird", "pet", "target"],
>   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>   "color": "Yellow",
>   "nemesis": "Sylvester"
> }
>
> And say all my docs have various "isa" (or tags) values.. does this  
> mean I
> need to put the values in specific order and know this order in  
> advance to
> find them? ie:
> http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name? 
> key=["bird",
> "pet" "target"]
>
>
> Terry
>
> On Thu, Oct 14, 2010 at 4:19 PM, Paul Davis <paul.joseph.davis@gmail.com 
> >wrote:
>
>> Terry,
>>
>> // map.js
>> function(doc) {
>>   if(!doc.isa || !doc.color || !doc.nemesis) return;
>>   emit([doc.isa, doc.color], doc.nemesis);
>> }
>>
>> // Query URL
>> http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name? 
>> key=["bird",
>> "yellow"]
>>
>> HTH,
>> Paul Davis
>>
>> On Thu, Oct 14, 2010 at 7:07 PM, Terry Brownell <terrybrownell@gmail.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.
>>>
>>> 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?
>>>
>>>
>>> {
>>>  "_id": "tweety",
>>>   "isa": "bird",
>>>  "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>>>  "color": "Yellow",
>>>  "nemesis": "Sylvester"
>>> }
>>>
>>> {
>>>  "_id": "Sweetie Pie",
>>>   "isa": "bird",
>>>  "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>>>  "color": "Yellow"
>>> }
>>>
>>> {
>>>  "_id": "Woody",
>>>   "isa": "bird",
>>>  "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>>>  "color": "Blue",
>>> "nemesis": "Buzz Buzzard"
>>>
>>> }
>>>
>>> Thanks
>>>
>>


Re: Cooking up Views

Posted by Terry Brownell <te...@gmail.com>.
Thanks Paul,

Here's another question.
Let's say i expand the "isa" property to contain an array...

{
   "_id": "tweety",
    "isa": ["bird", "pet", "target"],
   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
   "color": "Yellow",
   "nemesis": "Sylvester"
}

And say all my docs have various "isa" (or tags) values.. does this mean I
need to put the values in specific order and know this order in advance to
find them? ie:
http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name?key=["bird",
"pet" "target"]


Terry

On Thu, Oct 14, 2010 at 4:19 PM, Paul Davis <pa...@gmail.com>wrote:

> Terry,
>
> // map.js
> function(doc) {
>    if(!doc.isa || !doc.color || !doc.nemesis) return;
>    emit([doc.isa, doc.color], doc.nemesis);
> }
>
> // Query URL
> http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name?key=["bird",
> "yellow"]
>
> HTH,
> Paul Davis
>
> On Thu, Oct 14, 2010 at 7:07 PM, Terry Brownell <te...@gmail.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.
> >
> > 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?
> >
> >
> > {
> >   "_id": "tweety",
> >    "isa": "bird",
> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
> >   "color": "Yellow",
> >   "nemesis": "Sylvester"
> > }
> >
> > {
> >   "_id": "Sweetie Pie",
> >    "isa": "bird",
> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
> >   "color": "Yellow"
> > }
> >
> > {
> >   "_id": "Woody",
> >    "isa": "bird",
> >   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
> >   "color": "Blue",
> > "nemesis": "Buzz Buzzard"
> >
> > }
> >
> > Thanks
> >
>

Re: Cooking up Views

Posted by Paul Davis <pa...@gmail.com>.
Terry,

// map.js
function(doc) {
    if(!doc.isa || !doc.color || !doc.nemesis) return;
    emit([doc.isa, doc.color], doc.nemesis);
}

// Query URL
http://127.0.0.1:5984/dbname/_design/ddocid/_view/view_name?key=["bird",
"yellow"]

HTH,
Paul Davis

On Thu, Oct 14, 2010 at 7:07 PM, Terry Brownell <te...@gmail.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.
>
> 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?
>
>
> {
>   "_id": "tweety",
>    "isa": "bird",
>   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>   "color": "Yellow",
>   "nemesis": "Sylvester"
> }
>
> {
>   "_id": "Sweetie Pie",
>    "isa": "bird",
>   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>   "color": "Yellow"
> }
>
> {
>   "_id": "Woody",
>    "isa": "bird",
>   "_rev": "3-d085e60568133d80a96cd735c6c306f6",
>   "color": "Blue",
> "nemesis": "Buzz Buzzard"
>
> }
>
> Thanks
>