5

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?

Community
  • 1
  • 1
qkboy
  • 208
  • 3
  • 7
  • If changed data or view script,only the first time get result from one view is not update.After the second time run,this view and other all views look will update sync.The result is corrent. – qkboy Nov 14 '12 at 09:08

1 Answers1

12

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

Tug Grall
  • 3,410
  • 1
  • 14
  • 16
  • THX.It's clear.BTW,Couchbase Forums's spam block is too strict for asian IP.I already changed three IPs,from China to HK,but still can't submit a new post.%>_<% – qkboy Nov 15 '12 at 03:25
  • Not sure that it related to the IP address but probably due to the number of links you put in your message. I will raise that to the Couchbase Community Manager – Tug Grall Nov 15 '12 at 06:55