son*_*xqt 2 full-text-search mongodb meteor
我正在使用MeteorJS与MongoDB相关联来创建全文搜索功能,我所做的是我按照这里的步骤操作:http://meteorpedia.com/read/Fulltext_search,我的搜索功能现在有点"正在工作" .
以下是我的一些重要代码:
server/zip-index.js文件:
Meteor.startup(function () {
var search_index_name = 'my_search_index';
// Remove old indexes as you can only have one text index and if you add
// more fields to your index then you will need to recreate it.
Zips._dropIndex(search_index_name);
Zips._ensureIndex({
city: 'text',
state: 'text'
}, {
name: 'my_search_index'
});
});
Run Code Online (Sandbox Code Playgroud)
server/lib/search_zips.js文件
var _searchZips = function (searchText) {
var Future = Npm.require('fibers/future');
var future = new Future();
MongoInternals.defaultRemoteCollectionDriver().mongo.db.executeDbCommand({
text: 'zips',
search: searchText,
project: {
id: 1 // Only return the ids
}
}
, function(error, results) {
if (results && results.documents[0].ok === 1) {
var x = results.documents[0].results;
future.return(x);
}
else {
future.return('');
}
});
return future.wait();
};
Run Code Online (Sandbox Code Playgroud)
现在的问题是:说,我有一个文件name = Washington, state = DC
.
然后,当我用search key ="Washington"提交时,它返回所有文件name = Washington
; 但是当我提交搜索键="正在洗涤"时,它什么都不返回!
所以我怀疑MongoDB的全文搜索要求搜索关键字与文档的字段值完全相同吗?你们可以帮助我改进我的搜索功能,以便它仍然使用MongoDB的全文搜索,但是如果我提交完整的搜索键,它能够返回文档事件吗?
我一直坚持这几个小时.希望你们能提供帮助.非常感谢你!
MongoDB全文搜索的工作原理是将所有字符串拆分为单个单词(使用基于索引语言的一些词干).这意味着您只能搜索完整的单词,并且无法执行任何模糊搜索.
如果要搜索单词片段,可以使用正则表达式进行搜索.但请记住,正则表达式不能使用文本索引(但是当正则表达式以begin-of-string(^
)标记开头时,它们在某些情况下可以限制使用普通索引).
例如,查询db.Zips.find({ name: /^Washing/ }
将查找名称以其开头的所有文档,"Washing"
并将从索引中受益{ name: 1 }
.您还可以使用db.Zips.find({ name: /DC/ }
查找名称包含"DC"
在任何位置的所有文档,但它不会从任何索引中受益,并且需要执行完整的集合扫描.
当您需要更高级的文本搜索功能时,您应该考虑将MongoDB与Lucene等专用解决方案配对.
归档时间: |
|
查看次数: |
916 次 |
最近记录: |