Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with the Views(Map/Reduce) example #10

Open
7stud opened this issue Feb 4, 2018 · 1 comment
Open

Problem with the Views(Map/Reduce) example #10

7stud opened this issue Feb 4, 2018 · 1 comment

Comments

@7stud
Copy link

7stud commented Feb 4, 2018

Couchbase: Community Edition 5.0.1 build 5003

I'm having problems with the Views example on the README page. When I do:

res.each do |row|
  # Returns extended results by default
  puts "#{row.key}: #{row.value}"
end

The output I get is:

hello-world: {:title=>"Hello World", :body=>"Well hello and welcome to my new blog...", :date=>"2009/01/15 15:52:20"}
biking: {:title=>"Biking", :body=>"My biggest hobby is mountainbiking. The other day...", :date=>"2009/01/30 18:04:11"}
bought-a-cat: {:title=>"Bought a Cat", :body=>"I went to the the pet store.", :date=>"2009/01/30 20:04:11"}

I expected the output:

"2009/01/15 15:52:20": "Hello World"
"2009/01/30 18:04:11": "Biking"  
"2009/01/30 20:04:11": "Bought a Cat"

Because the map function is this:

     "map": "function(doc){if(doc.date && doc.title){emit(doc.date, doc.title);}}"

I think map() should emit the date as row.key and the title as row.value.

Full code, which is copied from the README:

require 'libcouchbase'

bucket = Libcouchbase::Bucket.new(
  hosts: '127.0.0.1', 
  bucket: 'recent-posts', 
  username: '7stud',
  password: 'abc123')


bucket['biking'] = {:title => 'Biking',
                   :body => 'My biggest hobby is mountainbiking. The other day...',
                   :date => '2009/01/30 18:04:11'}
bucket['bought-a-cat'] = {:title => 'Bought a Cat',
                          :body => 'I went to the the pet store.',
                          :date => '2009/01/30 20:04:11'}
bucket['hello-world'] = {:title => 'Hello World',
                        :body => 'Well hello and welcome to my new blog...',
                        :date => '2009/01/15 15:52:20'}

view_doc = %q|
    {
      "_id": "_design/blog",
      "language": "javascript",
      "views": {
        "recent_posts": {
          "map": "function(doc){if(doc.date && doc.title){emit(doc.date, doc.title);}}"
        }
      }
    }
|

bucket.save_design_doc(view_doc)

blog = bucket.design_docs['blog']
p blog.views    

res = blog.view('recent_posts')  #=> #<Libcouchbase::Results:0x007fbaed12c988>

res.each do |row|
  # Returns extended results by default
  puts "#{row.key}: #{row.value}"
end

I'm not sure what the comment #Returns extended results by default means. If I do:

res = blog.view('recent_posts', include_docs: false)

then my results are:

hello-world: 
biking: 
bought-a-cat: 

It doesn't seem to matter what the map function emit()'s because the results are always the same. For instance, I get the same output with emit(1, 2), emit('abc', '123'), and emit(doc.date, doc.title)

@7stud 7stud changed the title Problem with Views(Map/Reduce) example Problem with the Views(Map/Reduce) example Feb 4, 2018
@stakach
Copy link
Member

stakach commented Feb 6, 2018

This is the expected behaviour. My primary use case was to use the emitted values for filtering or ordering. The key is always set to the key of the document that emitted the value - you can see the structure that couchbase sends back here:
https://github.com/cotag/libcouchbase/blob/master/lib/libcouchbase/ext/libcouchbase/respviewquery.rb

For the emitted values, you want to look at:

res.each do |row|
  # See what was emitted 
  puts row.metadata[:emitted]
end

This isn't parsed, so at this point will be a string. (Probably an array represented in JSON? I'm not actually sure)

You can see how the response from couchbase is processed here: https://github.com/cotag/libcouchbase/blob/master/lib/libcouchbase/query_view.rb#L77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants