Full-text search with Meteor.js and MongoDB

后端 未结 4 1104
[愿得一人]
[愿得一人] 2021-02-06 05:32

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

相关标签:
4条回答
  • 2021-02-06 05:46

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

    0 讨论(0)
  • 2021-02-06 05:55

    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.

    0 讨论(0)
  • 2021-02-06 06:03

    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/

    0 讨论(0)
  • 2021-02-06 06:11

    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
    
    0 讨论(0)
提交回复
热议问题