Skip to content

Use a stricter form of search for extremely short queries #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
query = query.filter(
q.clone()
.matches(crates::textsearchable_index_col)
.or(Crate::like_name(&q_string)),
.or(Crate::loosly_matches_name(&q_string)),
);

query = query.select((
Expand Down
29 changes: 22 additions & 7 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::NaiveDateTime;
use diesel::associations::Identifiable;
use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::sql_types::Bool;
use url::Url;

use crate::app::App;
Expand Down Expand Up @@ -86,8 +87,6 @@ pub const MAX_NAME_LENGTH: usize = 64;
type CanonCrateName<T> = self::canon_crate_name::HelperType<T>;
type All = diesel::dsl::Select<crates::table, AllColumns>;
type WithName<'a> = diesel::dsl::Eq<CanonCrateName<crates::name>, CanonCrateName<&'a str>>;
/// The result of a loose search
type LikeName = diesel::dsl::Like<CanonCrateName<crates::name>, CanonCrateName<String>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
type ByExactName<'a> = diesel::dsl::Filter<All, diesel::dsl::Eq<crates::name, &'a str>>;

Expand Down Expand Up @@ -236,12 +235,28 @@ impl<'a> NewCrate<'a> {
}

impl Crate {
/// SQL filter with the `like` binary operator. Adds wildcards to the beginning and end to get
/// substring matches.
pub fn like_name(name: &str) -> LikeName {
let wildcard_name = format!("%{}%", name);
canon_crate_name(crates::name).like(canon_crate_name(wildcard_name))
/// SQL filter based on whether the crate's name loosly matches the given
/// string.
///
/// The operator used varies based on the input.
pub fn loosly_matches_name<QS>(
name: &str,
) -> Box<dyn BoxableExpression<QS, Pg, SqlType = Bool> + '_>
where
crates::name: SelectableExpression<QS>,
{
if name.len() > 2 {
let wildcard_name = format!("%{}%", name);
Box::new(canon_crate_name(crates::name).like(canon_crate_name(wildcard_name)))
} else {
diesel_infix_operator!(MatchesWord, "%>");
Box::new(MatchesWord::new(
canon_crate_name(crates::name),
name.into_sql::<Text>(),
))
}
}

/// SQL filter with the = binary operator
pub fn with_name(name: &str) -> WithName<'_> {
canon_crate_name(crates::name).eq(canon_crate_name(name))
Expand Down
4 changes: 4 additions & 0 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ fn loose_search_order() {
for (lhs, rhs) in search_temp.crates.iter().zip(ordered) {
assert_eq!(lhs.name, rhs.name);
}

let search_temp = anon.search("q=te");
assert_eq!(search_temp.meta.total, 3);
assert_eq!(search_temp.crates.len(), 3);
}

#[test]
Expand Down