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
}
}
37% of all statistics are made up on the spot
-------------------------------------------------------------------------------------
Nitin Borwankar
[email protected]
On Sat, May 2, 2009 at 5:57 PM, Nitin Borwankar <[email protected]> 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
> [email protected]
>
>
> On Fri, May 1, 2009 at 10:07 PM, Anand Chitipothu <[email protected]>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
>>
>
>