Skip to content

Commit 143432b

Browse files
committed
WIP: Improved search
Includes kaminari for pagination
1 parent 899b240 commit 143432b

File tree

7 files changed

+110
-29
lines changed

7 files changed

+110
-29
lines changed

app/controllers/better_together/search_controller.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,42 @@ module BetterTogether
55
class SearchController < ApplicationController
66
def search
77
@query = params[:q]
8-
@results = if @query.present?
9-
BetterTogether::Page.search(@query).records
10-
else
11-
[]
12-
end
8+
9+
if @query.present?
10+
search_response = BetterTogether::Page.search({
11+
query: {
12+
bool: {
13+
must: [
14+
{
15+
multi_match: {
16+
query: @query,
17+
fields: ['title^2', 'content', 'blocks.rich_text_content.body'],
18+
type: 'best_fields'
19+
}
20+
}
21+
]
22+
}
23+
},
24+
highlight: {
25+
fields: {
26+
title: {},
27+
content: {},
28+
'blocks.rich_text_content.body': {
29+
fragment_size: 150,
30+
number_of_fragments: 3
31+
}
32+
}
33+
}
34+
})
35+
36+
# Use Kaminari for pagination
37+
@results = Kaminari.paginate_array(search_response.records.to_a).page(params[:page]).per(10)
38+
@highlights = search_response.response['hits']['hits']
39+
else
40+
@results = BetterTogether::Page.none.page(params[:page]).per(10)
41+
@highlights = []
42+
end
1343
end
44+
1445
end
1546
end

app/models/better_together/content/block.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,41 @@ def to_s
8585
'new'
8686
end}"
8787
end
88+
89+
# Method to return the content used for Elasticsearch indexing
90+
def cached_content
91+
{
92+
id: id,
93+
type: type,
94+
content: block_content,
95+
translations: translated_content
96+
}
97+
end
98+
99+
protected
100+
101+
# Define how to extract the main content from the block
102+
def block_content
103+
if respond_to?(:rich_text_content)
104+
rich_text_content&.body&.to_plain_text
105+
elsif respond_to?(:background_image_file)
106+
background_image_file.filename.to_s
107+
else
108+
''
109+
end
110+
end
111+
112+
# Extract translations if available
113+
def translated_content
114+
return {} unless respond_to?(:mobility_attributes)
115+
116+
I18n.available_locales.each_with_object({}) do |locale, translations|
117+
translations[locale] = {}
118+
mobility_attributes.each do |attr|
119+
translations[locale][attr] = send("#{attr}_#{locale}")
120+
end
121+
end
122+
end
88123
end
89124
end
90125
end

app/models/better_together/page.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Page < ApplicationRecord
3535
indexes :title, as: 'title'
3636

3737
indexes :blocks, type: 'nested' do
38+
indexes :type, type: 'keyword'
3839
indexes :rich_text_content, type: 'nested' do
3940
indexes :body, type: 'text'
4041
end

app/models/concerns/better_together/searchable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module Searchable
1818

1919
# Need to create another way to access elasticsearch import.
2020
# class.import is using by activerecord-import, I think
21-
def self.elastic_import
22-
__elasticsearch__.import unless Rails.env.test?
21+
def self.elastic_import args={}
22+
__elasticsearch__.import(args) unless Rails.env.test?
2323
end
2424
end
2525

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
<!-- app/views/pages/search.html.erb -->
2-
3-
<div class="container mt-4">
4-
<!-- Search Results -->
1+
<div class="container mt-5">
52
<% if @query.present? %>
6-
<h2>Search Results for "<%= @query %>"</h2>
3+
<h2 class="mb-4">Search Results for "<%= @query %>"</h2>
74

85
<% if @results.any? %>
9-
<table class="table table-striped mt-3">
10-
<thead>
11-
<tr>
12-
<th>Title</th>
13-
<th>Content</th>
14-
</tr>
15-
</thead>
16-
<tbody>
17-
<% @results.each do |result| %>
18-
<tr>
19-
<td><%= link_to result.title, render_page_path(result.slug) %></td>
20-
<td><%= truncate(strip_tags(result.content.to_s), length: 100) %></td>
21-
</tr>
22-
<% end %>
23-
</tbody>
24-
</table>
6+
<div class="row">
7+
<% @results.each_with_index do |result, index| %>
8+
<% highlight = @highlights[index]['highlight'] || {} %>
9+
<div class="col-md-6 mb-4">
10+
<div class="card shadow-sm">
11+
<div class="card-body">
12+
<h5 class="card-title">
13+
<%= link_to result.title, render_page_path(result.slug), class: "text-decoration-none" %>
14+
</h5>
15+
16+
<p class="card-text">
17+
<% if highlight['title'] %>
18+
<%= highlight['title'].first.html_safe %>
19+
<% elsif highlight['content'] %>
20+
<%= highlight['content'].first.html_safe %>
21+
<% elsif highlight['blocks.rich_text_content.body'] %>
22+
<%= highlight['blocks.rich_text_content.body'].first.html_safe %>
23+
<% else %>
24+
<%= truncate(strip_tags(result.content.to_s), length: 150) %>
25+
<% end %>
26+
</p>
27+
28+
<a href="<%= render_page_path(result.slug) %>" class="btn btn-primary mt-2">View More</a>
29+
</div>
30+
</div>
31+
</div>
32+
<% end %>
33+
</div>
34+
<%= paginate @results %>
2535
<% else %>
26-
<p>No results found for "<%= @query %>". Please try another search term.</p>
36+
<div class="alert alert-warning mt-4">
37+
<p>No results found for "<%= @query %>". Please try another search term.</p>
38+
</div>
2739
<% end %>
2840
<% end %>
2941
</div>

better_together.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Gem::Specification.new do |spec|
4949
spec.add_dependency 'image_processing', '~> 1.2'
5050
spec.add_dependency 'importmap-rails', '~> 2.0'
5151
spec.add_dependency 'jsonapi-resources', '>= 0.10.0'
52+
spec.add_dependency 'kaminari'
5253
spec.add_dependency 'memory_profiler'
5354
spec.add_dependency 'mobility', '>= 1.0.1', '< 2.0'
5455
spec.add_dependency 'mobility-actiontext', '~> 1.1'

lib/better_together/engine.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'humanize_boolean'
2323
require 'i18n-timezones'
2424
require 'importmap-rails'
25+
require 'kaminari'
2526
require 'noticed'
2627
require 'premailer/rails'
2728
require 'reform/rails'

0 commit comments

Comments
 (0)