Full-text search with Meteor.js and MongoDB

爱⌒轻易说出口 提交于 2019-12-20 14:42:51

问题


I am experimenting with Meteor.js and looking for a full-text search engine that can run on Meteor/MongoDB server. It seems that Meteor has not developed this feature.

I wonder if there is an existing solution for adding full-text search to Meteor app.

The only thing I can find is MongoLantern, which has node.js API. How should I go about adding MongoLantern to my Meteor app?


回答1:


MongoDB 2.4 will have a full text search in it. Guide can be found here.

If you are prepared to run the development releases you can download MongoDB 2.3 now - it contains text search.

Update: MongoDB 2.4 has been released. Text search is described as Beta.

The guide for text search is here and the mongod must be run like this

mongod --setParameter textSearchEnabled=true



回答2:


Take a look at http://lunrjs.com/. That might also help in getting the near-instant performance of real Meteor app.




回答3:


You might want to have a look at:

https://github.com/Crenshinibon/spomet

It's a Meteor native package to provide full-text search. It has an easy to include search-box with autocompletion.

You can read a tutorial about an extended example application here:

http://shiggyenterprises.wordpress.com/2013/09/28/developing-a-full-text-search-enabled-meteor-app/




回答4:


There is also another way to implement a solution with Meteor. It's Search Source.

It's a kind of typeahead but without the UI part. It exposes a reactive datasource where you can use it to build the UI with Blaze as you need.

  • Here's a demo app: https://instant-search-demo.meteor.com/

Above app is an instant search app to search Meteor packages. How it can build with search source is documented in this article

In brief this is how search source works:

In client, create a source

var options = {
  keepHistory: 1000 * 60 * 5,
  localSearch: true
};
var fields = ['packageName', 'description'];

PackageSearch = new SearchSource('packages', fields, options);

Then in the server define the search source

SearchSource.defineSource('packages', function(searchText, options) {
  var options = {sort: {isoScore: -1}, limit: 20};

  if(searchText) {
    var regExp = buildRegExp(searchText);
    var selector = {packageName: regExp, description: regExp};
    return Packages.find(selector, options).fetch();
  } else {
    return Packages.find({}, options).fetch();
  }
});

function buildRegExp(searchText) {
  // this is dumb implementation
  var parts = searchText.trim().split(' ');
  return new RegExp("(" + parts.join('|') + ")", "ig");
}

Now get the data source and render it

Template.searchResult.helpers({
  getPackages: function() {
    return PackageSearch.getData({
      transform: function(matchText, regExp) {
        return matchText.replace(regExp, "<b>$&</b>")
      },
      sort: {isoScore: -1}
    });
  }
});

Finally do the search

PackageSearch.search("the text to search");

You can learn more about how each of the above works with from the documentation.



来源:https://stackoverflow.com/questions/14567856/full-text-search-with-meteor-js-and-mongodb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!