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.
12 13 14 15 16 |
# File 'lib/rugroupy/group.rb', line 12 def initialize(database, entity) @database, @entity = database, entity @database["#{@entity}_count"].ensure_index([['_id.tag', Mongo::DESCENDING], ['value.count', Mongo::DESCENDING]], :background => false) end |
Instance Method Details
#count_entities(scoreFunction) ⇒ Object
34 35 36 37 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 |
# File 'lib/rugroupy/group.rb', line 34 def count_entities(scoreFunction) map = BSON::Code.new(<<eos) function() { 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) { result = {count:0}; values.forEach(function(value) { result.count += value.count; }); return result; } eos @database["#{@entity}_invert"].map_reduce(map, reduce, :out => "#{@entity}_count") nil end |
#group(options = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/rugroupy/group.rb', line 28 def group(={}) self.invert_entities([:includeFunction] || @@defaultIncludeFunction, [:dynamicTagFunction] || @@dynamicTagFunction) self.count_entities([:scoreFunction] || @@defaultScoreFunction) end |
#invert_entities(includeFunction, dynamicTagFunction) ⇒ Object
66 67 68 69 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 |
# File 'lib/rugroupy/group.rb', line 66 def invert_entities(includeFunction, dynamicTagFunction) map = BSON::Code.new(<<eos) function() { 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) { result = {entities:[]}; values.forEach(function(value) { value['entities'].forEach(function(entity_id) { result['entities'].push( entity_id ); }); }); return result; } eos @database[@entity].map_reduce(map, reduce, :out => "#{@entity}_invert") nil end |
#similiar(tag = nil, skip = nil, limit = nil, reverse = false) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/rugroupy/group.rb', line 18 def similiar(tag=nil, skip=nil, limit=nil, reverse=false) q = BSON::OrderedHash.new q["_id.tag"] = tag ? tag : {"$exists" => false} cursor = @database["#{@entity}_count"].find(q, :fields => {"_id.e" => 1}) cursor.skip(skip) if skip cursor.limit(limit) if limit cursor.sort("value.count", reverse ? Mongo::ASCENDING : Mongo::DESCENDING) cursor.collect { |r| r["_id"]["e"] } end |