dhtsearch icon indicating copy to clipboard operation
dhtsearch copied to clipboard

Add support multi tags for search query

Open batkor opened this issue 5 years ago • 1 comments

Hi

Problem/Motivation

Not support multi tags for search torrent files

Example

I want get all torrents taged video and hd, I go to this url http://localhost:6880/search?tag=ru,video and see empty result

Proposed resolution

Change query text

sqlTorrentsByTag = `
	select t.id, t.infohash, t.name, t.size, t.seen
	from torrents t
	inner join tags_torrents tt on t.id = tt.torrent_id
	inner join tags ta on tt.tag_id = ta.id
	where ta.name = $1 group by t.id
	order by seen desc
	limit 50 offset $2`
err := DB.Select(&torrents, sqlTorrentsByTag, tag, offset)

to

tags := strings.Split(tag, ",")
	query := `
	select t.id, t.infohash, t.name, t.size, t.seen
	from torrents t
	inner join tags_torrents tt on t.id = tt.torrent_id
	inner join tags ta on tt.tag_id = ta.id
    where ta.name in (`
	// group by t.id
	var where []string
	for _, tag_item := range tags {
		where = append(where, fmt.Sprintf(`'%s'`, tag_item))
	}
	query = query + strings.Join(where, `,`)  + `)
	group by t.id
	HAVING
		COUNT(ta.name) >= $2
	order by seen desc
	limit 50 offset $1`;
err := DB.Select(&torrents, query, offset, strconv.Itoa(len(tags)))

batkor avatar Jul 04 '20 07:07 batkor

Hi, can you create a PR for this?

felix avatar Nov 08 '20 23:11 felix