From b11d2c959928565f11f98cf2eb8dc8c6b276433f Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 15 Jan 2009 15:12:52 -0800 Subject: [PATCH] word count views --- examples/word_count/word_count_views.rb | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/word_count/word_count_views.rb diff --git a/examples/word_count/word_count_views.rb b/examples/word_count/word_count_views.rb new file mode 100644 index 0000000..4b1fb62 --- /dev/null +++ b/examples/word_count/word_count_views.rb @@ -0,0 +1,26 @@ +require 'rubygems' +require 'couchrest' + +couch = CouchRest.new("http://127.0.0.1:5984") +db = couch.database('word-count-example') + +word_count = { + :map => 'function(doc){ + var words = doc.text.split(/\W/); + words.forEach(function(word){ + if (word.length > 0) emit([word,doc.title],1); + }); + }', + :reduce => 'function(key,combine){ + return sum(combine); + }' +} + +db.delete db.get("_design/word_count") rescue nil + +db.save({ + "_id" => "_design/word_count", + :views => { + :words => word_count + } +})