Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Endpoint for searching and discovery functionality

use diesel::sql_types::{NotNull, Nullable};
use diesel_full_text_search::*;

use crate::controllers::helpers::Paginate;
Expand Down Expand Up @@ -41,17 +42,20 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
.get("sort")
.map(|s| &**s)
.unwrap_or("recent-downloads");
let mut has_filter = false;

let selection = (
ALL_COLUMNS,
false.into_sql::<Bool>(),
recent_crate_downloads::downloads.nullable(),
);
let mut query = crates::table
.left_join(recent_crate_downloads::table)
.select((
ALL_COLUMNS,
false.into_sql::<Bool>(),
recent_crate_downloads::downloads.nullable(),
))
.select(selection)
.into_boxed();

if let Some(q_string) = params.get("q") {
has_filter = true;
if !q_string.is_empty() {
let sort = params.get("sort").map(|s| &**s).unwrap_or("relevance");
let q = plainto_tsquery(q_string);
Expand All @@ -75,6 +79,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
}

if let Some(cat) = params.get("category") {
has_filter = true;
query = query.filter(
crates::id.eq_any(
crates_categories::table
Expand All @@ -90,6 +95,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
}

if let Some(kw) = params.get("keyword") {
has_filter = true;
query = query.filter(
crates::id.eq_any(
crates_keywords::table
Expand All @@ -99,6 +105,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
),
);
} else if let Some(letter) = params.get("letter") {
has_filter = true;
let pattern = format!(
"{}%",
letter
Expand All @@ -110,6 +117,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
);
query = query.filter(canon_crate_name(crates::name).like(pattern));
} else if let Some(user_id) = params.get("user_id").and_then(|s| s.parse::<i32>().ok()) {
has_filter = true;
query = query.filter(
crates::id.eq_any(
crate_owners::table
Expand All @@ -120,6 +128,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
),
);
} else if let Some(team_id) = params.get("team_id").and_then(|s| s.parse::<i32>().ok()) {
has_filter = true;
query = query.filter(
crates::id.eq_any(
crate_owners::table
Expand All @@ -130,6 +139,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
),
);
} else if params.get("following").is_some() {
has_filter = true;
query = query.filter(
crates::id.eq_any(
follows::table
Expand All @@ -151,9 +161,23 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {

// The database query returns a tuple within a tuple, with the root
// tuple containing 3 items.
let data = query
.paginate(limit, offset)
.load::<((Crate, bool, Option<i64>), i64)>(&*conn)?;
let data = if has_filter {
query
.paginate(limit, offset)
.load::<((Crate, bool, Option<i64>), i64)>(&*conn)?
} else {
sql_function!(fn coalesce<T: NotNull>(value: Nullable<T>, default: T) -> T);
query
.select((
// FIXME: Use `query.selection()` if that feature ends up in
// Diesel 2.0
selection,
coalesce(crates::table.count().single_value(), 0),
))
.limit(limit)
.offset(offset)
.load(&*conn)?
};
let total = data.first().map(|&(_, t)| t).unwrap_or(0);
let perfect_matches = data.iter().map(|&((_, b, _), _)| b).collect::<Vec<_>>();
let recent_downloads = data
Expand Down