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”: “113.211.53.160”,
“Origin-Host”: “seagull.bri-presales-01.csgi.com”,
“Origin-Realm”: “CSG”,
“RSU-CC-Total-Octets”: “1000000”,
“SGSN-Address”: “10.11.12.13”,
“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 :http://158.155.72.127:8092/dmgaudit/_design/audit/_view/By%20Timestamp?startkey=“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}