node.js - Couch DB use regex to search body -
i pretty new couchdb, map/reduce , nosql in general.
i hoping if guys guide me on how implement basic search on node.js/ couchdb application.
i looking @ searching text elements within couchdb documents.
most of documents within couch of similar format mentioned below :
{ "_id": "b001729a2c5cf4100ea50a71ec04e20b", "_rev": "1-8928ea122d80fd5c1da1c22dfc6ce46e", "approved publish": "false", "article body": "has retained carbon trust standard energy , carbon management.following demanding audit process took place in december 2012, awarded recertification carbon trust standard.this certificate mark of excellence in recognition of how measure, manage , reduce our carbon emissions.", "headline": "delight retains carbon trust standard" }
my search keys eg 'carbon trust', 'emissions', 'excellence recognition' etc.
what have temporary map function use in request body of node.js application in post request, sure not right approach, , expect stored view in couchdb.
my map function:
function (doc) { if ('headline' in doc) { if (doc['article body'].search(/" + req.body.jsonvalue + "/i) !== -1 || doc.headline.search(/" + req.body.jsonvalue + "/i) !== -1) { var key = doc.headline, value = doc; emit(key, value); } } }
please let me know on need improve approach or let me know if things unclear.
regards
a list function has access querystring values, can add 1 use in conjunction view.
map function
function (doc) { if ("headline" in doc) { emit(doc.headline, doc); } }
list function
function (head, req) { var rows = [], regex = new regexp(req.query.search, "i"), // use querystring param create regexp object row; while (row = getrow()) { if (row.value.headline.search(regex) > -1 || row.value["article body"].search(regex)) { rows.push(row); } } // i'm mocking view's output (not required) send(json.stringify({ total_rows: rows.length, offset: 0, rows: rows })); }
of course modify list function emit data in chunks, rather @ once, should give idea of solution looks like.
Comments
Post a Comment