Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | SDKs | SDKs

view with array key started to behave differently in HTTP request and PHP API.

7 replies [Last post]
  • Login or register to post comments
Sun, 12/16/2012 - 01:52
olegsv
Offline
Joined: 10/03/2012
Groups: None

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

Top
  • Login or register to post comments
Sun, 12/16/2012 - 10:23
ingenthr
Offline
Joined: 03/16/2010
Groups:

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.

Top
  • Login or register to post comments
Mon, 12/17/2012 - 02:36
olegsv
Offline
Joined: 10/03/2012
Groups: None

    /** 
     * 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"
    }
  }
}

Top
  • Login or register to post comments
Mon, 12/17/2012 - 19:45
mnunberg
Offline
Joined: 04/25/2012
Groups: None

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.

Top
  • Login or register to post comments
Fri, 01/04/2013 - 21:39
biswajit
Offline
Joined: 03/28/2012
Groups: None

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?

Top
  • Login or register to post comments
Sun, 01/06/2013 - 01:00
olegsv
Offline
Joined: 10/03/2012
Groups: None

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 ) )

biswajit wrote:
Sorry I am not sure where to post thishttp://www.couchbase.com/forums/thread/composite-key-query-php-sdk....So posting here again....
$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?

Top
  • Login or register to post comments
Mon, 01/07/2013 - 06:30
biswajit
Offline
Joined: 03/28/2012
Groups: None

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...

Top
  • Login or register to post comments
Sun, 03/03/2013 - 05:12
bjones
Offline
Joined: 03/03/2013
Groups: None

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.

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker