Skip to content

Instantly share code, notes, and snippets.

@kierangraham
Created May 23, 2012 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kierangraham/2773123 to your computer and use it in GitHub Desktop.
Save kierangraham/2773123 to your computer and use it in GitHub Desktop.
issue with complex keys and startkey and endkey in couchbase-1.2.0.dp ruby client

# Couchbase 1.2.0.dp Complex Key Query

Here is what's needed in order to replicate an issue I'm having with querying complex startkey and endkeys with the Ruby Couchbase 1.2.0.dp.

Use Case

Basically I want to use this kind of map/reduce on a document structure like below to find out the number of type by each author and want to use :start_key and :end_key to get a result for a particular author.

Sample Documents

{
	"_id": "3139527",
	"_rev": "1-3ff822afabb01fa40000000200000000",
	"$flags": 0,
	"$expiration": 0,
	"title": "The Avengers",
	"type": "video",
	"authors": [
		{ "name": "Marvel Studios" }
	]
}
{
	"_id": "1213007",
	"_rev": "1-00003ca23d54aff3000025a000000000",
	"$flags": 0,
	"$expiration": 0,
	"title": "Iron Man",
	"type": "video",
	"authors": [
		{ "name": "Marvel Studios" }
	]
}
{
	"_id": "811199",
	"_rev": "1-00003ca1c3836ef600001cd100000000",
	"$flags": 0,
	"$expiration": 0,
	"title": "Deadmau5 - Random Album Title",
	"type": "audio",
	"authors": [
		{ "name": "Deadmau5" }
	]
}
{
	"_id": "15667282",
	"_rev": "1-00003ca280f7f3010000050300000000",
	"$flags": 0,
	"$expiration": 0,
	"title": "Deadmau5 - For Lack of a Better Name",
	"type": "audio",
	"authors": [
		{ "name": "Deadmau5" }
	]
}

Map

function(doc) {
  doc.authors.forEach(function(author){
    emit([author.name, doc.type], doc.type);
  });
}

Reduce

_count

Expected Behaviour

Here's how I'd expect to get a result back as shown in the Couchbase Admin Console.

Example Working in Couchbase Admin Console

Test Script

A simple script to show the kind of problem I'm having and ways I've tried to get the result I'm expecting

require 'couchbase'
require 'cgi'

client = Couchbase.new("http://127.0.0.1:8091/pools/default/buckets/complex_keys")

# Gives warning and returns empty result
# ~/.rvm/gems/ruby-1.9.3-p0/gems/couchbase-1.2.0.dp/lib/couchbase/utils.rb:39: warning: regexp match /.../n against to UTF-8 string
puts "\n\nGives warning and returns empty result"
puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true, :startkey => ["Deadmau5", ""].to_json, :endkey => ["Deadmau5", "\u9999"].to_json).entries.inspect

# Returns empty result
puts "\n\nReturns empty result"
puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true, :startkey => CGI::escape(["Deadmau5", ""].to_json), :endkey => CGI::escape(["Deadmau5", "\u9999"].to_json)).entries.inspect

# Gives warning and returns empty result
# ~/.rvm/gems/ruby-1.9.3-p0/gems/couchbase-1.2.0.dp/lib/couchbase/utils.rb:39: warning: regexp match /.../n against to UTF-8 string
puts "\n\nGives warning and returns empty result"
puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true, :startkey => "['Deadmau5', '']", :endkey => "['Deadmau5', '\u9999']").entries.inspect

# Returns empty result
puts "\n\nReturns empty result"
puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true, :startkey => CGI::escape("['Deadmau5', '']"), :endkey => CGI::escape("['Deadmau5', '\u9999']")).entries.inspect

# Returns empty result
puts "\n\Returns empty result!"
puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true, :startkey => ["Deadmau5", ""], :endkey => ["Deadmau5", "\u9999"]).entries.inspect

Thanks

Cheers for looking at this, I hope it makes some sense. :)

Kieran Graham
me@kierangraham.com

@scalabl3
Copy link

You did a good job with the gist, and I re-created the documents and view and in the Admin panel, I get the same results. I'd have to test the couchbase views with the new ruby client which I haven't done. Ah I wish I could help, but I think that avsej in IRC is the best bet for helping you... I don't think it's a problem with your startkey and endkey, but might be a how it's being handled by the ruby client.

Ah one thing that isn't represented by your tests is:

puts client.design_docs["complex_keys"].test(:group => true, :group_level => 2, :reduce => true,
:startkey => {"Deadmau5" => ""}, :endkey => {"Deadmau5" => "\u9999"}).entries.inspect

Maybe :startkey and :endkey expect a hash instead of an array? avsej can confirm that, or if it needs to be done a different way...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment