Sha256: e51cc6620a352cf56f31acdeca0ef04ba3a9522a761d0307c984bff8499ed56f

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

Contents

function filter(collection, q, config) {
  var results = [];
  var counter = 0;

  var fields = {_keywords: 1};
  for(var i in config.select) {
    fields[config.select[i]] = 1;
  }

  var time = new Date().getTime();
  db[collection].find(q, fields).limit(500).forEach(
    function(doc) {
      var score = 0.0;
      for(var i in config.words) {
        var word = config.words[i];
        if(doc._keywords.indexOf(word) != -1 )
          score += 15.0;
      }

      for(var i in config.stemmed) {
        var word = config.stemmed[i];
        if(doc._keywords.indexOf(word) != -1 )
          score += (1.0 + word.length);
      }

      if(score >= config.min_score || 1.0 ) {
        delete doc._keywords;
        results.push({'score': score, 'doc': doc});
        counter += 1;
      }
    }
  );

  var sorted = results.sort(function(a,b) {
    return b.score - a.score;
  });

  time = (new Date().getTime() - time);

  return {total_entries: counter, elapsed_time:  time, results: sorted.slice(0, config.limit||500)};
}

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mongoid_ext-0.6.2 lib/mongoid_ext/js/filter.js
mongoid_ext-0.6.1 lib/mongoid_ext/js/filter.js
mongomapper_ext-0.5.2 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.5.1 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.5.0 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.4.0 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.3.0 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.2.4 lib/mongomapper_ext/js/filter.js
mongomapper_ext-0.2.3 lib/mongomapper_ext/js/filter.js