cloudant / sync-android

A JSON-based document datastore for Android applications
Apache License 2.0
267 stars 91 forks source link

Is pull replication using design doc views supported? #613

Closed h1771t closed 3 years ago

h1771t commented 3 years ago

Bug Description

Cannot do a pull replication using views. As per: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-using-views

Using PullFilter to try get data from a view in a design doc, does not work. It appears that views must have a specific url format which seems to not be supported by this lib yet?

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

Cannot see code to be able to do a pull replication using view

2. What you expected to happen

I expect to be able do a pull replication using views.

3. What actually happened

Nothing.

Environment details

rnewson commented 3 years ago

Replication and views are distinct features of cloudant/couchdb. It is not possible to replicate using a view. You can achieve a similar effect using replication with a filter function or selector.

h1771t commented 3 years ago

See last bullet point here: https://pouchdb.com/api.html#filtered-replication

How would I pull replicate a design doc with a view, where for eg: design doc id is: "_id": "_design/filterByTimestamp", and view function is: "views": { "now-minus-one-month": { "map": "function (doc)...

What syntax would I use for filter function or selector to query such? Thanks in advance.

h1771t commented 3 years ago

This seems to work, thanks so much for your suggestion.

val filter = PullFilter("_view", mapOf("view" to "filterByTimestamp/now-minus-one-month")) val replicator = ReplicatorBuilder.pull().from(uri).to(ds).filter(filter).build()

rnewson commented 3 years ago

hm be careful there. replication is not using the view index, it's just re-running the map function and letting docs through that would emit a row. It's better to write a filter function (https://docs.couchdb.org/en/stable/ddocs/ddocs.html?highlight=filter#filter-functions) or a selector (https://docs.couchdb.org/en/stable/api/database/find.html#find-selectors). A selector would be faster too.

A view called "now-minus-one-month" also makes me think you've made a mistake in your map function. If you are comparing a document property to the current date (i.e, new Date()) then you have made a mistake. The map function in your view is called at indexing time not querying time, so it will return the wrong answers.