Sorry for novice question. I'm trying to query Cloudant db with sample code (taken from official gitHub cloudant repository):
To query using the index, use the `.find()` method.
~~~ js
db.find({selector:{name:'Alice'}}, function(er, result) {
if (er) {
throw er;
}
console.log('Found %d documents with name Alice', result.docs.length);
for (var i = 0; i < result.docs.length; i++) {
console.log(' Doc id: %s', result.docs[i]._id);
}
});
~~~
I need to search through all document in the db and to improve this query performance I created Search Index (especially for field "name"). But how do I define in the code that I want to use the index and which one if there are some?
The _find
API endpoint, which backs the .find()
method will use the index automatically, if it can. Sometimes, it can't -- typically because the actual query doesn't actually match the index. It's worth reviewing the documentation for Cloudant Query for better understanding of this mechanism.
It is possible to specify the actual index for the query to use via the parameter use_index
-- but I haven't checked if this is implemented in the node.js client, but in most cases it shouldn't be necessary.
There is another useful API endpoint called _explain that will show you the indexes found, or if the query falls back on a full db scan (which you want to avoid!).
Note that when you're using _find
(.find()
), you are actually using something called Cloudant Query, as opposed to Cloudant Search. The distinction is important, as Cloudant Search is a different query mechanism that is on offer.