Skip to content

Commit

Permalink
perform search with litesearch
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Nov 2, 2023
1 parent 4fa7bb8 commit 846db36
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
3 changes: 1 addition & 2 deletions app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class TalksController < ApplicationController
def index
session[:talks_page] = params[:page] || 1
if params[:q].present?
talks = Talk.includes(:speakers, :event).pagy_search(params[:q])
@pagy, @talks = pagy_meilisearch(talks, items: 9, page: session[:talks_page]&.to_i || 1)
@pagy, @talks = pagy(Talk.search(params[:q]).all.order(date: :desc).includes(:speakers, :event), items: 9, page: session[:talks_page]&.to_i || 1)
else
@pagy, @talks = pagy(Talk.all.order(date: :desc).includes(:speakers, :event), items: 9, page: session[:talks_page]&.to_i || 1)
end
Expand Down
31 changes: 5 additions & 26 deletions app/models/talk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class Talk < ApplicationRecord
include Sluggable
include Suggestable
slug_from :title
include MeiliSearch::Rails
ActiveRecord_Relation.include Pagy::Meilisearch
extend Pagy::Meilisearch
include Litesearch::Model

# associations
belongs_to :event, optional: true
Expand All @@ -43,32 +41,13 @@ class Talk < ApplicationRecord
delegate :name, to: :event, prefix: true, allow_nil: true

# search
meilisearch do
attribute :title
attribute :description
attribute :slug
attribute :video_id
attribute :video_provider
attribute :thumbnail_sm
attribute :thumbnail_md
attribute :thumbnail_lg
attribute :speaker_names do
speakers.pluck(:name)
end
attribute :event_name do
event_name
end
searchable_attributes [:title, :description, :speaker_names, :event_name]
sortable_attributes [:title]

attributes_to_highlight ["*"]
litesearch do |schema|
schema.field :title, weight: 3
schema.field :description
schema.tokenizer :trigram
end

meilisearch enqueue: true

# ensure that during the reindex process the associated records are eager loaded
scope :meilisearch_import, -> { includes(:speakers, :event) }

def to_meta_tags
{
title: title,
Expand Down

0 comments on commit 846db36

Please sign in to comment.