Couchbase indexing based on MapReduce function

index

#1

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


#2

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.


#3

@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: