Skip to content

Implemented 'sort by relevance/downloads' for search results #391

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 3 commits into from
Jul 29, 2016
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
7 changes: 6 additions & 1 deletion app/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import PaginationMixin from '../mixins/pagination';
const { computed } = Ember;

export default Ember.Controller.extend(PaginationMixin, {
queryParams: ['q', 'page', 'per_page'],
queryParams: ['q', 'page', 'per_page', 'sort'],
q: null,
page: '1',
per_page: 10,
sort: null,

totalItems: computed.readOnly('model.meta.total'),

currentSortBy: computed('sort', function() {
return (this.get('sort') === 'downloads') ? 'Downloads' : 'Relevance';
}),
});
1 change: 1 addition & 0 deletions app/routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default Ember.Route.extend({
queryParams: {
q: { refreshModel: true },
page: { refreshModel: true },
sort: { refreshModel: true },
},

model(params) {
Expand Down
25 changes: 25 additions & 0 deletions app/templates/search.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@
of <span class='total'>{{ totalItems }}</span> total results
</span>
</div>

<div class='sort'>
<span class='small'>Sort by </span>

{{#rl-dropdown-container class="dropdown-container"}}
{{#rl-dropdown-toggle tagName="a" class="dropdown"}}
<img class="sort" src="/assets/sort.png"/>
{{ currentSortBy }}
<span class='arrow'></span>
{{/rl-dropdown-toggle}}

{{#rl-dropdown tagName="ul" class="dropdown" closeOnChildClick="a:link"}}
<li>
{{#link-to (query-params page=1 sort="")}}
Relevance
{{/link-to}}
</li>
<li>
{{#link-to (query-params page=1 sort="downloads")}}
Downloads
{{/link-to}}
</li>
{{/rl-dropdown}}
{{/rl-dropdown-container}}
</div>
</div>

<div id='crates' class='white-rows'>
Expand Down
21 changes: 11 additions & 10 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
let query = req.query();
let sort = query.get("sort").map(|s| &s[..]).unwrap_or("alpha");
let sort_sql = match sort {
"downloads" => "ORDER BY crates.downloads DESC",
_ => "ORDER BY crates.name ASC",
"downloads" => "crates.downloads DESC",
_ => "crates.name ASC",
};

// Different queries for different parameters.
Expand All @@ -469,12 +469,12 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
let mut args = vec![&limit as &ToSql, &offset];
let (q, cnt) = query.get("q").map(|query| {
args.insert(0, query);
("SELECT crates.* FROM crates,
(format!("SELECT crates.* FROM crates,
plainto_tsquery($1) q,
ts_rank_cd(textsearchable_index_col, q) rank
WHERE q @@ textsearchable_index_col
ORDER BY name = $1 DESC, rank DESC, crates.name ASC
LIMIT $2 OFFSET $3".to_string(),
ORDER BY name = $1 DESC, rank DESC, {}
LIMIT $2 OFFSET $3", sort_sql),
"SELECT COUNT(crates.*) FROM crates,
plainto_tsquery($1) q
WHERE q @@ textsearchable_index_col".to_string())
Expand All @@ -484,7 +484,7 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
.to_lowercase().collect::<String>());
needs_pattern = true;
(format!("SELECT * FROM crates WHERE canon_crate_name(name) \
LIKE $1 {} LIMIT $2 OFFSET $3", sort_sql),
LIKE $1 ORDER BY {} LIMIT $2 OFFSET $3", sort_sql),
"SELECT COUNT(*) FROM crates WHERE canon_crate_name(name) \
LIKE $1".to_string())
})
Expand All @@ -497,7 +497,7 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
INNER JOIN keywords
ON crates_keywords.keyword_id = keywords.id
WHERE lower(keywords.keyword) = lower($1)";
(format!("SELECT crates.* {} {} LIMIT $2 OFFSET $3", base, sort_sql),
(format!("SELECT crates.* {} ORDER BY {} LIMIT $2 OFFSET $3", base, sort_sql),
format!("SELECT COUNT(crates.*) {}", base))
})
}).or_else(|| {
Expand All @@ -508,7 +508,8 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
INNER JOIN crate_owners
ON crate_owners.crate_id = crates.id
WHERE crate_owners.owner_id = $1
AND crate_owners.owner_kind = {} {}
AND crate_owners.owner_kind = {}
ORDER BY {}
LIMIT $2 OFFSET $3",
OwnerKind::User as i32, sort_sql),
format!("SELECT COUNT(crates.*) FROM crates
Expand All @@ -524,15 +525,15 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
(format!("SELECT crates.* FROM crates
INNER JOIN follows
ON follows.crate_id = crates.id AND
follows.user_id = $1
follows.user_id = $1 ORDER BY
{} LIMIT $2 OFFSET $3", sort_sql),
"SELECT COUNT(crates.*) FROM crates
INNER JOIN follows
ON follows.crate_id = crates.id AND
follows.user_id = $1".to_string())
})
}).unwrap_or_else(|| {
(format!("SELECT * FROM crates {} LIMIT $1 OFFSET $2",
(format!("SELECT * FROM crates ORDER BY {} LIMIT $1 OFFSET $2",
sort_sql),
"SELECT COUNT(*) FROM crates".to_string())
});
Expand Down
4 changes: 2 additions & 2 deletions tests/acceptance/search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('searching for "rust"', function(assert) {
assert.notOk(find('a[href="/search?page=3&q=rust"]')[0]);

hasText(assert, '#crates-heading', 'Search Results for \'rust\'');
hasText(assert, '#results', 'Displaying 1-10 of 18 total results');
hasText(assert, '#results', 'Displaying 1-10 of 18 total results Sort by Relevance Relevance Downloads');

hasText(assert, '#crates .row:first .desc .info', 'rust_mixin 0.0.1');
hasText(assert, '#crates .row:first .desc .summary', 'Yo dawg, use Rust to generate Rust, right in your Rust. (See `external_mixin` to use scripting languages.)');
Expand All @@ -35,7 +35,7 @@ test('searching for "rust"', function(assert) {
assert.notOk(find('a[href="/search?page=3&q=rust"]')[0]);

hasText(assert, '#crates-heading', 'Search Results for \'rust\'');
hasText(assert, '#results', 'Displaying 11-18 of 18 total results');
hasText(assert, '#results', 'Displaying 11-18 of 18 total results Sort by Relevance Relevance Downloads');

hasText(assert, '#crates .row:first .desc .info', 'rusted_cypher 0.7.1');
});
Expand Down