CBLite 1.4.1 PhoneGap REST API filtered pull replication

I have an existing working application that uses channels to sync data from a CouchBase database (5.0.1) using SyncGateway (1.5.1) to a Cordova application built with the Couchbase-Lite-PhoneGap-Plugin . The number of objects is some channels is causing performance issues (342K objects). I was trying to see if I could use filtered replication on the pull sync to reduce the number of objects without having to change the Channel architecture.

I was following the documentation at https://wiki.apache.org/couchdb/Replication#Filtered_Replication and How to set a push replication filter with REST api in Couchbase Lite?.

I cannot seem to get it to work. Some questions.

  1. How do I create the filter object - In CouchBase or using the Sync gateway API? Is this a regular object or a CouchBase View?
  2. Should this work?

I tried creating it based on the documentation but I get the following error.
400 Unknown filter; try sync_gateway/bychannel or _doc_ids

I created a CouchBase object with a ID of _design/syncFilter and a document body of the following.

{
“filters”: {
“accountFilter”: “function(doc, req) { if(doc.ObjectType != ‘Building’) {return true;} else { var include = false; if(req.query.accounts && req.query.accounts.length > 0) {var l = req.query.accounts.length;for (var x = 0; x < l; x++) {if(req.query.accounts[x] === doc.AccountID) {include = true;Break;}}} return (include);}}”
}
}

You’re trying to create a pull filter. Sync Gateway doesn’t support those; they’re not scalable (since they rely on running a JavaScript function on the server once for every document in the database, per user that pulls.) That’s why SG adds the concept of channels, which perform the same filtering but in a much more efficient way.

Check out the documentation on channels and sync functions. Then you can define a channel for the subset of documents you want the client to have, and specify that channel name to the CBL replicator.

Thanks for the quick response - I suspected as much. The documentation should probably be clearer on how this works.

The documentation should probably be clearer

Your initial post says you were reading Apache CouchDB documentation. That’s not our product and not our documentation. :slight_smile: If you have suggestions about our docs, there is a feedback button at the bottom of every page that you can use.

Your are correct - I didn’t even notice that - the product names are so similar and sometimes the APIs match up so sometimes it is confusing - sorry about that. I think what happened was I started on https://developer.couchbase.com/documentation/mobile/current/references/couchbase-lite/rest-api/index.html#/server/post__replicate and saw the “filter” option in the _replicate post body which seemed to indicate you could specify a filter function and started to google it to find more information which led me to the other pages. I will post some feedback to the documentation page. Thanks for your help.