incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anand Chitipothu <anandol...@gmail.com>
Subject Re: how to access key names of a document?
Date Sat, 02 May 2009 05:07:20 GMT
> 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

Mime
View raw message