Query view not returning any results

I am new to the Couchbase Python SDK and am having an issue with retrieving view data.

I can retrieve a row successfully using:

res = cb.get(“SESS_1111_61412333555”)

On the Couchbase server I have design doc “audit” containing view “Postpaid” that returns 10 documents in the results. However when I try to retrieve the documents using the Python SDK I get no results.

view_results = cb.query(design="audit", view="Postpaid") log("results: %r" % view_results) log("view contains %d indexed_rows" % view_results.indexed_rows) log("view contains %d rows_returned" % view_results.rows_returned)

Output is:

results: View<Design=dev_audit, View=Postpaid, Query=Query:’’, Rows Fetched=0>
view contains 0 indexed_rows
view contains 0 rows_returned

Since a get() works I know I am connected to the bucket ok, so what could be the issue?

Do you mind sharing some JSON, View and Python code to help us reproduce this?

Here is an example showing a view query that returns 1 result via the REST API, but 0 results via the Python SDK.
I am successfully using the REST API in lieu of the Python API not working for views.

Here is the sample JSON document in bucket “dmgaudit”:
Doc id: 61412000666_1096298391;1_2
Doc content:
“Request”: {
“3GPP-Charging-Characteristics”: “0”,
“3GPP-Charging-Id”: “1234567890”,
“3GPP-IMSI”: “505021122334460”,
“Acct-Application-Id”: “0”,
“Auth-Application-Id”: “4”,
“Auth-Session-State”: “1”,
“CC-Request-Number”: “1”,
“CC-Request-Type”: “2”,
“Called-Station-Id”: “yesinternet”,
“Destination-Host”: “server.optus.com.au”,
“Destination-Realm”: “Optus”,
“Event-Timestamp”: “2013-11-12 03:26:04”,
“Framed-IP-Address”: “”,
“Origin-Host”: “seagull.bri-presales-01.csgi.com”,
“Origin-Realm”: “CSG”,
“RSU-CC-Total-Octets”: “1000000”,
“SGSN-Address”: “”,
“Session-Id”: “1096298391;1”,
“Subscription-Id-Data”: “61412000666”,
“Subscription-Id-Type”: “0”,
“USU-CC-Total-Octets”: “990000”,
“Vendor-Id”: “11”,
“application_id”: “4”,
“command_code”: “272”,
“end_to_end”: “2015”,
“flags”: “1000”,
" hop_by_hop": “1015”
“Response”: {
“Acct-Application-Id”: “0”,
“Auth-Application-Id”: “4”,
“Auth-Session-State”: “1”,
“CC-Request-Type”: “2”,
“GSU-CC-Total-Octets”: “1000000”,
“Origin-Host”: “optus.com.au”,
“Origin-Realm”: “CSG”,
“Result-Code”: “2001”,
“Session-Id”: “1096298391;1”,
“Vendor-Id”: “11”,
“application_id”: “4”,
“command_code”: “272”,
“end_to_end”: “2015”,
“flags”: “0000”,
" hop_by_hop": “1015”
“Additional”: {
“TargetOCS”: “OCS2”,
“MessageType”: “PREPAID”,
" ResponseValue": “SUCCESS”

The view design doc is “audit” and view name is "By Timestamp"
i.e. dmgaudit -> Views -> _design/audit/_view/By Timestamp

View code:

function (doc, meta) {
emit(meta.id, ["Date: " + doc.Request[“Event-Timestamp”]]);

Python code:

def subscriber_search(msisdn, search_type, start_session):

    log("In subscriber_search (%s, %s, %s)" % (msisdn, search_type, start_session))

    # Make connection object to Couchbase - needed to retrieve the document contents
    sts, errmsg = cb_connect()
    if sts == 0:
            return make_error_doc(errmsg)

    qry_result = cb.query("audit", "By Timestamp", startkey=msisdn, endkey=msisdn + "\\u02ad")
    log("qry_result: %r" % qry_result)

    # Using the MSISDN as the Couchbase key prefix, retrieve all documents for that MSISDN via a view
    view_url = 'http://##IPADDRESS##:8092/dmgaudit/_design/audit/_view/By%%20Timestamp?startkey="%s"&endkey="%s\u02ad"' % (msisdn, msisdn)

    sts, result = get_url(view_url)
    if sts == 0:
            return make_error_doc(result)

    # result is a file-like object, read it and convert to dict type
    result_content = result.read()
    result_dict = eval(result_content.replace('\r\n',''))
    log("url result: %r" % result_dict)

Here is the contents of the debug file, showing the results:

2013-11-13 11:46:01.854212 In subscriber_search (61412000666, history, )
2013-11-13 11:46:01.913010 Connected to Couchbase (dmgaudit)
2013-11-13 11:46:01.913892 qry_result: View<Design=audit, View=By Timestamp, Query=Query:‘startkey=%2261412000666%22&endkey=%2261412000666%5C%5Cu02ad%22’, Rows Fetched=0>
2013-11-13 11:46:01.914143 Opening URL :“61412000666”&endkey="61412000666\u02ad"
2013-11-13 11:46:01.928047 url result: {‘rows’: [{‘value’: [‘Date: 2013-11-12 03:26:04’], ‘id’: ‘61412000666_1096298391;1_2’, ‘key’: ‘61412000666_1096298391;1_2’}], ‘total_rows’: 45777}

Hello - I’m curious if you ever figured anything out on this issue. I’m seeing something similar running some functional tests where I save several documents, query a view using python-sdk with stale=False and then delete the documents and do it again for different inputs. It seems the views will intermittently return no results but I can query the documents directly by key.

@nodsworth which server version are you on? If you are on pre 3.0, you also need to think about “persist to master” on mutating in addition to stale=false on querying, since the indexer picks it up from disk.

@daschl I have had the same results on couchbase 3.0.0 and 3.0.2, both on Ubuntu 14.04 running under virtualbox in a vagrant session on my macbook pro.

I’m going to try to put together a test case this morning just using python couchbase and see if I can recreate it.

Hi Jeremy, I didn’t end up solving it. I had to use the REST API to get the results.

Probably responding to a stale question here, but please keep in mind that the query() method returns an iterator, as such you must actually iterate over the returned object, either via list comprehension or explicitly, to get the view.

Thus the following is wrong

print cb.query('design', 'view')

The following forms are correct:

Use the following forms if you “just want a list of rows”:

rows = [x for x in cb.query('design', 'view')]

or, more traditionally,

for row in cb.query('design', 'view'):
    print row