Couchbase indexing based on MapReduce function

Hi,
I am trying to understand the indexing of documents based on MapReduce function.
As of the informations provided in the forums, all the documents which matches the condition provided in the map function will be indexed and as an when the the documents get updated index also gets updated (??).

Example: function (doc, meta) {
if(doc.doc_type==“person”){
if(doc.status && doc.status==“OK”){

 emit(null,null)
 }

}
}

Any documents which is emitted will be indexed. If I update the documents with status or doc_type, will the already indexed document is removed from the index…?

Please provide the more insight of the indexing.
Thanks and regards

Hi @girishbhat.m7,

It’s my understanding that basically any mutation of a document will cause it to be run through the map reduce index again. If the document is no longer emitted, then it will no longer be included as part of the index. You can give this a try yourself to make sure it’s behaving how you’d expect:

I created a bucket called “girishbhat”, and I put two small documents into it:

id 1 - { “status”:“OK” }
id 2 - { “status”:“OK” }

documents

I created a view called “statusOk”. Its map function:

function (doc, meta) {
  if(doc.status == "OK") {
	  emit(meta.id, null);
  }
}

And you can see the results of that function is initially both documents 1 and 2:

Next, I updated document 2 to have a status of “ERROR”. I ran the View again, and only document 1 is returned.

Note that Indexes are updated according to a couple factors, including an interval of time and a number of changes, more information on that in the documentation.

@matthew.groves thank you for the reply and the example . We tried similar kind of example before , wanted to confirm that the understanding of indexing concept is correct .
Thanks for dedicating a bucket in my name :wink:

1 Like

@matthew.groves I have another doubt. If a new document is added then how the indexing of new document would be done?

@randomemailec30, from that documentation link above:

  • Updates can be triggered in two ways:
    • At the point of access or query by using the stale parameter.

    • Automatically by Couchbase Server based on the number of updated documents, or the period since the last update. Automatic updates can be controlled either globally, or individually on each design document.

  • Views are updated incrementally. The first time the view is accessed, all the documents within the bucket are processed through the map/reduce functions. Each new access to the view only processes the documents that have been added, updated, or deleted, since the last time the view index was updated.
1 Like

@matthew.groves
What is the indexing method? Does it depend on the key I am emitting?

Also from the documentation:

The index information stored on disk consists of the combination of both the key and value information defined within your view. The key and value data is stored in the index so that the information can be returned as quickly as possible.