【问题标题】:MeteorJS - MongoDB - Why is Full Text Search only returns the exact matches?MeteorJS - MongoDB - 为什么全文搜索只返回完全匹配?
【发布时间】:2015-01-11 14:42:57
【问题描述】:

我正在使用与 MongoDB 关联的 MeteorJS 来创建 全文搜索 功能,我所做的是按照此处的步骤操作: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'
    });
});

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();
};

现在的问题是:比如说,我有一个带有name = Washington, state = DC 的文档。

然后,当我使用搜索key = "Washington" 提交时,它返回所有带有name = Washington 的文档;但是当我只提交搜索 key = "Washing" 时,它什么也不返回!

所以我怀疑 MongoDB 的 全文搜索 要求搜索键与文档的字段值完全相同?你们能帮我改进一下我的搜索功能,让它仍然使用 MongoDB 的全文搜索,但如果我提交不完整的搜索键,它能够返回文档事件吗?

我已经坚持了好几个小时了。希望各位大侠能帮忙。非常感谢您!

【问题讨论】:

    标签: mongodb meteor full-text-search


    【解决方案1】:

    MongoDB full text search 通过将所有字符串拆分为单个单词(使用基于索引语言的一些词干)来工作。这意味着您只能搜索完整的单词,不能进行任何模糊搜索。

    当你想搜索单词片段时,你可以search with a regular expression。但请记住,正则表达式不能使用文本索引(但在某些情况下,当正则表达式以字符串开头 (^) 标记开头时,它们可以有限地使用普通索引)。

    例如,查询db.Zips.find({ name: /^Washing/ } 将查找名称以"Washing" 开头的所有文档,并将受益于{ name: 1 } 上的索引。您还可以使用db.Zips.find({ name: /DC/ } 在任何地方查找名称包含"DC" 的所有文档,但它不会受益于任何索引,并且需要执行完整的集合扫描。

    当您需要更高级的文本搜索功能时,您应该考虑将 MongoDB 与 Lucene 等专门的解决方案配对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多