From 50f01041735d360982d1eb676a8b606aca3e92b0 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 15 Jan 2009 15:05:55 -0800 Subject: [PATCH] updated word count example --- README.md | 3 +-- examples/word_count/word_count.rb | 27 +++---------------------- examples/word_count/word_count_query.rb | 13 ++++++------ 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index d6bcfde..c42f06b 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,7 @@ which I find to be concise, clear, and well designed. CouchRest lightly wraps CouchDB's HTTP API, managing JSON serialization, and remembering the URI-paths to CouchDB's API endpoints so you don't have to. -CouchRest's lighweight is designed to make a simple base for application and -framework-specific object oriented APIs. +CouchRest is designed to make a simple base for application and framework-specific object oriented APIs. CouchRest is Object-Mapper agnostic, the parsed JSON it returns from CouchDB shows up as subclasses of Ruby's Hash. Naked JSON, just as it was mean to be. ## Easy Install diff --git a/examples/word_count/word_count.rb b/examples/word_count/word_count.rb index f0321ae..4f3af68 100644 --- a/examples/word_count/word_count.rb +++ b/examples/word_count/word_count.rb @@ -1,4 +1,5 @@ -require File.dirname(__FILE__) + '/../../couchrest' +require 'rubygems' +require 'couchrest' couch = CouchRest.new("http://127.0.0.1:5984") db = couch.database('word-count-example') @@ -39,29 +40,7 @@ books.keys.each do |book| end end end - -# 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 => { -# :count => word_count, -# :words => {:map => word_count[:map]} -# } -# }) # puts "The books have been stored in your CouchDB. To initiate the MapReduce process, visit http://127.0.0.1:5984/_utils/ in your browser and click 'word-count-example', then select view 'words' or 'count'. The process could take about 15 minutes on an average MacBook." -# + diff --git a/examples/word_count/word_count_query.rb b/examples/word_count/word_count_query.rb index e69b99a..07e2446 100644 --- a/examples/word_count/word_count_query.rb +++ b/examples/word_count/word_count_query.rb @@ -1,22 +1,23 @@ -require File.dirname(__FILE__) + '/../../couchrest' +require 'rubygems' +require 'couchrest' couch = CouchRest.new("http://127.0.0.1:5984") db = couch.database('word-count-example') puts "Now that we've parsed all those books into CouchDB, the queries we can run are incredibly flexible." puts "\nThe simplest query we can run is the total word count for all words in all documents:" - -puts db.view('word_count/count').inspect +puts "this will take a few minutes the first time. if it times out, just rerun this script in a few few minutes." +puts db.view('word_count/words').inspect puts "\nWe can also narrow the query down to just one word, across all documents. Here is the count for 'flight' in all three books:" word = 'flight' params = { :startkey => [word], - :endkey => [word,'Z'] + :endkey => [word,{}] } -puts db.view('word_count/count',params).inspect +puts db.view('word_count/words',params).inspect puts "\nWe scope the query using startkey and endkey params to take advantage of CouchDB's collation ordering. Here are the params for the last query:" puts params.inspect @@ -28,7 +29,7 @@ params = { :key => [word, title] } -puts db.view('word_count/count',params).inspect +puts db.view('word_count/words',params).inspect puts "\nHere are the params for 'flight' in the da-vinci book:"