public
Created — forked from kierangraham/gist:2773123

issue with complex keys and startkey and endkey in couchbase-1.2.0.dp ruby client

  • Download Gist
gistfile1.md
Markdown

 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.