-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `search_string` field is removed - text search vector for `content` and `torrent_contents` is generated in code with improvements in normalisation and tokenisation - the slow `LIKE` query is removed from text search - a query DSL is implemented which translates the input search string to a Postgres tsquery - a CLI command (`reindex`) is provided for updating the tsv for pre-existing records - the unused `tsv` and `search_string` fields are removed from the `torrents` table See #89
- Loading branch information
Showing
33 changed files
with
1,173 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package reindexcmd | ||
|
||
import ( | ||
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy" | ||
"github.com/bitmagnet-io/bitmagnet/internal/database/dao" | ||
"github.com/bitmagnet-io/bitmagnet/internal/database/fts" | ||
"github.com/bitmagnet-io/bitmagnet/internal/model" | ||
"github.com/schollz/progressbar/v3" | ||
"github.com/urfave/cli/v2" | ||
"go.uber.org/fx" | ||
"go.uber.org/zap" | ||
"gorm.io/gen" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
type Params struct { | ||
fx.In | ||
Dao lazy.Lazy[*dao.Query] | ||
Logger *zap.SugaredLogger | ||
} | ||
|
||
type Result struct { | ||
fx.Out | ||
Command *cli.Command `group:"commands"` | ||
} | ||
|
||
func New(p Params) (Result, error) { | ||
return Result{Command: &cli.Command{ | ||
Name: "reindex", | ||
Usage: "Reindex all records for full text search", | ||
Action: func(ctx *cli.Context) error { | ||
println("reindexing...") | ||
d, err := p.Dao.Get() | ||
if err != nil { | ||
return err | ||
} | ||
contentCount := int64(0) | ||
torrentContentCount := int64(0) | ||
if result, err := d.Content.WithContext(ctx.Context).Count(); err != nil { | ||
return err | ||
} else { | ||
contentCount = result | ||
} | ||
if result, err := d.TorrentContent.WithContext(ctx.Context).Count(); err != nil { | ||
return err | ||
} else { | ||
torrentContentCount = result | ||
} | ||
contentBar := progressbar.Default(contentCount, "[1/2] reindexing content") | ||
batchSize := 1000 | ||
tsvs := make(map[model.ContentRef]fts.Tsvector) | ||
var contentResult []*model.Content | ||
if err := d.Content.WithContext(ctx.Context).Preload( | ||
d.Content.Attributes.RelationField, | ||
d.Content.Collections.RelationField, | ||
).FindInBatches(&contentResult, batchSize, func(tx gen.Dao, _ int) error { | ||
for _, c := range contentResult { | ||
c.UpdateTsv() | ||
c.Collections = nil | ||
c.Attributes = nil | ||
tsvs[c.Ref()] = c.Tsv | ||
} | ||
if err := tx.Clauses( | ||
clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "type"}, {Name: "source"}, {Name: "id"}}, | ||
DoUpdates: clause.AssignmentColumns([]string{"tsv", "updated_at"}), | ||
}, | ||
).CreateInBatches(&contentResult, batchSize); err != nil { | ||
return err | ||
} | ||
_ = contentBar.Add(len(contentResult)) | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
_ = contentBar.Finish() | ||
torrentContentBar := progressbar.Default(torrentContentCount, "[2/2] reindexing torrent content") | ||
var torrentContentResult []*model.TorrentContent | ||
if err := d.TorrentContent.WithContext(ctx.Context).Preload( | ||
d.TorrentContent.Torrent.RelationField, | ||
d.TorrentContent.Torrent.Files.RelationField, | ||
).FindInBatches(&torrentContentResult, batchSize, func(tx gen.Dao, _ int) error { | ||
for _, tc := range torrentContentResult { | ||
ref := tc.EntityReference() | ||
if ref.Valid { | ||
tsv, ok := tsvs[ref.Val] | ||
if !ok { | ||
p.Logger.Warnw("missing tsv", "ref", ref.Val) | ||
continue | ||
} else { | ||
tc.Content.Tsv = tsv | ||
} | ||
} | ||
tc.UpdateTsv() | ||
tc.Torrent = model.Torrent{} | ||
tc.Content = model.Content{} | ||
} | ||
if err := tx.Clauses( | ||
clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "id"}}, | ||
DoUpdates: clause.AssignmentColumns([]string{"tsv", "updated_at"}), | ||
}, | ||
).CreateInBatches(torrentContentResult, batchSize); err != nil { | ||
return err | ||
} | ||
_ = torrentContentBar.Add(len(torrentContentResult)) | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
_ = torrentContentBar.Finish() | ||
return nil | ||
}, | ||
}}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.