Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a758f1a2b | ||
|
|
d17fa149a6 | ||
|
|
3bde1becbd |
@@ -176,6 +176,9 @@ gem 'reverse_markdown'
|
|||||||
# Sentiment analysis
|
# Sentiment analysis
|
||||||
gem 'informers'
|
gem 'informers'
|
||||||
|
|
||||||
|
# Algolia
|
||||||
|
gem "algoliasearch-rails"
|
||||||
|
|
||||||
### Gems required only in specific deployment environments ###
|
### Gems required only in specific deployment environments ###
|
||||||
##############################################################
|
##############################################################
|
||||||
|
|
||||||
|
|||||||
@@ -121,6 +121,14 @@ GEM
|
|||||||
jbuilder (~> 2)
|
jbuilder (~> 2)
|
||||||
rails (>= 4.2, < 7.1)
|
rails (>= 4.2, < 7.1)
|
||||||
selectize-rails (~> 0.6)
|
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)
|
annotate (3.2.0)
|
||||||
activerecord (>= 3.2, < 8.0)
|
activerecord (>= 3.2, < 8.0)
|
||||||
rake (>= 10.4, < 14.0)
|
rake (>= 10.4, < 14.0)
|
||||||
@@ -838,6 +846,7 @@ DEPENDENCIES
|
|||||||
administrate (>= 0.19.0)
|
administrate (>= 0.19.0)
|
||||||
administrate-field-active_storage
|
administrate-field-active_storage
|
||||||
administrate-field-belongs_to_search
|
administrate-field-belongs_to_search
|
||||||
|
algoliasearch-rails
|
||||||
annotate
|
annotate
|
||||||
attr_extras
|
attr_extras
|
||||||
audited (~> 5.4, >= 5.4.1)
|
audited (~> 5.4, >= 5.4.1)
|
||||||
|
|||||||
+39
-1
@@ -28,6 +28,7 @@
|
|||||||
#
|
#
|
||||||
class Article < ApplicationRecord
|
class Article < ApplicationRecord
|
||||||
include PgSearch::Model
|
include PgSearch::Model
|
||||||
|
include AlgoliaSearch
|
||||||
|
|
||||||
has_many :associated_articles,
|
has_many :associated_articles,
|
||||||
class_name: :Article,
|
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(
|
records = joins(
|
||||||
:category
|
:category
|
||||||
).search_by_category_slug(
|
).search_by_category_slug(
|
||||||
@@ -93,6 +119,18 @@ class Article < ApplicationRecord
|
|||||||
records
|
records
|
||||||
end
|
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)
|
def associate_root_article(associated_article_id)
|
||||||
article = portal.articles.find(associated_article_id) if associated_article_id.present?
|
article = portal.articles.find(associated_article_id) if associated_article_id.present?
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
AlgoliaSearch.configuration = {
|
||||||
|
application_id: ENV.fetch('ALGOLIA_APP_ID', nil),
|
||||||
|
api_key: ENV.fetch('ALGOLIA_KEY', nil)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user