2016-10-25 18:45:05 +02:00
|
|
|
# See:
|
|
|
|
# - https://robots.thoughtbot.com/implementing-multi-table-full-text-search-with-postgres
|
|
|
|
# - http://calebthompson.io/talks/search.html
|
2018-03-06 13:44:29 +01:00
|
|
|
class Search < ApplicationRecord
|
2016-10-29 00:53:04 +02:00
|
|
|
# :nodoc:
|
|
|
|
#
|
|
|
|
# Englobs a search result (actually a collection of Search objects) so it acts
|
|
|
|
# like a collection of regular Dossier objects, which can be decorated,
|
|
|
|
# paginated, ...
|
|
|
|
class Results
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
def initialize(results)
|
|
|
|
@results = results
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
@results.each do |search|
|
|
|
|
yield search.dossier
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
@results.__send__(name, *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decorate!
|
|
|
|
@results.each do |search|
|
|
|
|
search.dossier = search.dossier.decorate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-25 18:45:05 +02:00
|
|
|
attr_accessor :gestionnaire
|
|
|
|
attr_accessor :query
|
2016-10-29 00:53:04 +02:00
|
|
|
attr_accessor :page
|
2016-10-25 18:45:05 +02:00
|
|
|
|
|
|
|
belongs_to :dossier
|
|
|
|
|
|
|
|
def results
|
2018-01-11 19:04:39 +01:00
|
|
|
if @query.blank?
|
2016-10-29 00:53:04 +02:00
|
|
|
return Search.none
|
|
|
|
end
|
|
|
|
|
2016-11-04 17:11:26 +01:00
|
|
|
search_term = Search.connection.quote(to_tsquery)
|
2016-10-29 00:53:04 +02:00
|
|
|
|
2016-11-02 17:34:29 +01:00
|
|
|
dossier_ids = @gestionnaire.dossiers
|
|
|
|
.select(:id)
|
2017-07-10 16:15:09 +02:00
|
|
|
.not_archived
|
2017-07-10 16:11:12 +02:00
|
|
|
.state_not_brouillon
|
2016-11-02 17:34:29 +01:00
|
|
|
|
2016-11-04 17:11:26 +01:00
|
|
|
q = Search
|
2016-10-29 00:53:04 +02:00
|
|
|
.select("DISTINCT(searches.dossier_id)")
|
|
|
|
.select("COALESCE(ts_rank(to_tsvector('french', searches.term::text), to_tsquery('french', #{search_term})), 0) AS rank")
|
|
|
|
.joins(:dossier)
|
2016-11-02 17:34:29 +01:00
|
|
|
.where(dossier_id: dossier_ids)
|
2016-10-29 00:53:04 +02:00
|
|
|
.where("to_tsvector('french', searches.term::text) @@ to_tsquery('french', #{search_term})")
|
|
|
|
.order("rank DESC")
|
|
|
|
.preload(:dossier)
|
|
|
|
|
2016-11-04 17:11:26 +01:00
|
|
|
if @page.present?
|
|
|
|
q = q.paginate(page: @page)
|
2016-10-25 18:45:05 +02:00
|
|
|
end
|
2016-11-04 17:11:26 +01:00
|
|
|
|
|
|
|
Results.new(q)
|
2016-10-25 18:45:05 +02:00
|
|
|
end
|
|
|
|
|
2018-01-15 19:14:09 +01:00
|
|
|
# def self.refresh
|
|
|
|
# # TODO: could be executed concurrently
|
|
|
|
# # See https://github.com/thoughtbot/scenic#what-about-materialized-views
|
|
|
|
# Scenic.database.refresh_materialized_view(table_name, concurrently: false)
|
|
|
|
# end
|
2016-10-25 18:45:05 +02:00
|
|
|
|
2016-10-29 00:53:04 +02:00
|
|
|
private
|
2016-10-25 18:45:05 +02:00
|
|
|
|
2016-10-29 00:53:04 +02:00
|
|
|
def to_tsquery
|
|
|
|
@query.gsub(/['?\\:&|!]/, "") # drop disallowed characters
|
|
|
|
.split(/\s+/) # split words
|
|
|
|
.map { |x| "#{x}:*" } # enable prefix matching
|
|
|
|
.join(" & ")
|
2016-10-25 18:45:05 +02:00
|
|
|
end
|
|
|
|
end
|