Compare commits

...
Author SHA1 Message Date
Shivam Mishra 4a758f1a2b feat: setup algolia 2023-11-13 11:10:10 +05:30
Shivam Mishra d17fa149a6 feat: init algolia 2023-11-11 19:35:30 +05:30
Shivam Mishra 3bde1becbd chore: install algoliasearch-rails 2023-11-11 18:52:59 +05:30
4 changed files with 55 additions and 1 deletions
+3
View File
@@ -176,6 +176,9 @@ gem 'reverse_markdown'
# Sentiment analysis
gem 'informers'
# Algolia
gem "algoliasearch-rails"
### Gems required only in specific deployment environments ###
##############################################################
+9
View File
@@ -121,6 +121,14 @@ GEM
jbuilder (~> 2)
rails (>= 4.2, < 7.1)
selectize-rails (~> 0.6)
algolia (2.3.2)
faraday (>= 0.15, < 3)
faraday-net_http_persistent (>= 0.15, < 3)
multi_json (~> 1.0)
net-http-persistent
algoliasearch-rails (2.3.0)
algolia (< 3.0.0)
json (>= 1.5.1)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
@@ -838,6 +846,7 @@ DEPENDENCIES
administrate (>= 0.19.0)
administrate-field-active_storage
administrate-field-belongs_to_search
algoliasearch-rails
annotate
attr_extras
audited (~> 5.4, >= 5.4.1)
+39 -1
View File
@@ -28,6 +28,7 @@
#
class Article < ApplicationRecord
include PgSearch::Model
include AlgoliaSearch
has_many :associated_articles,
class_name: :Article,
@@ -82,7 +83,32 @@ class Article < ApplicationRecord
}
)
def self.search(params)
algoliasearch per_environment: true do
# the list of attributes sent to Algolia's API
attribute :created_at, :title, :content, :description
add_attribute :tenant_id
# searchableAttributes ['title', 'description', 'content']
searchableAttributes %w[title description content]
# integer version of the created_at datetime field, to use numerical filtering
attribute :created_at_i do
created_at.to_i
end
end
def tenant_id
"cw-tenant-#{account.id}-#{portal.id}"
end
def self.search_with_algolia(params)
results = raw_search(params[:query])
hit_ids = results['hits'].pluck('objectID')
Article.where(id: hit_ids)
end
def self.search_on_db(params)
records = joins(
:category
).search_by_category_slug(
@@ -93,6 +119,18 @@ class Article < ApplicationRecord
records
end
def self.search(params)
if algolia_enabled?
search_with_algolia(params)
else
search_on_db(params)
end
end
def self.algolia_enabled?
ENV['ALGOLIA_KEY'].present? && ENV['ALGOLIA_APP_ID'].present?
end
def associate_root_article(associated_article_id)
article = portal.articles.find(associated_article_id) if associated_article_id.present?
+4
View File
@@ -0,0 +1,4 @@
AlgoliaSearch.configuration = {
application_id: ENV.fetch('ALGOLIA_APP_ID', nil),
api_key: ENV.fetch('ALGOLIA_KEY', nil)
}