4

I'm working on a android messagerie app, messages are stored in a CouchDB(Apache) database on the internet. How can I pull messages with filter on my Android devices?

Android Snippet:

Replication pull = new Replication(messageDB, messageUrl, Replication.Direction.PULL);

//filter
pull.setFilter("message/by_username");
HashMap<String, Object> filterParams = new HashMap<>();
filterParams.put("username", usr);
pull.setFilterParams(filterParams);

pull.setContinuous(false); //oneshot
pull.start();

Apache design document:

{
  "_id": "_design/message",
  "views": {
    "by_username": {
      "map": "function (doc) {\n  emit([doc.username]);\n  \n}"
    }
  },
  "filters": {
    "by_username": "function(doc, req){ if(!doc.username) return false; if(req.query.username && req.query.username != doc.username){return false} else {return true}}"
  },
  "language": "javascript"
}

With the block filter, the synchronization will never stop but the database is always empty (I did not find any document after 3 minutes).

Without the block filter, all messages are downloaded in only few seconds. Thanks.

Anh-Tuan Mai
  • 1,129
  • 19
  • 36
  • Can you try to add a document with a specific username (eg: `foo` in lowercase). Then, in your Java code, filter on the hardcoded value `foo`). If it doesn't work, try to use your filter function directly in the CouchDB interface and replicate to another db to see if the problems comes from your filter. – Alexis Côté Nov 13 '17 at 14:15
  • How can i use my filter function directrly in the CouchDB interface? I can do the CouchDB replication with filter but I have to add my arguments in the body of the request. And from CouchBase, I can synchronize with a url but I did not found any methods for adding arguments in the replication object. – Anh-Tuan Mai Nov 13 '17 at 14:41
  • I only know the method to replicate a CouchDB with filter which can be found in this link: https://wiki.apache.org/couchdb/Replication `{"source":"http://example.org/example-database","target":"http://admin:password@127.0.0.1:5984/example-database", "filter":"myddoc/myfilter", "query_params": {"key":"value"}}` – Anh-Tuan Mai Nov 13 '17 at 14:46
  • If you try it with curl, it works as expected? – Alexis Côté Nov 13 '17 at 15:54
  • I did do the test with Postman and it works : by posting a request, the data is replicated with filter, but the problem here is in the current version of CouchBase lite (for Android) I cannot make it work. – Anh-Tuan Mai Nov 13 '17 at 16:07
  • For example if I set url `http://example.org/example-database` for my replication object, all the database will be synchronized. Are there a url for the filter as `http://example.org/example-database/my-filter-name?my-key=a-value` to pull only the filtered data? – Anh-Tuan Mai Nov 13 '17 at 16:11
  • And have you tried with an hardcoded value? Maybe the `usr` variable isn't what you expect. – Alexis Côté Nov 13 '17 at 19:58
  • I tried it all. In addition, the synchronization should finish at least. But it never finish and the database is always empty. – Anh-Tuan Mai Nov 14 '17 at 15:21
  • 2
    Couchbase lite has a [known bug when using a filter function with CouchDB](https://developer.couchbase.com/documentation/mobile/current/guides/couchbase-lite/native-api/replication/index.html#filtered-pull-from-couchdb-pouchdb-or-cloudant). It seemd that Couchbase lite updated its filter function to POST and CouchDB is expecting a GET. You can read through the linked github issue for possible work arounds. – Hypnic Jerk Nov 14 '17 at 16:16
  • Can I have somethings like : Android (CouchbBase lite) <-> Sync Gateway (channel) <-> CouchDB (Apache) for pulling with filter? I found a topic (in 2014) saying this is not possible but it was 3 years ago. – Anh-Tuan Mai Nov 17 '17 at 08:27

2 Answers2

0

Source : https://developer.couchbase.com/documentation/mobile/current/guides/couchbase-lite/native-api/replication/index.html#filtered-pull-from-couchdb-pouchdb-or-cloudant

Filtered pull from CouchDB, PouchDB or Cloudant

Since Couchbase Lite 1.2, filter functions in pull replications with non-Couchbase databases are no longer available. There is an incompatibility in the way the filter parameter is handled in the POST /{db}/_changes request (see #1139).

Anh-Tuan Mai
  • 1,129
  • 19
  • 36
0

There is however a workaround for working with couchbase lite 1.4 with couchdb 2.x server to working without any change in the library as mentioned above. Please refer to answer given here

Fenil
  • 1,194
  • 10
  • 11