Ejemplo n.º 1
0
func GetRandomKeywords(howMany int) ([]Keyword, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	total, err := d.C(KEYWORD_COLLECTION).Count()
	if err != nil {
		return nil, fmt.Errorf("error counting keywords: %v", err)
	}

	toRet := []Keyword{}
	for i := 0; i < howMany; i++ {
		toSkip := rand.Int() % total
		out := &Keyword{}
		err = d.C(KEYWORD_COLLECTION).Find(bson.M{}).Select(bson.M{"kw": 1}).
			Skip(toSkip).One(out)
		if err != nil {
			return nil, fmt.Errorf("error finding keyword: %v", err)
		}
		toRet = append(toRet, *out)
	}

	return toRet, nil
}
Ejemplo n.º 2
0
func (self *Keyword) Insert() error {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	return d.C(KEYWORD_COLLECTION).Insert(self)
}
Ejemplo n.º 3
0
func FindKeyword(word string) (*Keyword, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	keyword := &Keyword{}
	err = d.C(KEYWORD_COLLECTION).Find(bson.M{"kw": word}).One(keyword)
	return keyword, err
}
Ejemplo n.º 4
0
func FindMovie(title string) (*Movie, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	movie := &Movie{}
	err = d.C(MOVIE_COLLECTION).Find(
		bson.M{
			"t": title,
		},
	).One(movie)
	return movie, err
}
Ejemplo n.º 5
0
func FindKeywords(words []string) ([]Keyword, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	keywords := []Keyword{}
	err = d.C(KEYWORD_COLLECTION).Find(
		bson.M{
			"kw": bson.M{
				"$in": words,
			},
		},
	).All(&keywords)
	return keywords, err
}
Ejemplo n.º 6
0
func FindMovies(titles []string) ([]Movie, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	movies := []Movie{}
	err = d.C(MOVIE_COLLECTION).Find(
		bson.M{
			"t": bson.M{
				"$in": titles,
			},
		},
	).All(&movies)
	return movies, err

}
Ejemplo n.º 7
0
func FindMoviesWithPrefix(prefix string) ([]Movie, error) {
	sess, d, err := db.GetFactory().GetSession()
	if err != nil {
		return nil, fmt.Errorf("couldn't get session: %v", err)
	}
	defer sess.Close()

	movies := []Movie{}
	err = d.C(MOVIE_COLLECTION).Find(
		bson.M{
			"t": bson.RegEx{
				Pattern: "^" + prefix,
				Options: "",
			},
		},
	).Limit(20).All(&movies)
	return movies, err
}