WIP: improve search #1

Closed
rob wants to merge 26 commits from improve-search into master
2 changed files with 10 additions and 7 deletions
Showing only changes of commit 6be17c7268 - Show all commits

View File

@ -28,7 +28,7 @@ enum SortKey {
Size, Size,
Seeders, Seeders,
Leechers, Leechers,
Date, Scraped,
} }
impl Default for SortKey { impl Default for SortKey {
@ -44,7 +44,7 @@ impl fmt::Display for SortKey {
Self::Size => "size_bytes", Self::Size => "size_bytes",
Self::Seeders => "seeders", Self::Seeders => "seeders",
Self::Leechers => "leechers", Self::Leechers => "leechers",
Self::Date => "scraped_date", Self::Scraped => "scraped_date",
}; };
write!(f, "{}", s) write!(f, "{}", s)
} }
@ -190,7 +190,7 @@ fn search_query(
); );
let res = if type_ == "file" { let res = if type_ == "file" {
let results = torrent_file_search(conn, q, size, offset)?; let results = torrent_file_search(conn, q, sort_key, sort_dir, size, offset)?;
serde_json::to_value(&results).unwrap() serde_json::to_value(&results).unwrap()
} else { } else {
let results = torrent_search(conn, q, sort_key, sort_dir, size, offset)?; let results = torrent_search(conn, q, sort_key, sort_dir, size, offset)?;
@ -329,10 +329,13 @@ struct File {
fn torrent_file_search( fn torrent_file_search(
conn: r2d2::PooledConnection<SqliteConnectionManager>, conn: r2d2::PooledConnection<SqliteConnectionManager>,
query: &str, query: &str,
sort_key: SortKey,
sort_dir: SortDirection,
size: usize, size: usize,
offset: usize, offset: usize,
) -> Result<Vec<File>, Error> { ) -> Result<Vec<File>, Error> {
let stmt_str = "select * from files where path like '%' || ?1 || '%' limit ?2, ?3"; // `sort_key` and `sort_dir` are already sanitized and should not be escaped:
let stmt_str = format!("select * from files where path like '%' || ?1 || '%' order by {} {} limit ?2, ?3", sort_key, sort_dir);
let mut stmt = conn.prepare(&stmt_str).unwrap(); let mut stmt = conn.prepare(&stmt_str).unwrap();
let file_iter = stmt.query_map( let file_iter = stmt.query_map(
params![ params![
@ -405,7 +408,7 @@ mod tests {
let manager = SqliteConnectionManager::file(super::torrents_db_file()); let manager = SqliteConnectionManager::file(super::torrents_db_file());
let pool = r2d2::Pool::builder().max_size(15).build(manager).unwrap(); let pool = r2d2::Pool::builder().max_size(15).build(manager).unwrap();
let conn = pool.get().unwrap(); let conn = pool.get().unwrap();
let results = super::torrent_search(conn, "sherlock", 10, 0, SortKey::Name, SortDirection::Desc); let results = super::torrent_search(conn, "sherlock", super::SortKey::Name, super::SortDirection::Desc, 10, 0);
assert!(results.unwrap().len() > 2); assert!(results.unwrap().len() > 2);
// println!("Query took {:?} seconds.", end - start); // println!("Query took {:?} seconds.", end - start);
} }

View File

@ -40,7 +40,7 @@ class SortableLink extends Component {
if (searchParams.sort_dir === "Asc") { if (searchParams.sort_dir === "Asc") {
return <svg class="icon icon-arrow-up d-none d-sm-inline mr-1"><use xlinkHref="#icon-arrow-up"></use></svg>; return <svg class="icon icon-arrow-up d-none d-sm-inline mr-1"><use xlinkHref="#icon-arrow-up"></use></svg>;
} }
return <svg class="icon icon-arrow-down mr-1"><use xlinkHref="#icon-arrow-down"></use></svg>l return <svg class="icon icon-arrow-down mr-1"><use xlinkHref="#icon-arrow-down"></use></svg>;
} }
) )
@ -163,7 +163,7 @@ export class Search extends Component<any, State> {
<SortableLink sortKey="Leechers" state={this.state}/> <SortableLink sortKey="Leechers" state={this.state}/>
</th> </th>
<th class="text-right d-none d-md-table-cell"> <th class="text-right d-none d-md-table-cell">
<SortableLink sortKey="Date" state={this.state}/> <SortableLink sortKey="Scraped" state={this.state}/>
</th> </th>
<th></th> <th></th>
</tr> </tr>