View auto update semantics


I have a view setup within a design document that I setup with it’s own auto update options. The options are as follows:

  "options" : {
      "updateInterval" : 60000,
      "updateMinChanges" : 1,
      "replicaUpdateMinChanges" : 1

I have been querying this view with stale=ok to see the behaviors. My expectation was that if a single document changed within a minute (absolutely) then the index was updated. If I queried the view within that absolute 1 minute interval, I would see nothing until that 1 minute fully passed after which I would then see the change. What I have seen instead is that the very moment one new document is added, the index rebuilds. If I add a new document and wait 5 seconds and then add another, the index will rebuild twice even though a full minute has yet to pass between the first and second changes. It seems that update interval window is short circuited the very moment the update min changes has been met and this ran counter to my expectation.

So, is there any good way to make the index rebuild consistently every minute on a fixed interval as long as at least one document has changed?



What version of CB are you running this against? The expected behavior as you’re describing is correct. Also, what sdk are you using to query and update?


I’m testing locally using Couchbase 3.0.0 Community Edition and the Java SDK 2.1.1


I suspect what is happening is using the stale=ok flag, each time you query the view to see if the item is there, the view is being updated immediately after each query. The behavior semantics for view consistency changed slightly with CB 3.0 and can you provide the sample code for your java query?


Ya, give me a few to draft up a self contained example (in Scala) using the Java client to show you how I am able to reproduce the issue.


Okay, here is a very simple example where I start with an empty bucket and search it, add a document, search again, add a second document and do one last search. All searches are done with Stale=True. I set up the view with the following auto update options (10 minutes, 1 doc):

  "options" : {
    "updateInterval": 600000,
    "updateMinChanges" : 1,
    "replicaUpdateMinChanges" : 1

The code is as follows:

  val cluster = CouchbaseCluster.create()
  val bucket = cluster.openBucket("clicks", password)  
  val query = ViewQuery.from("all", "allIds").stale(Stale.TRUE)
  var results = bucket.query(query)
  println(s"First call to query view returned: ${results.totalRows()} rows")
  val doc = JsonDocument.create("foo", JsonObject.fromJson("""{"foo": 1}"""))
  Thread.sleep(5000) //sleeping to let the document get persisted to disk
  results = bucket.query(query)
  println(s"Second call to query view returned: ${results.totalRows()} rows")  
  val doc2 = JsonDocument.create("bar", JsonObject.fromJson("""{"bar": 1}"""))
  Thread.sleep(5000) //sleeping to let the document get persisted to disk  
  results = bucket.query(query)
  println(s"Third call to query view returned: ${results.totalRows()} rows")  

What gets printed out is as follows:

First call to query view returned: 0 rows    
Second call to query view returned: 1 rows
Third call to query view returned: 2 rows

Now it’s possible that in between adding the first document and sleeping that the view auto updated, but after adding the second doc, I’m not expecting it to show that second doc in the view results until after the 10 minute window expires.


I dare to quote myself:


@vmx, this makes sense now. It was not immediately clear from the documentation that the update interval itself could not be controlled at the individual design document level. That also explains why my 5 second pauses in the test code (which match the global update interval) were what made things work. Many thanks. Now that I understand things better and can explain the current behavior I will be able to come up with a solution to my problem.


If you see any way to make the docs clearer, feel free to use the “Feedback On This Page” button at the bottom right corner of the docs.