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

标签 javascript mongodb search meteor

我正在考虑向 Meteor 应用程序添加全文搜索。我知道 MongoDB 现在支持此功能,但我对实现有一些疑问:

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

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

最佳答案

不编辑任何 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

关键行是 setParameter = textSearchEnabled=true,正如它所说,它启用文本搜索。

启动mongod

通过指定 MONGO_URL 环境变量,告诉 meteor 使用你的 mongod 而不是它自己的。

MONGO_URL="mongodb://localhost:27017/meteor" meteor

现在假设您在 collections/dinosaurs.js

中声明了名为 Dinosaurs 的集合
Dinosaurs = new Meteor.Collection('dinosaurs');

要为集合创建文本索引,请创建文件 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
    });
});

然后您可以通过 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;
    }
};

然后你可以只发布已经在'server/publications.js'中搜索过的文档

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

客户端订阅在 client/main.js

中看起来像这样
Meteor.subscribe('dinosaurs', Session.get('searchQuery'));

Prop Timo Brinkmann谁的musiccrawler project是大部分知识的来源。

关于javascript - 在 Meteor 应用程序中实现 MongoDB 2.4 的全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17159626/

相关文章:

java - Glassfish 和 MongoDB 连接错误 : NoClassDefFoundError

mongodb - 从展开的数组中获取最大值

javascript - 分配随机数,避免 JavaScript 中某个范围内的数字被阻塞

javascript - 用php编写客户端重定向服务器端

C++ Mongodb 驱动程序 v2.2 scons 在 Linux 上安装失败

javascript - 在 MixedCollection 中搜索项目

algorithm - 自定义二进制搜索

mongodb - 在 NOSQL 数据库中搜索 GPS 点的最佳方法

javascript - 从 WinDBG 的 JavaScript 脚本访问文件系统

javascript - 为 google.visualization.datatable 创建高级 KnockOut 绑定(bind)处理程序