Class: Groupy::EntityGrouper
- Inherits:
-
Object
- Object
- Groupy::EntityGrouper
- Defined in:
- lib/rugroupy/group.rb
Constant Summary collapse
- @@defaultScoreFunction =
'function(tag) { return 1; }'
- @@defaultIncludeFunction =
'function(tag) { return true; }'
- @@dynamicTagFunction =
'function(doc) {}'
Instance Method Summary collapse
- #count_entities(scoreFunction) ⇒ Object
- #group(options = {}) ⇒ Object
-
#initialize(database, entity) ⇒ EntityGrouper
constructor
A new instance of EntityGrouper.
- #invert_entities(includeFunction, dynamicTagFunction) ⇒ Object
- #similiar(tag = nil, skip = nil, limit = nil, reverse = false) ⇒ Object
Constructor Details
#initialize(database, entity) ⇒ EntityGrouper
Returns a new instance of EntityGrouper.
10 11 12 13 14 15 16 17 |
# File 'lib/rugroupy/group.rb', line 10 def initialize(database, entity) @database = database @entity = entity @database["#{@entity}_count"].indexes.create_many([ { key: { '_id.tag' => -1 }, background: false }, { key: { 'value.count' => -1 }, background: false } ]) end |
Instance Method Details
#count_entities(scoreFunction) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rugroupy/group.rb', line 38 def count_entities(scoreFunction) map = BSON::Code.new(<<eos) function() { var score = #{scoreFunction}, tag = this._id.tag, tagScore = score(tag), entities = this.value.entities.slice(0).sort(); for (x in entities) { for (y in entities) { if (x < y) { emit({tag:tag, e:[entities[x], entities[y]]}, {count:tagScore}); emit({e:[entities[x], entities[y]]}, {count:tagScore}); } } } } eos reduce = BSON::Code.new(<<eos) function(key, values) { var result = {count:0}; values.forEach(function(value) { result.count += value.count; }); return result; } eos @database["#{@entity}_invert"].find.map_reduce(map, reduce, out: "#{@entity}_count").execute nil end |
#group(options = {}) ⇒ Object
32 33 34 35 36 |
# File 'lib/rugroupy/group.rb', line 32 def group( = {}) invert_entities([:includeFunction] || @@defaultIncludeFunction, [:dynamicTagFunction] || @@dynamicTagFunction) count_entities([:scoreFunction] || @@defaultScoreFunction) end |
#invert_entities(includeFunction, dynamicTagFunction) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rugroupy/group.rb', line 70 def invert_entities(includeFunction, dynamicTagFunction) map = BSON::Code.new(<<eos) function() { var include = #{includeFunction}, dynamicTagFunction = #{dynamicTagFunction}, entity_id = this._id; if (this.tags) { for (tag in this.tags) { if (!include(tag)) continue; this.tags[tag].forEach(function(z) { emit({tag:tag, value:z}, {entities: [entity_id]}); }); } dynamicTagFunction(this); } } eos reduce = BSON::Code.new(<<eos) function(key, values) { var result = {entities:[]}; values.forEach(function(value) { value['entities'].forEach(function(entity_id) { result['entities'].push( entity_id ); }); }); return result; } eos @database[@entity].find.map_reduce(map, reduce, out: "#{@entity}_invert").execute nil end |
#similiar(tag = nil, skip = nil, limit = nil, reverse = false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rugroupy/group.rb', line 19 def similiar(tag = nil, skip = nil, limit = nil, reverse = false) q = BSON::Document.new q['_id.tag'] = tag ? tag : { '$exists' => false } opts = { projection: { '_id.e' => 1 }, sort: { 'value.count' => reverse ? 1 : -1 } } opts[:skip] = skip if skip opts[:limit] = limit if limit cursor = @database["#{@entity}_count"].find(q, opts) cursor.collect { |r| r['_id']['e'] } end |