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

Add sort filters to search page #1314

Merged
merged 1 commit into from
Oct 7, 2024
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
4 changes: 3 additions & 1 deletion app/controllers/solr_search_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class SolrSearchController < ApplicationController
before_action :search_for_dataset, only: [:search]

def search; end
def search
@sort = params["sort"]
end

private

Expand Down
4 changes: 4 additions & 0 deletions app/services/search/solr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ class Solr
def self.search(params)
query_param = params.fetch("q", "").squish
page = params["page"]
sort_param = params["sort"]
page && page.to_i.positive? ? page.to_i : 1

solr_client = client

query = "*:*" if query_param.empty?

sort_query = "metadata_modified desc" if sort_param == "recent"

solr_client.get "select", params: {
q: query,
start: page,
rows: 20,
fl: field_list,
sort: sort_query,
}
end

Expand Down
1 change: 1 addition & 0 deletions app/views/solr_search/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<div class="govuk-grid-row dgu-filters">
<div class="govuk-grid-column-two-thirds dgu-results">
<%= render '/search/sort' if @num_results > 1 %>
<span class="dgu-results__summary">
<span class="govuk-body-s govuk-!-font-weight-bold"><%= number_with_delimiter(@num_results) %></span>
<%= t('.result').pluralize(@num_results) %> <%= t('.found') %>
Expand Down
85 changes: 54 additions & 31 deletions spec/features/solr_search_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,66 @@
RSpec.describe "Solr Search page", type: :feature do
let(:results) { File.read(Rails.root.join("spec/fixtures/solr_response.json").to_s) }

before do
allow(Search::Solr).to receive(:search).and_return(JSON.parse(results))
end
describe "Default search page behaviour" do
before do
allow(Search::Solr).to receive(:search).and_return(JSON.parse(results))
end

scenario "Displays search heading" do
visit "/search/solr"
expect(page).to have_css("h1", text: "Search results")
end
scenario "Displays search heading" do
kentsanggds marked this conversation as resolved.
Show resolved Hide resolved
visit "/search/solr"
expect(page).to have_css("h1", text: "Search results")
end

scenario "Displays search box" do
visit "/search/solr"
expect(page).to have_content("Search data.gov.uk")
end
scenario "Displays search box" do
visit "/search/solr"
expect(page).to have_content("Search data.gov.uk")
end

scenario "Displays search results" do
visit "/search/solr"
expect(page).to have_content("2 results found")
end
scenario "Displays search results" do
visit "/search/solr"
expect(page).to have_content("2 results found")
end

scenario "Displays the title for each search result" do
visit "/search/solr"
expect(page).to have_css("h2", text: "A very interesting dataset")
expect(page).to have_css("h2", text: "A dataset with additional inspire metadata")
end
scenario "Displays the title for each search result" do
visit "/search/solr"
expect(page).to have_css("h2", text: "A very interesting dataset")
expect(page).to have_css("h2", text: "A dataset with additional inspire metadata")
end

scenario "Displays the publisher for each search result" do
visit "/search/solr"
results = all(".published_by")
expect(results.length).to be(2)
expect(results[0]).to have_content "Ministry of Housing, Communities and Local Government"
expect(results[1]).to have_content "Mole Valley District Council"
scenario "Displays the publisher for each search result" do
visit "/search/solr"
results = all(".published_by")
expect(results.length).to be(2)
expect(results[0]).to have_content "Ministry of Housing, Communities and Local Government"
expect(results[1]).to have_content "Mole Valley District Council"
end

scenario "Displays the last updated for each search result" do
visit "/search/solr"
expect(page).to have_content("Last updated", count: 2)
expect(page).to have_content("30 June 2017")
expect(page).to have_content("17 August 2018")
end

scenario "Results are sorted by best match" do
visit "/search/solr"
expect(page).to have_select("sort", selected: "Best match")
end
end

scenario "Displays the last updated for each search result" do
visit "/search/solr"
expect(page).to have_content("Last updated", count: 2)
expect(page).to have_content("30 June 2017")
expect(page).to have_content("17 August 2018")
describe "When a user filters the results" do
before do
allow(Search::Solr).to receive(:search).and_return(JSON.parse(results))
end

scenario "Results are sorted by best match" do
filtered_solr_search_for("Best match")
expect(page).to have_select("sort", selected: "Best match")
end

scenario "Results are sorted by most recent" do
filtered_solr_search_for("Most recent")
expect(page).to have_select("sort", selected: "Most recent")
end
end
end
7 changes: 7 additions & 0 deletions spec/support/solr_dataset_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def filtered_solr_search_for(sort_method)
visit "/search/solr"
within "#main-content" do
select sort_method, from: "Sort by"
find(".gem-c-search__submit").click
end
end
Loading