view with array key started to behave differently in HTTP request and PHP API.
Views with startkey and endkey defined suddenly stopped working in the latest CB API.
A call to view() with startkey and endkey defined result to the following error
array(2) {
["error"]=> string(17) "query_parse_error"
["reason"]=> string(91) "No rows can match your key range, reverse your start_key and end_key or set descending=true"
}
While calling the view with the same parameters from http request, everything works as expected
http://localhost:8092/mybucketname/_design/mydesigndoc/_view/myview?startkey=["user"]&endkey=["user__"]&group=true
The problem appeared with 64bit couchbase community edition under Ubuntu 12.04 64bit, C libraries installed from libcouchbase-2.0.1_ubuntu1110_amd64.tar , PHP API from php-ext-couchbase-1.1.0-ubuntu1110-x86_64.tar.gz ( reinstalled version of 14th Dec.)
The view was working correctly in couchbase developer preview DP5 / 32bit CB / Ubuntu 12.04 32 bit.
---
Oleg
/** * create 25 document, each document will be emitted twice for sake of testing * */ function simulate_view_issue() { $DOC_CODE = 'user_event'; for( $v=1;$v<=25;$v++ ) { $key = 'test_doc_'.$v; $doc = array( 'type' => 'testevent', 'code' => $DOC_CODE, 'code1' => 'notinview', 'count' => $v, 'uid' => $v, 'random' => rand(1,100), 'local' => date( 'Y-m-d\TH:i:s\Z' ) ); $this->cbconn->set( $key, json_encode( $doc ), 3600 ); // set expiration - dont pollute cb } } function retrieve_view() { $DOC_CODE = 'user_event'; $viewkeys = array( 'startkey'=>sprintf('["%s"]' , $DOC_CODE ), 'endkey' =>sprintf('["%s__"]', $DOC_CODE ), 'group'=>'true', ); $res = $this->cbconn->view( 'debug', 'user2hour', $viewkeys ); echo "\nAccessing with API"; var_dump($res); if( empty( $res['rows'] )) { echo "\nNo rows found ."; } echo "\nAccessing with REST"; $url = 'http://localhost:8092/default/_design/debug/_view/user2hour?'; $url .= http_build_query( $viewkeys ); echo "\n URL will work: $url"; }
There's a design document, use curl to upload it
{"views": {
"user2hour":
{"map": "
\n function (doc, meta ) {
\n /* user2hour emits: [ event,user_id,bid,Y,M,D,Hr ]*/
\n if( meta.type=='json' && doc.type=='testevent' ) {
\n var d = dateToArray( doc.local );
\n emit( [ doc.code, doc.uid, doc.bid,d[0], d[1], d[2], d[3] ], 1 );
\n if( doc.code1>'' ) {
\n emit( [ doc.code+'_'+doc.code1, doc.uid, doc.bid, d[0], d[1], d[2], d[3] ], 1 );
\n }
\n }
\n }
\n ",
"reduce": "_sum"
}
}
}Hi, I believe the default behavior has changed between DP and beta.
Specifically, startkey and endkey are now by default serialized to their respective types and are quoted;
so if your old code would do:
array("startkey" => sprintf('["%s","%s"]', $something, $else));
you can now just do
array("startkey" => array($something, $else));
and it will be serialized into the proper datatype. This is the default; the change was made between DP and beta, or during one of the DP releases, and was done because it had been requested; and forcing people to manually encode things into JSON was a bit inelegant :).
If you wish to have the old behavior (i.e. no automatic conversion, but a strict usage of literal string scalars) you should set the COUCHBASE_OPT_VOPTS_PASSTHROUGH to true; like so:
https://github.com/couchbase/php-ext-couchbase/blob/07105126780d751b3e0f...
You may also use the utility method viewGenQuery() to see how your URI is sent to the server; this is useful for debugging the view options.
Sorry I am not sure where to post thishttp://www.couchbase.com/forums/thread/composite-key-query-php-sdk....So posting here again....
I have a view which i have to query with keys. its happening from admin console but not with PHP SDK. Can you please tell me what am i doing wrong here? Code example...
Admin URL which is working
http://URL:PORT/bucket/_design/DDocument/_view/viewName?startkey=[2012,11]&endkey=[2012,12]&group=true&group_level=2&reduce=true&stale=update_after&connection_timeout=60000&limit=10&skip=0
But not with these php call
1. $couchbaseObject->view('DDocument','viewName',array('startkey'=>array('2012','11'),'endkey'=>array('2012','12'),'stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));
2. $couchbaseObject->view('DDocument','viewName',array('startkey'=>'[2012,11]','endkey'=>'[2012,12]','stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));
3. $couchbaseObject->view('DDocument','viewName',array('startkey'=>rawurlencode('[2012,11]'),'endkey'=>rawurlencode('[2012,12]'),'stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));
Where i am doing wrong?
biswajit, your first example is almost correct. Try to convert the key values to integers, and arguments to strings...
$couchbaseObject->view('DDocument','viewName',array('startkey'=>array(2012,11),'endkey'=>array(2012,12),'stale'=>'update_after','group'=>'true','group_level'=>2,'reduce'=>'true'));
Also you can use debug with excellent viewGenQuery() call
echo urldecode( $couchbaseObject->viewGenQuery( .... your view args ) )
$couchbaseObject->view('DDocument','viewName',array('startkey'=>array('2012','11'),'endkey'=>array('2012','12'),'stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));2.
$couchbaseObject->view('DDocument','viewName',array('startkey'=>'[2012,11]','endkey'=>'[2012,12]','stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));3.
$couchbaseObject->view('DDocument','viewName',array('startkey'=>rawurlencode('[2012,11]'),'endkey'=>rawurlencode('[2012,12]'),'stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));Where i am doing wrong?
Thanks a lot......
Can you explain something,
When I am doing this
$couchbaseObject->view('DDocument','viewName',array('startkey'=>array(2012,11),'endkey'=>array(2012,12),'stale'=>'update_after','group'=>true,'group_level'=>2,'reduce'=>true));
Why the key [2012,11] is missing from the result? What am i getting is only [2012,12]. But My expectation was [2012,11],[2012,12]
Thanks again...
This was very helpful. I struggled with this for a few days till I found this post. Here's an example of my work calling a view with a string range.
$opt = array('stale' => 'update_after',
'skip' => '0',
'startkey' => array("MyString"),
'endkey' => array("MyString\u02ad"),
'group' => 'true', //we are grouping this data
'group_level' => '2'); //Grouping it to two levels in the key
$result = $cb->view(DESIGN_DOC, VIEW, $opt);//Let's call the view.
Can you drop in an example of using the view() function that isn't working? I think I see what you're saying, but I want to be sure we're both trying the same thing.