refactor mutualize complicated part of the search
This commit is contained in:
parent
11402c6ee2
commit
6472d738d6
1 changed files with 12 additions and 11 deletions
|
@ -6,7 +6,7 @@ class DossierSearchService
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
dossier_by_exact_id(dossiers, search_terms)
|
dossier_by_exact_id(dossiers, search_terms)
|
||||||
.presence || dossier_by_full_text(dossiers, search_terms, with_annotations)
|
.presence || dossier_ids_by_full_text(dossiers, search_terms, with_annotations)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,24 +26,25 @@ class DossierSearchService
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.dossier_by_full_text(dossiers, search_terms, with_annotations)
|
def self.dossier_ids_by_full_text(dossiers, search_terms, with_annotations)
|
||||||
ts_vector = "to_tsvector('french', unaccent(#{with_annotations ? 'dossiers.search_terms || dossiers.private_search_terms' : 'dossiers.search_terms'}))"
|
columns = with_annotations ? 'search_terms || private_search_terms' : 'search_terms'
|
||||||
ts_query = "to_tsquery('french', unaccent(#{Dossier.connection.quote(to_tsquery(search_terms))}))"
|
|
||||||
|
|
||||||
dossiers
|
dossier_by_full_text(dossiers.visible_by_administration, columns, search_terms)
|
||||||
.visible_by_administration
|
|
||||||
.where("#{ts_vector} @@ #{ts_query}")
|
|
||||||
.order(Arel.sql("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC"))
|
|
||||||
.pluck('id')
|
.pluck('id')
|
||||||
.uniq
|
.uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.dossier_by_full_text_for_user(search_terms, dossiers)
|
def self.dossier_by_full_text_for_user(search_terms, dossiers)
|
||||||
ts_vector = "to_tsvector('french', search_terms)"
|
columns = 'search_terms'
|
||||||
ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})"
|
|
||||||
|
dossier_by_full_text(dossiers.visible_by_user, columns, search_terms)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.dossier_by_full_text(dossiers, columns, search_terms)
|
||||||
|
ts_vector = "to_tsvector('french', unaccent(#{columns}))"
|
||||||
|
ts_query = "to_tsquery('french', unaccent(#{Dossier.connection.quote(to_tsquery(search_terms))}))"
|
||||||
|
|
||||||
dossiers
|
dossiers
|
||||||
.visible_by_user
|
|
||||||
.where("#{ts_vector} @@ #{ts_query}")
|
.where("#{ts_vector} @@ #{ts_query}")
|
||||||
.order(Arel.sql("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC"))
|
.order(Arel.sql("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC"))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue