/
index.go
53 lines (47 loc) · 1.21 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package commands
import (
"github.com/ms900ft/movielite"
"github.com/ms900ft/movielite/models"
"github.com/prometheus/common/log"
"github.com/urfave/cli"
)
// StartCommand is used to register the start cli command
var IndexCommand = cli.Command{
Name: "index",
Usage: "reindex fulltext",
//Flags: startFlags,
Action: indexAction,
}
var indexFlags = []cli.Flag{
// cli.BoolFlag{
// Name: "detach-server, d",
// Usage: "detach from the console (daemon mode)",
// },
}
// startAction start the web server and initializes the daemon
func indexAction(ctx *cli.Context) error {
conf := movielite.GetConfig(ctx.GlobalString("config"))
dbc := models.DBConfig{DBName: conf.DataBase}
db := models.ConnectDataBase(dbc)
tx := db.Begin()
defer tx.Close()
rows, err := tx.Model(&models.Movie{}).Rows()
if err != nil {
log.Error(err)
}
defer rows.Close()
for rows.Next() {
var movie models.Movie
var new models.Movie
db.ScanRows(rows, &movie)
if err := tx.Set("gorm:auto_preload", true).Where("id = ?", movie.ID).First(&new).Error; err != nil {
log.Error(err)
}
log.Debugf("indexing %s", movie.Title)
if err := new.FullTextIndex(tx); err != nil {
log.Warn(err)
}
}
tx.Commit()
return nil
}