demarches-normaliennes/app/models/search.rb
Julien Portalier 6bb1ad892f Fixes for PostgreSQL full text search
- Drop individual GIN indices that aren't used by the search query.
- Add missing indices to speed up view query (missing dossier_id)
- Fix view query for drop_down_lists (and merge it with champs)
2016-11-04 15:34:35 +01:00

41 lines
1 KiB
Ruby

# See:
# - https://robots.thoughtbot.com/implementing-multi-table-full-text-search-with-postgres
# - http://calebthompson.io/talks/search.html
class Search < ActiveRecord::Base
extend Textacular
attr_accessor :gestionnaire
attr_accessor :query
belongs_to :dossier
def results
if @query.present?
self.class
.select("DISTINCT(searches.dossier_id)")
.search(@query)
.joins(:dossier)
.where(dossier_id: @gestionnaire.dossier_ids)
.where("dossiers.archived = ? AND dossiers.state != ?", false, "draft")
.preload(:dossier)
.map(&:dossier)
else
Search.none
end
end
def self.searchable_language
"french"
end
def self.searchable_columns
%i(term)
end
# Refreshes the materialized searches view.
def self.refresh
# NOTE: could be executed concurrently
# See https://github.com/thoughtbot/scenic#what-about-materialized-views
Scenic.database.refresh_materialized_view(table_name, concurrently: false)
end
end