incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: how to access key names of a document?
Date Sun, 03 May 2009 20:12:49 GMT
On Sat, May 2, 2009 at 5:59 PM, Nitin Borwankar <nitin@borwankar.com> wrote:
> sorry previous message got prematurely sent....
>
>
> Anand,
>
> Thanks much.
> I did muddle around and figure that out in the meanwhile by playing around
> in futon.
> But when I create the design doc and put this code in I get a "bad match"
> Erlang error.
>
>
> {
>
>   [...]
>
>  views: {
>
>     cnt : {
>       map:  function () {....},
>       reduce: function() {...}
>     }
>    /// no more views here
>  }
>
> }

Hmm, it might help to post the literal design doc JSON to
friendpaste.com or somesuch. From what you've written you need to put
"function(){..}" in quotes, but it's hard to diagnose without more
detail.

Also, use CouchApp, it's designed to take the guesswork out of managing views.

Cheers,
Chris

>
>
> 37% of all statistics are made up on the spot
> -------------------------------------------------------------------------------------
> Nitin Borwankar
> nborwankar@gmail.com
>
>
> On Sat, May 2, 2009 at 5:57 PM, Nitin Borwankar <nitin@borwankar.com> wrote:
>
>> Anand,
>>
>> Thanks much.
>> I did muddle around and figure that out in the meanwhile by playing around
>> in futon.
>> But when I create the design doc and put this code as
>>
>> {
>>
>> [...]
>>
>> views: {
>>
>>   cnt : {
>>      map:  function () {....},
>>      reduce: function() {...}
>>   }
>>
>> }
>>
>> 37% of all statistics are made up on the spot
>>
>> -------------------------------------------------------------------------------------
>> Nitin Borwankar
>> nborwankar@gmail.com
>>
>>
>> On Fri, May 1, 2009 at 10:07 PM, Anand Chitipothu <anandology@gmail.com>wrote:
>>
>>> > I have a database with documents that have varying number of fields.
>>> > I need to create a view that displays
>>> >
>>> > { field_name1 : count, ..... }
>>> >
>>> > i.e. a report that displays how many times a field occurs in the db.
>>> > It's like a word frequency vector in Lucene except for the fact that
>>> these
>>> > are field names.
>>> >
>>> > So I am missing something basic but I need to loop over all keys of a
>>> doc
>>> > without knowing their names in advance.
>>> > so I do something like
>>> >
>>> > var x = {}
>>> > for ( var k in doc.keys ) {
>>> >  x[k] = 1
>>> > }
>>> > emit ( x, 1 ) <---- is this the right thing to emit ?
>>>
>>> No, you should do this:
>>>
>>> function(doc) {
>>>    for (var k in doc)
>>>        emit(k, 1);
>>> }
>>>
>>> And your reduce function should be:
>>>
>>> function(keys, values, rereduce) {
>>>    return sum(values);
>>> }
>>>
>>> I'm assuming that you want global frequency of k1, k2 etc in documents
>>> like {"k1": "v1", "k2", "v2", ..}
>>>
>>> If name of your design document is d and name of your view is v, you
>>> can get the frequency from:
>>>
>>> http://localhost:5984/dbname/_design/d/_view/v?reduce=true
>>>
>>> > newbie questions
>>> > a) whats the syntax to access all keys of a doc
>>>
>>> You can achieve this by writing a view with the following map function:
>>>
>>> function(doc) {
>>>    var keys = [];
>>>    for (key in doc)
>>>        keys.push(key);
>>>    emit(doc.id, keys);
>>> }
>>>
>>> > b) what's the right thing to emit
>>> > c) what do I do in the reduce phase ?
>>>
>>> Answered above.
>>>
>>> -Anand
>>>
>>
>>
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message