From d681b1116f1a553d1beb84e6ca29306a78e5fc03 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Tue, 31 Jul 2018 15:08:10 +0200 Subject: [PATCH] [Fix #2179] Use new full text search --- app/services/dossier_search_service.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/services/dossier_search_service.rb b/app/services/dossier_search_service.rb index e5ce40c04..7bfb521e9 100644 --- a/app/services/dossier_search_service.rb +++ b/app/services/dossier_search_service.rb @@ -27,9 +27,21 @@ class DossierSearchService end def self.dossier_by_full_text_for_gestionnaire(search_terms, gestionnaire) - Search.new( - gestionnaire: gestionnaire, - query: search_terms - ).results + ts_vector = "to_tsvector('french', search_terms || private_search_terms)" + ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})" + + gestionnaire + .dossiers + .not_archived + .state_not_brouillon + .where("#{ts_vector} @@ #{ts_query}") + .order("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC") + end + + def self.to_tsquery(search_terms) + search_terms.gsub(/['?\\:&|!]/, "") # drop disallowed characters + .split(/\s+/) # split words + .map { |x| "#{x}:*" } # enable prefix matching + .join(" & ") end end