Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couchbase View not update immediately?

I just follow this post to test Couchbase View.When edit view use Couchbase GUI and save,the view didn't update immediately.

For example.With php scripts store some array to Couchbase.And define a named 'last' view in 'dev_sessions' document:

function (doc) {
if(doc.namespace == 'sessions') {
emit(doc.lastSeen, 1);
}
}

Then use curl to get the json result. First time:

$ curl
http://192.168.228.134:8092/default/_design/dev_sessions/_view/last/?group_level=1&reduce=true'
{"rows":[ ] }

Second:

$ curl 'http://192.168.228.134:8092/default/_design/dev_sessions/_view/last/?group_level=1&reduce=true'
{"rows":[
{"key":1352872218,"value":1},
{"key":1352879418,"value":3}
]
}

Another test,add a new array in php scripts:

'eb255262434407766f212d1b6f23' => array(
'namespace' => 'sessions',
"type" => "user",
"userID" => "1107",
'lastSeen' => time(),
'firstSeen' => time(),
"remoteAddress" => "2.3.4.5",
"location" => "Vienna/Austria",
"name" => "Golden K"

Run curl again,first time:

$ curl 'http://192.168.228.134:8092/default/_design/dev_sessions/_view/last/?group_level=1&reduce=true'
{"rows":[
{"key":1352872218,"value":1},
{"key":1352879418,"value":3}
]
}

Second time:

$ curl 'http://192.168.228.134:8092/default/_design/dev_sessions/_view/last/?group_level=1&reduce=true'
{"rows":[
{"key":1352875163,"value":1},
{"key":1352882363,"value":4}
]
}

If changed data or view script,click Views - 'Show Results' button in Couchbase GUI,the first & second time does different. Why View not update immediately?

like image 272
qkboy Avatar asked Nov 14 '12 08:11

qkboy


1 Answers

This is the expected behavior. Couchbase is by default "updating" the index after the call of the view. And this to have faster response time.

When executing a view you can control the status of the view (in fact its index) using the stale parameter.

In your case just do a:

curl 'http://192.168.228.134:8092/default/_design/dev_sessions/_view/last/?group_level=1&reduce=true&stale=false'

And it will update the index before returning the data to your application.

You can find in the documentation all the information about this parameter and how the views are working:

http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-stale.html

like image 173
Tug Grall Avatar answered Sep 28 '22 00:09

Tug Grall