在Meteor应用程序中实现MongoDB 2.4的全文搜索

Sac*_*cha 38 javascript search mongodb meteor

我正在考虑将全文搜索添加到Meteor应用程序.我知道MongoDB现在支持这个功能,但我对这个实现有一些问题:

  • textSearchEnabled=true在Meteor应用程序中启用文本搜索功能()的最佳方法是什么?
  • 有没有办法db.collection.ensureIndex()在你的应用程序中添加一个index()?
  • 如何db.quotes.runCommand( "text", { search: "TOMORROW" } )在Meteor应用程序中运行Mongo命令(即)?

由于我的目标是将搜索添加到Telescope,我正在寻找一种"即插即用"的实现,它需要最少的命令行魔法,甚至可以在Heroku或*.meteor.com上工作.

ElD*_*Dog 27

没有编辑任何Meteor代码的最简单方法是使用你自己的mongodb.你mongodb.conf应该看起来像这样(在Arch Linux上找到它/etc/mongodb.conf)

bind_ip = 127.0.0.1
quiet = true
dbpath = /var/lib/mongodb
logpath = /var/log/mongodb/mongod.log
logappend = true
setParameter = textSearchEnabled=true
Run Code Online (Sandbox Code Playgroud)

关键是setParameter = textSearchEnabled=true,正如其所述,它启用了文本搜索.

开始mongod

告诉meteor mongod通过指定MONGO_URL环境变量来使用你自己的.

MONGO_URL="mongodb://localhost:27017/meteor" meteor
Run Code Online (Sandbox Code Playgroud)

现在说你有一个叫做Dinosaurs声明的集合collections/dinosaurs.js

Dinosaurs = new Meteor.Collection('dinosaurs');
Run Code Online (Sandbox Code Playgroud)

要为集合创建文本索引,请创建文件 server/indexes.js

Meteor.startUp(function () {
    search_index_name = 'whatever_you_want_to_call_it_less_than_128_characters'

    // 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.
    Dinosaurs._dropIndex(search_index_name);

    Dinosaurs._ensureIndex({
        species: 'text',
        favouriteFood: 'text'
    }, {
        name: search_index_name
    });
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过a Meteor.method,例如在文件中公开搜索server/lib/search_dinosaurs.js.

// Actual text search function
_searchDinosaurs = function (searchText) {
    var Future = Npm.require('fibers/future');
    var future = new Future();
    Meteor._RemoteCollectionDriver.mongo.db.executeDbCommand({
        text: 'dinosaurs',
        search: searchText,
        project: {
          id: 1 // Only take the ids
        }
     }
     , function(error, results) {
        if (results && results.documents[0].ok === 1) {
            future.ret(results.documents[0].results);
        }
        else {
            future.ret('');
        }
    });
    return future.wait();
};

// Helper that extracts the ids from the search results
searchDinosaurs = function (searchText) {
    if (searchText && searchText !== '') {
        var searchResults = _searchEnquiries(searchText);
        var ids = [];
        for (var i = 0; i < searchResults.length; i++) {
            ids.push(searchResults[i].obj._id);
        }
        return ids;
    }
};
Run Code Online (Sandbox Code Playgroud)

然后,您只能发布在'server/publications.js'中搜索过的文档

Meteor.publish('dinosaurs', function(searchText) {
    var doc = {};
    var dinosaurIds = searchDinosaurs(searchText);
    if (dinosaurIds) {
        doc._id = {
            $in: dinosaurIds
        };
    }
    return Dinosaurs.find(doc);
});
Run Code Online (Sandbox Code Playgroud)

客户端订阅看起来像这样 client/main.js

Meteor.subscribe('dinosaurs', Session.get('searchQuery'));
Run Code Online (Sandbox Code Playgroud)

Timo Brinkmann的道具,其音乐抓取项目是大多数这方面知识的来源.

  • @StephanTual Thimo Brinkmann指出在Google网上论坛中使用MongoInternals.defaultRemoteCollectionDriver().mongo.db.executeDbCommand https://groups.google.com/d/msg/meteor-talk/x9kYnO52Btg/YaWrYLZSKJQJ (2认同)

She*_*eel 2

要创建文本索引并尝试像这样添加,我希望这样如果仍然存在问题评论,它将很有用

来自docs.mongodb.org


将标量索引字段附加到文本索引,如下例所示,该示例在用户名上指定升序索引键:

db.collection.ensureIndex( { comments: "text",
                             username: 1 } )
Run Code Online (Sandbox Code Playgroud)

警告 您不能包含多键索引字段或地理空间索引字段。

使用文本中的project选项仅返回索引中的字段,如下所示:

db.quotes.runCommand( "text", { search: "tomorrow",
                                project: { username: 1,
                                           _id: 0
                                         }
                              }
                    )
Run Code Online (Sandbox Code Playgroud)

注意:默认情况下,_id 字段包含在结果集中。由于示例索引不包含 _id 字段,因此您必须在项目文档中显式排除该字段。

  • -1 用于从 [docs.mongodb.org](http://docs.mongodb.org/manual/tutorial/return-text-queries-using-only-text-index) 复制和粘贴,无需引用。RTFM 为 +50。恕我直言,问题仍然没有答案。缺少流星背景。 (4认同)