The easiest way is to use list functions. However, this is equivalent to full scan operation:
function(head, req){
var filter = function(key){
if (!req.query.q){
return key; // list all
}
if (!req.query.q.match('^[\d-]+$'){
return; // don't allow regex injections
}
var match = key.match('.*' + req.query.q + '.*');
if (match) return match[0];
}
start({'headers': {'Content-Type': 'text/plain'}});
var num = null;
while(row=getRow()){
num = filter(row.key);
if (num){
send(num + '\n');
}
}
}
Also define view that emits phone numbers for contacts:
function(doc){
if(doc.type == 'contact'){emit(doc.phone_num, null)}
}
And call with view that emits only contacts like: /db/_design/contacts/_list/search/phone?q=23
The fastest way is to use couchdb-lucene.
First, create index ddoc:
{
"_id":"_design/contacts",
"fulltext": {
"by_phone": {
"index":"function(doc) { var ret=new Document(); ret.add(doc.phone_num); return ret }"
}
}
}
And query it like http://localhost:5984/db/_fti/_design/contacts/by_phone?q=23