Search code examples
filteringcouchbasecouchbase-viewnosql

How "keys" and "key" filters should be used in Couchbase to filter view results?


I have issue with key and keys filter parameter in Couchbase (Version: 3.0.0 Enterprise Edition) in the web console. Whatever value I set in those parameters, no record is returned.

Documents look like:

{
   "folder": "F3",
   "ccy": "USD",
   "pnl": 789700,
   "maturity": "4424-10-16 00:00 AM CEST",
   "source": "BackOffice1",
   "npv": 341684,
   "symbolic_id": 880888,
   "bpv": 374000,
   "cpty": "CPTY2"
}

Map function is:

function (doc, meta) {

 emit([doc.source,doc.cpty], doc.npv); 

}

Reduce is the built in function

_count

I assume that I should be able to get all documents with the key ["BackOffice1","CPTY2"] by setting the key parameter in the querystring ?key=["BackOffice1","CPTY2"]&reduce=true&group=true. But nothing is returned.

I may miss something in how we should use key and keys parameters.

For information, startkey and endkey work as expected.

Is there something wrong with my approach ?


Solution

  • I figured out what was the issue with the key parameter. In the Couchbase version 3.0.0, we need to add inclusive_end=true parameter to get results.

    Looking at Couchbase bugs, it looks like it was not required in previous versions and that it will be reverted back in next version, refer to https://www.couchbase.com/issues/browse/MB-12378.

    After reviewing samples in Couchbase documentation, my usage of keys parameter was not correct. To query view with array as a key, keys parameter should be formatted as follow: keys=[["value1",value2"],["value3",value2"]].

    If I apply this to the example I provided in my question, the query will look like: ?keys=[["BackOffice1","CPTY2"]] or key=["BackOffice1","CPTY2"]

    Thanks