Skip to content

Commit 2f40914

Browse files
committed
Don't do full text search on empty strings
I was going through our log filters to see if they were all still needed. We were specifically filtering out `text-search query doesn't contain lexemes: ""`. This happens whenever someone runs `cargo search` with no parameters, resulting in a request like `?q=&page=1`. Rather than filtering this out of our logs, let's avoid doing full text search at all. This will reduce load on our DB server. This does change the behavior from returning nothing to returning the first 10 crates, but that's probably what the user wanted anyway.
1 parent 9eb73e9 commit 2f40914

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/controllers/krate/search.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,25 @@ pub fn search(req: &mut Request) -> CargoResult<Response> {
5252
.into_boxed();
5353

5454
if let Some(q_string) = params.get("q") {
55-
let sort = params.get("sort").map(|s| &**s).unwrap_or("relevance");
56-
let q = plainto_tsquery(q_string);
57-
query = query.filter(
58-
q.matches(crates::textsearchable_index_col)
59-
.or(Crate::with_name(q_string)),
60-
);
61-
62-
query = query.select((
63-
ALL_COLUMNS,
64-
Crate::with_name(q_string),
65-
recent_crate_downloads::downloads.nullable(),
66-
));
67-
query = query.order(Crate::with_name(q_string).desc());
68-
69-
if sort == "relevance" {
70-
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
71-
query = query.then_order_by(rank.desc())
55+
if !q_string.is_empty() {
56+
let sort = params.get("sort").map(|s| &**s).unwrap_or("relevance");
57+
let q = plainto_tsquery(q_string);
58+
query = query.filter(
59+
q.matches(crates::textsearchable_index_col)
60+
.or(Crate::with_name(q_string)),
61+
);
62+
63+
query = query.select((
64+
ALL_COLUMNS,
65+
Crate::with_name(q_string),
66+
recent_crate_downloads::downloads.nullable(),
67+
));
68+
query = query.order(Crate::with_name(q_string).desc());
69+
70+
if sort == "relevance" {
71+
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
72+
query = query.then_order_by(rank.desc())
73+
}
7274
}
7375
}
7476

0 commit comments

Comments
 (0)