Skip to content

Commit a477bf2

Browse files
committed
Improve search
Allow multi-model results. Only include searchable in models that are ready to return search results.
1 parent 143432b commit a477bf2

File tree

18 files changed

+173
-183
lines changed

18 files changed

+173
-183
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
.image-gallery {
3-
min-height: 50vh;
3+
// min-height: 50vh;
44
}

app/controllers/better_together/search_controller.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,44 @@ module BetterTogether
44
# Handles dispatching search queries to elasticsearch and displaying the results
55
class SearchController < ApplicationController
66
def search
7+
searchable_models = BetterTogether::Searchable.included_in_models
78
@query = params[:q]
9+
search_results = []
10+
suggestions = []
811

912
if @query.present?
10-
search_response = BetterTogether::Page.search({
13+
response = Elasticsearch::Model.search({
1114
query: {
1215
bool: {
1316
must: [
1417
{
1518
multi_match: {
1619
query: @query,
17-
fields: ['title^2', 'content', 'blocks.rich_text_content.body'],
20+
fields: ['title^3', 'content', 'blocks.rich_text_content.body^2', 'formatted_address^2', 'name^3', 'description^2'],
1821
type: 'best_fields'
1922
}
2023
}
2124
]
2225
}
2326
},
24-
highlight: {
25-
fields: {
26-
title: {},
27-
content: {},
28-
'blocks.rich_text_content.body': {
29-
fragment_size: 150,
30-
number_of_fragments: 3
27+
suggest: {
28+
text: @query,
29+
suggestions: {
30+
term: {
31+
field: 'name',
32+
suggest_mode: 'always'
3133
}
3234
}
3335
}
34-
})
36+
}, searchable_models)
3537

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 = []
38+
search_results = response.records.to_a
39+
suggestions = response.response['suggest']['suggestions'].map { |s| s['options'].map { |o| o['text'] } }.flatten
4240
end
41+
42+
# Use Kaminari for pagination
43+
@results = Kaminari.paginate_array(search_results).page(params[:page]).per(10)
44+
@suggestions = suggestions
4345
end
4446

4547
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
module BetterTogether
3+
class ElasticsearchIndexJob < ApplicationJob
4+
queue_as :default
5+
6+
def perform(record, action)
7+
return unless record.respond_to? :__elasticsearch__
8+
9+
case action
10+
when :index
11+
record.__elasticsearch__.index_document
12+
when :delete
13+
record.__elasticsearch__.delete_document
14+
else
15+
raise ArgumentError, "Unknown action: #{action}"
16+
end
17+
end
18+
end
19+
end

app/models/better_together/calendar.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class Calendar < ApplicationRecord
88
include Identifier
99
include Privacy
1010
include Protected
11-
include Searchable
1211
include Viewable
1312

1413
belongs_to :community, class_name: '::BetterTogether::Community'
@@ -18,19 +17,6 @@ class Calendar < ApplicationRecord
1817
translates :name
1918
translates :description, backend: :action_text
2019

21-
settings index: { number_of_shards: 1 } do
22-
mappings dynamic: 'false' do
23-
indexes :name, as: 'name'
24-
indexes :description, as: 'description'
25-
indexes :rich_text_content, type: 'nested' do
26-
indexes :body, type: 'text'
27-
end
28-
indexes :rich_text_translations, type: 'nested' do
29-
indexes :body, type: 'text'
30-
end
31-
end
32-
end
33-
3420
def to_s
3521
name
3622
end

app/models/better_together/community.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Community < ApplicationRecord
1010
include Protected
1111
include Privacy
1212
include Permissible
13-
include Searchable
1413

1514
belongs_to :creator,
1615
class_name: '::BetterTogether::Person',
@@ -60,21 +59,7 @@ class Community < ApplicationRecord
6059
before_save :purge_cover_image, if: -> { remove_cover_image == '1' }
6160
before_save :purge_logo, if: -> { remove_logo == '1' }
6261

63-
validates :name,
64-
presence: true
65-
66-
settings index: { number_of_shards: 1 } do
67-
mappings dynamic: 'false' do
68-
indexes :title, as: 'title'
69-
indexes :description_html, as: 'description_html'
70-
indexes :rich_text_content, type: 'nested' do
71-
indexes :body, type: 'text'
72-
end
73-
indexes :rich_text_translations, type: 'nested' do
74-
indexes :body, type: 'text'
75-
end
76-
end
77-
end
62+
validates :name, presence: true
7863

7964
# Resize the cover image to specific dimensions
8065
def cover_image_variant(width, height)

app/models/better_together/content/block.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ module Content
88
class Block < ApplicationRecord
99
include ::BetterTogether::Content::BlockAttributes
1010

11-
SUBCLASSES = [
12-
::BetterTogether::Content::Image, ::BetterTogether::Content::Hero, ::BetterTogether::Content::Html,
13-
::BetterTogether::Content::Css, ::BetterTogether::Content::RichText, ::BetterTogether::Content::Template
14-
].freeze
15-
1611
has_many :page_blocks, foreign_key: :block_id, dependent: :destroy
1712
has_many :pages, through: :page_blocks
1813

@@ -43,7 +38,7 @@ def self.block_name
4338
end
4439

4540
def self.load_all_subclasses
46-
SUBCLASSES.each(&:connection) # Add all known subclasses here
41+
Rails.application.eager_load! # Ensure all models are loaded
4742
end
4843

4944
def self.localized_block_attributes

app/models/better_together/geography/map.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Map < ApplicationRecord
99
include Identifier # Adds unique identifier functionality
1010
include Privacy # Manages privacy settings
1111
include Protected # Adds protection mechanisms
12-
include Searchable # Enables search capabilities
1312
include Viewable # Tracks view counts and related metrics
1413

1514
belongs_to :mappable, polymorphic: true, optional: true
@@ -26,19 +25,6 @@ class Map < ApplicationRecord
2625

2726
before_validation :set_default_center, on: :create
2827

29-
settings index: { number_of_shards: 1 } do
30-
mappings dynamic: 'false' do
31-
indexes :title, as: 'title'
32-
indexes :description, as: 'description'
33-
indexes :rich_text_content, type: 'nested' do
34-
indexes :body, type: 'text'
35-
end
36-
indexes :rich_text_translations, type: 'nested' do
37-
indexes :body, type: 'text'
38-
end
39-
end
40-
end
41-
4228
def self.permitted_attributes(id: false, destroy: false)
4329
super + %i[type zoom center]
4430
end

app/models/better_together/infrastructure/building.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Building < Structure
1111
include Geography::Geospatial::One
1212
include Privacy
1313
include PrimaryCommunity
14-
include Searchable
1514

1615
belongs_to :address,
1716
-> { where(label: 'physical', physical: true, primary_flag: true) },
@@ -40,19 +39,6 @@ class Building < Structure
4039

4140
slugged :name
4241

43-
settings index: { number_of_shards: 1 } do
44-
mappings dynamic: 'false' do
45-
indexes :name, as: 'name'
46-
indexes :description, as: 'description'
47-
indexes :rich_text_content, type: 'nested' do
48-
indexes :body, type: 'text'
49-
end
50-
indexes :rich_text_translations, type: 'nested' do
51-
indexes :body, type: 'text'
52-
end
53-
end
54-
end
55-
5642
def self.permitted_attributes(id: false, destroy: false)
5743
[
5844
{

app/models/better_together/infrastructure/floor.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Floor < ApplicationRecord
1212
include Positioned
1313
include Privacy
1414
include PrimaryCommunity
15-
include Searchable
1615

1716
after_create :ensure_room
1817

@@ -24,19 +23,6 @@ class Floor < ApplicationRecord
2423

2524
slugged :name
2625

27-
settings index: { number_of_shards: 1 } do
28-
mappings dynamic: 'false' do
29-
indexes :name, as: 'name'
30-
indexes :description, as: 'description'
31-
indexes :rich_text_content, type: 'nested' do
32-
indexes :body, type: 'text'
33-
end
34-
indexes :rich_text_translations, type: 'nested' do
35-
indexes :body, type: 'text'
36-
end
37-
end
38-
end
39-
4026
validates :level,
4127
numericality: { only_integer: true },
4228
uniqueness: { scope: %i[building_id] },

app/models/better_together/infrastructure/room.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Room < ApplicationRecord
1111
include Geography::Geospatial::One
1212
include Privacy
1313
include PrimaryCommunity
14-
include Searchable
1514

1615
belongs_to :floor, class_name: 'BetterTogether::Infrastructure::Floor', touch: true
1716
has_one :building, through: :floor, class_name: 'BetterTogether::Infrastructure::Building'
@@ -23,19 +22,6 @@ class Room < ApplicationRecord
2322

2423
slugged :name
2524

26-
settings index: { number_of_shards: 1 } do
27-
mappings dynamic: 'false' do
28-
indexes :name, as: 'name'
29-
indexes :description, as: 'description'
30-
indexes :rich_text_content, type: 'nested' do
31-
indexes :body, type: 'text'
32-
end
33-
indexes :rich_text_translations, type: 'nested' do
34-
indexes :body, type: 'text'
35-
end
36-
end
37-
end
38-
3925
def to_s
4026
name
4127
end

0 commit comments

Comments
 (0)