* Add tsvector index on listings * Fix sorting order when fetching tag flares * Add published_at as a sorting condition for Homepage::ArticlesQuery * Re-added param needed by ES, this got lost somewhere down the line
45 lines
1.4 KiB
Ruby
45 lines
1.4 KiB
Ruby
module Search
|
|
module Postgres
|
|
class Article
|
|
DEFAULT_SORT_BY = "hotness_score DESC, comments_count DESC".freeze
|
|
|
|
def self.search_documents(
|
|
term: nil,
|
|
user_id: nil,
|
|
sort_by: nil,
|
|
sort_direction: nil,
|
|
page: nil,
|
|
per_page: nil
|
|
)
|
|
relation = Homepage::ArticlesQuery.call(user_id: user_id, page: page, per_page: per_page)
|
|
|
|
relation = relation.search_articles(term) if term.present?
|
|
|
|
relation = sort(relation, sort_by, sort_direction)
|
|
|
|
tag_flares = Homepage::FetchTagFlares.call(relation)
|
|
|
|
# including user and organization as the last step as they are not needed
|
|
# by the query that fetches tag flares, they are only needed by the serializer
|
|
relation = relation.includes(:user, :organization)
|
|
|
|
serialize(relation, tag_flares)
|
|
end
|
|
|
|
def self.sort(relation, sort_by, sort_direction)
|
|
return relation.reorder(sort_by => sort_direction) if sort_by&.to_sym == :published_at
|
|
|
|
relation.reorder(DEFAULT_SORT_BY)
|
|
end
|
|
private_class_method :sort
|
|
|
|
def self.serialize(articles, tag_flares)
|
|
Homepage::ArticleSerializer
|
|
.new(articles, params: { tag_flares: tag_flares }, is_collection: true)
|
|
.serializable_hash[:data]
|
|
.pluck(:attributes)
|
|
end
|
|
private_class_method :serialize
|
|
end
|
|
end
|
|
end
|