Search code examples
javascriptnode.jsnosqlcloudant

How make cloudantDb.search return all records instead of limit 25 and bookmark


I'm trying to use search index to make search faster. Here is my code:

return new Promise((resolve, reject) => {
  let conf = {'include_docs': true}
  conf.q = "user: 'new'";

  db.search('app', 'userIndex', conf, (err, body) => {
    if (!err) {
      console.log('success');
      resolve({status: 'Success', response: {userDocs: body.total_rows}});
    } else {
      console.log('error');
      resolve({ status: 'Failure', response: { Error: err } });
    }
  });
});

It works fine but returns just 25 documents (there are hundreds) and bookmark. Is there a way to make search return all documents instead of calling new promise for each bookmark. If I go through all bookmarks it works even slower than just .find() method.


Solution

  • The db.search function makes a query on a Cloudant Search index. Cloudant Search is an Apache Lucene-based indexing system for free-text searching and the results come back in quantities of up to 200 documents per search. If the result set is over 200 documents, a search result set will contain a "bookmark" which can be passed to Cloudant on a subsequent request to get the next block of search results.

    The db.find function makes a query on a Cloudant Query index. Cloudant Query is a general purpose query language similar to MongoDB's query syntax. Search result sets are limited to 25 (by default) but this can be overridden up to a maximum of 200 documents. Again, a bookmark system allows paging through the result set.

    e.g.

    const q = { 
      selector: {
        name: { "$eq": "Brian"},
        age : { "$gt": 25 }
      },
      fields: [ "name", "age", "tags", "url" ]
      limit:200
    };
    mydb.find(q).then((data) => {
      console.log(data);
    });
    

    Larger sets of data can be returned in a single API call by defining MapReduce views which can be queries with large values of "limit" using the db.view function.