Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why emit(meta.id, NULL)

I've read some sample code (especially from the Couchbase Model Views demo project link) and realized the map() function is so strange.

function(doc, meta) {
   if (doc.type == "beer" && doc.name){
       emit(doc.name, null);
   }
}

Why the emit function's value is null, but the result from GetView("beers", "beer") is getting the value perfectly???

Please help me out !

like image 211
Phillip Avatar asked Aug 23 '13 04:08

Phillip


1 Answers

In couchbase usally view's result set is built in background. If you have for example 1 million documents each 4Kb size without any views it take ~4Gb on disk. When you create a view with map function like

function(doc, meta) {
   emit(doc.name, doc);
}

As a result it take additional 4Gb on disk for view results because view results are stored separately. And in most cases (if you query view with param Stale=Ok) couchbase returns result from that "precompiled" set of records, couchbase doesn't scan all docs on each query.

So emmiting null in map functions is used to prevent disk space usage and it also increase speed of indexing process.

Now the second question about couchbase magic when "result from GetView("beers", "beer") is getting the value perfectly". Couchbase get(key) and getMulti(keys) operations are very fast. So when you query view that emits null it returns not only nulls, also it returns document ids. Then you can manually use getMulti for that array of document ids to get doc's value or in some SDKs there is query param called IncludeDocs that will do the same automatically.

like image 156
m03geek Avatar answered Dec 09 '22 19:12

m03geek