Implemented ( not so great ) search for PostgreSQL
This commit is contained in:
parent
096777e2da
commit
791e53c84f
@ -198,7 +198,6 @@ func (db *postgresDatabase) QueryTorrents(
|
|||||||
lastOrderedValue *float64,
|
lastOrderedValue *float64,
|
||||||
lastID *uint64,
|
lastID *uint64,
|
||||||
) ([]TorrentMetadata, error) {
|
) ([]TorrentMetadata, error) {
|
||||||
|
|
||||||
if query == "" && orderBy == ByRelevance {
|
if query == "" && orderBy == ByRelevance {
|
||||||
return nil, fmt.Errorf("torrents cannot be ordered by relevance when the query is empty")
|
return nil, fmt.Errorf("torrents cannot be ordered by relevance when the query is empty")
|
||||||
}
|
}
|
||||||
@ -216,8 +215,23 @@ func (db *postgresDatabase) QueryTorrents(
|
|||||||
, total_size
|
, total_size
|
||||||
, discovered_on
|
, discovered_on
|
||||||
, (SELECT COUNT(*) FROM files WHERE torrents.id = files.torrent_id) AS n_files
|
, (SELECT COUNT(*) FROM files WHERE torrents.id = files.torrent_id) AS n_files
|
||||||
FROM torrents {{ if not .FirstPage }}
|
{{ if .QueryExists }}
|
||||||
WHERE {{.OrderOn}} {{GTEorLTE .Ascending}} {{.LastOrderedValue}} {{ end }}
|
, similarity(name, '{{ .Query }}') * -1 as relevance
|
||||||
|
{{ else }}
|
||||||
|
, 0
|
||||||
|
{{ end }}
|
||||||
|
FROM torrents
|
||||||
|
{{ if not .FirstPage }}
|
||||||
|
WHERE
|
||||||
|
{{ if not .QueryExists }}
|
||||||
|
{{.OrderOn}}
|
||||||
|
{{ else }}
|
||||||
|
similarity(name, '{{ .Query }}') * -1
|
||||||
|
{{ end }}
|
||||||
|
{{GTEorLTE .Ascending}} {{.LastOrderedValue}}
|
||||||
|
{{ if .QueryExists }} AND {{ end }}
|
||||||
|
{{ end }}
|
||||||
|
{{ if and .QueryExists .FirstPage }} WHERE {{ end }}{{ if .QueryExists }} to_tsvector(replace(name, '.', ' ')) @@ plainto_tsquery('{{ .Query }}') {{ end }}
|
||||||
ORDER BY {{.OrderOn}} {{AscOrDesc .Ascending}}
|
ORDER BY {{.OrderOn}} {{AscOrDesc .Ascending}}
|
||||||
LIMIT {{.Limit}};
|
LIMIT {{.Limit}};
|
||||||
`, struct {
|
`, struct {
|
||||||
@ -226,12 +240,16 @@ func (db *postgresDatabase) QueryTorrents(
|
|||||||
Ascending bool
|
Ascending bool
|
||||||
Limit uint
|
Limit uint
|
||||||
LastOrderedValue *float64
|
LastOrderedValue *float64
|
||||||
|
QueryExists bool
|
||||||
|
Query string
|
||||||
}{
|
}{
|
||||||
FirstPage: firstPage,
|
FirstPage: firstPage,
|
||||||
OrderOn: orderOn(orderBy),
|
OrderOn: orderOnPostgreSQL(orderBy),
|
||||||
Ascending: ascending,
|
Ascending: ascending,
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
LastOrderedValue: lastOrderedValue,
|
LastOrderedValue: lastOrderedValue,
|
||||||
|
QueryExists: query != "",
|
||||||
|
Query: query,
|
||||||
}, template.FuncMap{
|
}, template.FuncMap{
|
||||||
"GTEorLTE": func(ascending bool) string {
|
"GTEorLTE": func(ascending bool) string {
|
||||||
if ascending {
|
if ascending {
|
||||||
@ -266,7 +284,7 @@ func (db *postgresDatabase) QueryTorrents(
|
|||||||
&torrent.Size,
|
&torrent.Size,
|
||||||
&torrent.DiscoveredOn,
|
&torrent.DiscoveredOn,
|
||||||
&torrent.NFiles,
|
&torrent.NFiles,
|
||||||
//&torrent.Relevance,
|
&torrent.Relevance,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -277,6 +295,25 @@ func (db *postgresDatabase) QueryTorrents(
|
|||||||
return torrents, nil
|
return torrents, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func orderOnPostgreSQL(orderBy OrderingCriteria) string {
|
||||||
|
switch orderBy {
|
||||||
|
case ByRelevance:
|
||||||
|
return "relevance"
|
||||||
|
|
||||||
|
case ByTotalSize:
|
||||||
|
return "total_size"
|
||||||
|
|
||||||
|
case ByDiscoveredOn:
|
||||||
|
return "discovered_on"
|
||||||
|
|
||||||
|
case ByNFiles:
|
||||||
|
return "n_files"
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("unknown orderBy: %v", orderBy))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (db *postgresDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) {
|
func (db *postgresDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) {
|
||||||
rows, err := db.conn.Query(`
|
rows, err := db.conn.Query(`
|
||||||
SELECT
|
SELECT
|
||||||
|
Loading…
Reference in New Issue
Block a user