3

I have an CouchDB database named t-customers. Using Fauxton I've created the following view t-customers/_design/t-cust-design/_view/by-custdes. Here is the map function:

function (doc) {
  var custname = doc.CUSTNAME;
  if(custname != undefined && custname.length != undefined && custname.length != ''){
    for(var i = 0; i < custname.length - 1; i++)
      for(var j = i + 1; j < custname.length + 1; j++)
          emit(custname.substring(i, j),doc._id);
  }
}

The view will contain all available sub-strings for custdes (e.g. custdes=abc -> a, ab, abc, bc) as key and doc._id as its value.

After the view is created I can query it with the following http requests:

  • http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="ab"
  • http://127:0.0.1:5984/t-customers/_design/t-cust-design/_view/by-custdes?key="abc"

It works as fast as lightning although my view has about 1.500.000 documents indexed.

First of all: I've noticed that the PouchBD syncs only the t-customers database and not it's view. Why? To make the view avaliable in PouchDB it requires for me to run the following command which takes up to 20 minutes to complete:

t-customers.query("t-cust-design/by-custdes").then(...).catch(...);

Only and only then I can see the view at IndexedDB in Chrome.

Second of all: What is the way to look up a doc in PouchDB view t-cust-design/by-custdes without triggering the whole map/reduce process every time I want to find the ab key? As I mentioned I can query the CouchDB _design/t-cust-design/_view/by-custdes view with http request and it works fast, but I'm unable to do the equivalent action using PouchDB API.

I've read tons of documentation but I'm still confused about it...

Community
  • 1
  • 1
altgov3en
  • 1,100
  • 2
  • 18
  • 33
  • When you're making the t-customers.query(), is this a local PouchDB instance or a PouchDB pointing to CouchDB ? – Alexis Côté Mar 08 '17 at 02:45
  • I think it's a local PouchDB instance. I'm using PouchDB API to trigger the query() function. I assume the function will be executed on local PouchDB database which is linked to its CouchDB database "twin". – altgov3en Mar 08 '17 at 05:57
  • I mean, have you create your db locally `new PouchDB("dbName")` or remotely `new PouchDB("remote ip")` – Alexis Côté Mar 08 '17 at 23:17

1 Answers1

1

The answer to your second question first (well, sort of):

To avoid generating all possible combinations of characters (leading to 1.500.000 view results), emit only whole words and query for keys starting with your query string. You can use the startkey and endkey parameters for that.

function(doc) {
  var custname = doc.CUSTNAME || '';
  for(var i = 0; i < custname.length - 1; i++) {
    emit(custname.substring(i)); // You don't need to emit the _id - it's available on each view row automatically.
  }
}

Query the view with the parameters {startkey:'ab', endkey:'ab\ufff0'}. See the CouchDB docs for details.

Regarding your first question: Views are always built per CouchDB and PouchDB instance. One of the reasons is that you can do filtered replications, so each instance might have its own view of the "world" a.k.a. database contents.

I assume from your comments that you use PouchDB to replicate your database into the browser and then call the view locally, so technically you are using two instances of the database, which makes total sense if you're creating an offline-first app. But if you expect the CouchDB instance to always be available, just query the CouchDB server, so the views don't have to be rebuilt in each user's browser.

Bernhard Gschwantner
  • 1,547
  • 11
  • 12
  • 1
    Another reason not to synchronize the indexes themselves, even if they had the same data, is that the on-disk format is entirely different, so Couch's index wouldn't be of any use to Pouch anyway. – Jonathan Hall Mar 13 '17 at 21:22