Node.js coach DB使用regex搜索body
一般来说,我对CouchDB、Map/Reduce和NoSQL都很陌生 我希望你们能指导我如何在Node.js/CouchDB应用程序上实现非常基本的搜索 我正在搜索CouchDB文档中的某些文本元素 我在coach中的大多数文档的格式与下面提到的类似:Node.js coach DB使用regex搜索body,node.js,couchdb,Node.js,Couchdb,一般来说,我对CouchDB、Map/Reduce和NoSQL都很陌生 我希望你们能指导我如何在Node.js/CouchDB应用程序上实现非常基本的搜索 我正在搜索CouchDB文档中的某些文本元素 我在coach中的大多数文档的格式与下面提到的类似: { "_id": "b001729a2c5cf4100ea50a71ec04e20b", "_rev": "1-8928ea122d80fd5c1da1c22dfc6ce46e", "Approved To Publis
{
"_id": "b001729a2c5cf4100ea50a71ec04e20b",
"_rev": "1-8928ea122d80fd5c1da1c22dfc6ce46e",
"Approved To Publish": "false",
"Article Body": "has retained the Carbon Trust Standard for energy and carbon management.Following a demanding audit process that took place in December 2012, was awarded recertification for the Carbon Trust Standard.This certificate is a mark of excellence in recognition of how well we measure, manage and reduce our carbon emissions.",
"Headline": "Delight retains Carbon Trust Standard"
}
我的搜索键是“碳信任”、“排放”、“卓越认可”等
我使用的是一个临时映射函数,我在POST请求中的Node.js应用程序的请求体中使用它,但我确信这不是正确的方法,我希望它是CouchDB中的存储视图
我的地图功能:
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);
}
}
}
请让我知道我需要做什么来改进我的方法,或者如果事情不清楚,请让我知道
关于您可以构建一个视图,其中键是关键字(或者任何单词),值是id 缺点是,该视图可能会变得非常大。 CouchDB专家可能有更好的解决方案,我认为这是一个典型的问题 天真的例子1: 然后,您可以使用
/app/\u design/app/\u view/search?key=“keyword”
进行查询
1:实际上,您需要规范化大小写、删除标点符号、常用词,如a、the、of等…a可以访问querystring值,因此您只需添加一个与视图结合使用的值即可
映射函数
列表函数
当然,您可以修改list函数,将数据分块发送,而不是一次全部发送,但这应该让您了解这样的解决方案是什么样子的。这个视图确实变得非常大,不过感谢您的回复。
function(doc) {
if ('Headline' in doc) {
for (key in doc.Headline.split(' ')) {
emit(key, doc._id)
}
}
if ('Article Body' in doc) {
for (key in doc['Article Body'].split(' ')) {
emit(key, doc._id)
}
}
}
function (doc) {
if ("Headline" in doc) {
emit(doc.Headline, doc);
}
}
function (head, req) {
var rows = [],
regex = new RegExp(req.query.search, "i"), // use the querystring param to create the RegExp object
row;
while (row = getRow()) {
if (row.value.Headline.search(regex) > -1 || row.value["Article Body"].search(regex)) {
rows.push(row);
}
}
// I'm just mocking up what a view's output would look like (not required)
send(JSON.stringify({
total_rows: rows.length,
offset: 0,
rows: rows
}));
}