Skip to content
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

Allow multiple keywords in crate search #1543

Merged
merged 5 commits into from
Jan 4, 2020
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
1 change: 1 addition & 0 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default Controller.extend(EKMixin, {
queryParams: {
q: this.searchQuery,
page: 1,
all_keywords: null,
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PaginationMixin from '../mixins/pagination';

export default Controller.extend(PaginationMixin, {
search: service(),
queryParams: ['q', 'page', 'per_page', 'sort'],
queryParams: ['all_keywords', 'page', 'per_page', 'q', 'sort'],
q: alias('search.q'),
page: '1',
per_page: 10,
Expand Down
3 changes: 2 additions & 1 deletion app/routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import Route from '@ember/routing/route';

export default Route.extend({
queryParams: {
q: { refreshModel: true },
all_keywords: { refreshModel: true },
page: { refreshModel: true },
q: { refreshModel: true },
sort: { refreshModel: true },
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE keywords ALTER COLUMN keyword TYPE varchar;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE keywords ALTER COLUMN keyword TYPE text;
25 changes: 24 additions & 1 deletion src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,28 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
);
}

if let Some(kw) = params.get("keyword") {
if let Some(kws) = params.get("all_keywords") {
use diesel::sql_types::Array;
sql_function!(#[aggregate] fn array_agg<T>(x: T) -> Array<T>);

let names: Vec<_> = kws
.split_whitespace()
.map(|name| name.to_lowercase())
.collect();

query = query.filter(
// FIXME: Just use `.contains` in Diesel 2.0
// https://github.com/diesel-rs/diesel/issues/2066
Contains::new(
crates_keywords::table
.inner_join(keywords::table)
.filter(crates_keywords::crate_id.eq(crates::id))
.select(array_agg(keywords::keyword))
.single_value(),
names.into_sql::<Array<Text>>(),
),
);
} else if let Some(kw) = params.get("keyword") {
query = query.filter(
crates::id.eq_any(
crates_keywords::table
Expand Down Expand Up @@ -230,3 +251,5 @@ pub fn search(req: &mut dyn Request) -> CargoResult<Response> {
},
}))
}

diesel_infix_operator!(Contains, "@>");
4 changes: 2 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,10 @@ table! {
id -> Int4,
/// The `keyword` column of the `keywords` table.
///
/// Its SQL type is `Varchar`.
/// Its SQL type is `Text`.
///
/// (Automatically generated by Diesel.)
keyword -> Varchar,
keyword -> Text,
/// The `crates_cnt` column of the `keywords` table.
///
/// Its SQL type is `Int4`.
Expand Down
14 changes: 10 additions & 4 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,34 @@ fn index_queries() {
CrateBuilder::new("foo", user.id)
.keyword("kw3")
.expect_build(conn);

CrateBuilder::new("two-keywords", user.id)
.keyword("kw1")
.keyword("kw3")
.expect_build(conn);
(krate, krate2)
});

assert_eq!(anon.search("q=baz").meta.total, 0);

// All of these fields should be indexed/searched by the queries
assert_eq!(anon.search("q=foo").meta.total, 2);
assert_eq!(anon.search("q=kw1").meta.total, 2);
assert_eq!(anon.search("q=kw1").meta.total, 3);
assert_eq!(anon.search("q=readme").meta.total, 1);
assert_eq!(anon.search("q=description").meta.total, 1);

assert_eq!(anon.search_by_user_id(user.id).crates.len(), 3);
assert_eq!(anon.search_by_user_id(user.id).crates.len(), 4);
assert_eq!(anon.search_by_user_id(0).crates.len(), 0);

assert_eq!(anon.search("letter=F").crates.len(), 2);
assert_eq!(anon.search("letter=B").crates.len(), 1);
assert_eq!(anon.search("letter=b").crates.len(), 1);
assert_eq!(anon.search("letter=c").crates.len(), 0);

assert_eq!(anon.search("keyword=kw1").crates.len(), 2);
assert_eq!(anon.search("keyword=KW1").crates.len(), 2);
assert_eq!(anon.search("keyword=kw1").crates.len(), 3);
assert_eq!(anon.search("keyword=KW1").crates.len(), 3);
assert_eq!(anon.search("keyword=kw2").crates.len(), 0);
assert_eq!(anon.search("all_keywords=kw1 kw3").crates.len(), 1);

assert_eq!(anon.search("q=foo&keyword=kw1").crates.len(), 1);
assert_eq!(anon.search("q=foo2&keyword=kw1").crates.len(), 0);
Expand Down