Search code examples
javascriptnode.jsnosqlcloudant

How to build cloudant index once desing document created?


I'm creating an search index programmatically by code below. The question is how I build the index once I insert the design document? Or will it be build first time used by .search() method?

var book_indexer = function(doc) {
  if (doc.author && doc.title) {
    // This looks like a book.
    index('title', doc.title);
    index('author', doc.author);
  }
}

var ddoc = {
  _id: '_design/library',
  indexes: {
    books: {
      analyzer: {name: 'standard'},
      index   : book_indexer
    }
  }
};

db.insert(ddoc, function (er, result) {
  if (er) {
    throw er;
  }

  console.log('Created design document with books index');
});

Solution

  • The creation of indexes happens automatically once you insert a (correctly formed) design document. No further action is required. The actual index is built incrementally on document create, update and delete rather than on index query.

    If you change the design doc, all its indexes will be rebuilt, which can take a bit of time if you have a lot of data.