Skip to content

Commit 5baba9a

Browse files
committed
Enhance project search functionality with faceting and improved UI
1 parent dea806b commit 5baba9a

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

app/controllers/projects_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def index
2828
end
2929

3030
def search
31-
@projects = Project.pagy_search(params[:q])
32-
@pagy, @projects = pagy_meilisearch(@projects, limit: 100)
31+
@projects = Project.pagy_search(params[:q], facets: ['keywords', 'language'])
32+
@pagy, @projects = pagy_meilisearch(@projects, limit: 20)
3333
end
3434

3535
def lookup

app/models/project.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ class Project < ApplicationRecord
77
ActiveRecord_Relation.include Pagy::Meilisearch
88

99
meilisearch if: :reviewed? do
10+
add_attribute :language
1011
searchable_attributes [:name, :description, :url, :keywords, :owner, :category, :sub_category, :rubric, :readme, :works, :citation_file]
11-
displayed_attributes [:id, :name, :description, :url, :keywords, :owner, :category, :sub_category, :rubric, :readme, :works, :citation_file]
12+
displayed_attributes [:id, :name, :description, :url, :keywords, :owner, :category, :sub_category, :rubric, :readme, :works, :citation_file]
1213
filterable_attributes [:language, :keywords]
1314

1415
sortable_attributes [:name, :score]
16+
17+
faceting "sortFacetValuesBy": {'*'=> 'count'}
1518
end
1619

1720
validates :url, presence: true, uniqueness: { case_sensitive: false }

app/views/projects/search.html.erb

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,59 @@
66
</div>
77
<% end %>
88

9-
<% if @projects.any? %>
10-
<%= render @projects %>
11-
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
12-
<% else %>
13-
<div class="alert alert-info" role="alert">
14-
No projects found for the search term "<%= params[:q] %>"
9+
<div class="row">
10+
<div class="col-md-3">
11+
<% @projects.facets_distribution.each do |facet, values| %>
12+
<div class="mb-3">
13+
<ul class="list-group">
14+
<li class="list-group-item list-group-item-secondary">
15+
<%= facet.humanize %>
16+
</li>
17+
<% values.each_with_index do |(value, count), index| %>
18+
<li class="list-group-item facet-item <%= 'd-none' if index >= 20 %>" data-facet="<%= facet %>">
19+
<%= link_to search_projects_path(q: params[:q], facet => value), class: "text-decoration-none d-flex justify-content-between align-items-center" do %>
20+
<span><%= value %></span>
21+
<span class="badge bg-secondary"><%= count %></span>
22+
<% end %>
23+
</li>
24+
<% end %>
25+
<% if values.size > 20 %>
26+
<li class="list-group-item text-center">
27+
<button class="btn btn-link p-0 show-more" data-facet="<%= facet %>">Show More</button>
28+
</li>
29+
<% end %>
30+
</ul>
31+
</div>
32+
<% end %>
1533
</div>
16-
<% end %>
17-
</div>
34+
35+
<div class="col-md-9">
36+
<% if @projects.any? %>
37+
<%= render @projects %>
38+
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
39+
<% else %>
40+
<div class="alert alert-info" role="alert">
41+
No projects found for the search term "<%= params[:q] %>"
42+
</div>
43+
<% end %>
44+
</div>
45+
</div>
46+
</div>
47+
48+
<script>
49+
document.addEventListener("DOMContentLoaded", function() {
50+
document.querySelectorAll(".show-more").forEach(button => {
51+
button.addEventListener("click", function() {
52+
const facet = this.dataset.facet;
53+
54+
// Show all hidden facet items
55+
document.querySelectorAll(`.facet-item[data-facet="${facet}"].d-none`).forEach(el => {
56+
el.classList.remove("d-none");
57+
});
58+
59+
// Remove the entire list-group-item containing the button
60+
this.closest(".list-group-item").remove();
61+
});
62+
});
63+
});
64+
</script>

lib/tasks/search.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ namespace :search do
22
desc "Reindex all searchable models"
33
task reindex: :environment do
44
Project.clear_index!(true)
5-
Project.reviewed.reindex!
5+
Project.reviewed.find_each(&:index!)
66
end
77
end

0 commit comments

Comments
 (0)