Search code examples
couchdbpouchdb

Does CouchDB unconditionally rebuild indices when design docs are updated?


Does CouchDB rebuild an index when a design document has been 'updated' without any change to its map or reduce functions?

EG, will the following code cause 5 re-indexes of the database,

const db = new pouch('some-database-url');
const ddoc = {
  _id: '_design/findStuff',
  views: {
    'findStuff': {
      map: `function(doc) { 
             if (doc._id.indexOf('stuff') === 0) {
               emit(doc._id, doc);
             }
            }`
          }
  }
}
    
for (let i=0; i<5; i++) {
  db.get(ddoc._id).then( d => {
    db.put( {
      ...ddoc,
      _rev: d._rev
    })
  })
}

or does CouchDB make a comparison with the previous versions and decide not to bother?

It's not immediately obvious to me how to test this because the re-indexing is a background process whose status I don't know how to check, and because it results in the same built index.


Solution

  • Yes... and not exactly.

    Changing the rev on a ddoc triggers an index rebuild, but that rebuild happens on read, not on update.

    That is to say, index updates happen the first time the index is consulted, after a change (whether that change be brought on by a ddoc update, or by changing the documents in the database).