regex - Caseless matching using views in Cloudant -
i have database of documents have following data structure:
{ "_id": "sampleid", "_rev": "sample-rev", "added": "2014-09-09 01:05:32", "cached": 1, "subject": "sample topic", "mode": "<samplemode>", "protected": 0, "added_by": "myname", "factoid": "sample factoid" }
and have following view:
function(doc) { if (doc.subject && doc.factoid && doc.mode){ emit(doc.subject, doc.factoid); } }
i need retrieve docs "subject" matches provided key. non case sensitive. post give me of matches want, if case matches.
https://<username>.cloudant.com/<db>/_design/<designdoc>/_view/<view>?include_docs=true { "keys" : ["<subject>"] }
i have tried search indexes without success. api mentions regex operator haven't been able working. seems simple thing, how should approaching this?
regarding $regex approach, here i'm at. following post works previous attempts @ returning case-sensitive results.
https://<username>.cloudant.com/<db>/_find { "selector": { "subject": {"$eq": "<subject>"} }, "fields": ["_id", "_rev", "subject", "factoid"] }
substituting $regex operator $eq yields following error:
{ "error": "no_usable_index", "reason": "there no operator in selector can used index." }
the reference material function rather slim. mentioned on page: https://docs.cloudant.com/api/cloudant-query.html
one option create secondary index stores uppercased search string:
function(doc) { if (doc.subject){ emit(doc.subject.touppercase()); } }
the application can uppercase search criteria before sending database:
https://username.cloudant.com/database/_design/designdoc/_view/view?key=“sample topic"&include_docs=true
will result in this:
{ "offset": 0, "rows": [ { "doc": { "_id": "sampleid", "_rev": "1-d77a468b74497771f8d37130a7cf02eb", "added": "2014-09-09 01:05:32", "added_by": "myname", "cached": 1, "factoid": "sample factoid", "mode": "<samplemode>", "protected": 0, "subject": "sample topic" }, "id": "sampleid", "key": "sample topic", "value": null } ], "total_rows": 1 }
Comments
Post a Comment