It seems not matter what I do I have to query the view twice to get the latest docs. Is there caching going on somewhere?
irb(main):008:0> TestDoc.by_user_id.to_a.count
=> 5
irb(main):009:0> TestDoc.create(id: "testing123")
=> #<TestDoc id: testing123, created_at: 2015-03-23 09:14:47 -0400, type: "txn", updated_at: 2015-03-23 09:14:47 -0400>
### I can wait 5 minutes here and it does not make a difference
irb(main):010:0> TestDoc.by_user_id.to_a.count
=> 5
irb(main):011:0> TestDoc.by_user_id.to_a.count
=> 6
To give you a speedier response, view queries run against “stale” indexes by default. Couchbase then re-runs the view to refresh the index after it gives you the response.
At this point, Couchbase now re-runs the view to rebuild the index … and your new document is now in the index as you see when you re-run your query below.
You can get around this by specifying stale=false in your query. That’ll make Couchbase re-run the view before it returns the result, and of course there’ll be a performance impact.