Use aggregations to calculate tags
This commit is contained in:
parent
b3fd8aabe2
commit
706fc88a61
1 changed files with 25 additions and 13 deletions
38
mapreduce.go
38
mapreduce.go
|
@ -10,7 +10,7 @@ func GetTags(numTags int, tagsColl *mgo.Collection) ([]string, error) {
|
||||||
var result []struct {
|
var result []struct {
|
||||||
Tag string "_id"
|
Tag string "_id"
|
||||||
}
|
}
|
||||||
err := tagsColl.Find(nil).Sort("-value").Limit(numTags).All(&result)
|
err := tagsColl.Find(nil).Sort("-count").Limit(numTags).All(&result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -55,18 +55,30 @@ func NewMR(database *mgo.Database) *MR {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MR) UpdateTags(booksColl *mgo.Collection) error {
|
func (m *MR) UpdateTags(booksColl *mgo.Collection) error {
|
||||||
var mr mgo.MapReduce
|
var tags []struct {
|
||||||
mr.Map = `function() {
|
Tag string "_id"
|
||||||
if (this.subject) {
|
Count int "count"
|
||||||
this.subject.forEach(function(s) { emit(s, 1); });
|
}
|
||||||
}
|
err := booksColl.Pipe([]bson.M{
|
||||||
}`
|
{"$project": bson.M{"subject": 1}},
|
||||||
mr.Reduce = `function(tag, vals) {
|
{"$unwind": "$subject"},
|
||||||
var count = 0;
|
{"$group": bson.M{"_id": "$subject", "count": bson.M{"$sum": 1}}},
|
||||||
vals.forEach(function() { count += 1; });
|
{"$sort": bson.M{"count": -1}},
|
||||||
return count;
|
{"$limit": TAGS_DISPLAY},
|
||||||
}`
|
}).All(&tags)
|
||||||
return m.update(&mr, bson.M{"active": true}, booksColl, TAGS_COLL)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tagsColl := m.database.C(TAGS_COLL)
|
||||||
|
tagsColl.DropCollection()
|
||||||
|
for _, tag := range tags {
|
||||||
|
err = tagsColl.Insert(tag)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MR) UpdateMostVisited(statsColl *mgo.Collection) error {
|
func (m *MR) UpdateMostVisited(statsColl *mgo.Collection) error {
|
||||||
|
|
Reference in a new issue