本文介绍了在Meteor应用程序中实现MongoDB 2.4的全文本搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking into adding full text search to a Meteor app. I know MongoDB now supports this feature, but I have a few questions about the implementation:

  • 在Meteor应用程序中启用文本搜索功能(textSearchEnabled=true)的最佳方法是什么?
  • 是否可以从您的应用程序内添加索引(db.collection.ensureIndex())?
  • 如何在Meteor应用程序中运行Mongo命令(即db.quotes.runCommand( "text", { search: "TOMORROW" } ))?
  • What's the best way to enable the text search feature (textSearchEnabled=true) in a Meteor app?
  • Is there a way to add an index (db.collection.ensureIndex()) from within your app?
  • How can you run a Mongo command (i.e. db.quotes.runCommand( "text", { search: "TOMORROW" } )) from within a Meteor app?

由于我的目标是将搜索添加到望远镜,因此我正在寻找即插即用"的实现只需很少的命令行魔术,甚至可以在Heroku或* .meteor.com上运行.

Since my goal is to add search to Telescope, I'm searching for a "plug-and-play" implementation that requires minimal command line magic and could even work on Heroku or *.meteor.com.

推荐答案

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

The simplest way without editing any Meteor code is to use your own mongodb. Your mongodb.conf should look something like this (on Arch Linux it is found at /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,正如其指出的那样,它可以进行文本搜索.

The key line is setParameter = textSearchEnabled=true, which, as it states, enables text search.

启动mongod

通过指定MONGO_URL环境变量告诉流星不要使用您的mongod.

Tell meteor to use your mongod not its own by specifying the MONGO_URL environmental variable.

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

现在说您有一个名为Dinosaurs的集合,在collections/dinosaurs.js

Now say you have collection called Dinosaurs declared say in collections/dinosaurs.js

Dinosaurs = new Meteor.Collection('dinosaurs');

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

To create an text index for the collection create a file 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中.

Then you can expose the search through a Meteor.method, for example in the file 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"中搜索过的文档.

Then you can publish only documents that have been searched for in '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'));

支持 Timo Brinkmann musiccrawler项目 a>是大多数此类知识的来源.

Props to Timo Brinkmann whose musiccrawler project was the source of most this knowledge.

这篇关于在Meteor应用程序中实现MongoDB 2.4的全文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:57