Drop textacular (to enable prefix matching) + disable materialized view (for the time being) + specs

This commit is contained in:
Julien Portalier 2016-10-29 00:53:04 +02:00
parent 6bb1ad892f
commit f9a5e5c5ff
8 changed files with 223 additions and 205 deletions

View file

@ -31,10 +31,11 @@ class Backoffice::DossiersController < Backoffice::DossiersListController
@dossier = Search.new(
gestionnaire: current_gestionnaire,
query: @search_terms,
page: params[:page]
).results
unless @dossier.empty?
@dossiers = @dossiers.paginate(page: params[:page])
@dossier = @dossier.paginate(page: params[:page])
end
smartlisting_dossier @dossier, 'search'

View file

@ -258,37 +258,6 @@ class Dossier < ActiveRecord::Base
where(state: TERMINE, archived: false).order("updated_at #{order}")
end
def self.search current_gestionnaire, terms
return [] if terms.blank?
dossiers = Dossier.arel_table
users = User.arel_table
etablissements = Etablissement.arel_table
entreprises = Entreprise.arel_table
composed_scope = self.joins('LEFT OUTER JOIN users ON users.id = dossiers.user_id')
.joins('LEFT OUTER JOIN entreprises ON entreprises.dossier_id = dossiers.id')
.joins('LEFT OUTER JOIN etablissements ON etablissements.dossier_id = dossiers.id')
terms.split.each do |word|
query_string = "%#{word}%"
query_string_start_with = "#{word}%"
composed_scope = composed_scope.where(
users[:email].matches(query_string).or\
etablissements[:siret].matches(query_string_start_with).or\
entreprises[:raison_sociale].matches(query_string).or\
dossiers[:id].eq(word_is_an_integer word))
end
composed_scope = composed_scope.where(
dossiers[:id].eq_any(current_gestionnaire.dossiers.ids).and\
dossiers[:state].does_not_match('draft').and\
dossiers[:archived].eq(false))
composed_scope
end
def cerfa_available?
procedure.cerfa_flag? && cerfa.size != 0
end

View file

@ -2,40 +2,82 @@
# - https://robots.thoughtbot.com/implementing-multi-table-full-text-search-with-postgres
# - http://calebthompson.io/talks/search.html
class Search < ActiveRecord::Base
extend Textacular
# :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
#extend Textacular
attr_accessor :gestionnaire
attr_accessor :query
attr_accessor :page
belongs_to :dossier
def results
if @query.present?
self.class
unless @query.present?
return Search.none
end
search_term = self.class.connection.quote(to_tsquery)
q = self.class
.select("DISTINCT(searches.dossier_id)")
.search(@query)
.select("COALESCE(ts_rank(to_tsvector('french', searches.term::text), to_tsquery('french', #{search_term})), 0) AS rank")
.joins(:dossier)
.where(dossier_id: @gestionnaire.dossier_ids)
.where("dossiers.archived = ? AND dossiers.state != ?", false, "draft")
.where("to_tsvector('french', searches.term::text) @@ to_tsquery('french', #{search_term})")
.order("rank DESC")
.paginate(page: @page)
.preload(:dossier)
.map(&:dossier)
else
begin
q.to_a
rescue ActiveRecord::StatementInvalid
Search.none
else
Results.new(q)
end
end
def self.searchable_language
"french"
end
#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
def self.searchable_columns
%i(term)
end
private
# 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)
def to_tsquery
@query.gsub(/['?\\:&|!]/, "") # drop disallowed characters
.split(/\s+/) # split words
.map { |x| "#{x}:*" } # enable prefix matching
.join(" & ")
end
end

View file

@ -9,7 +9,7 @@ class CreateSearches < ActiveRecord::Migration
add_index :individuals, :dossier_id
add_index :pieces_justificatives, :dossier_id
add_index :rna_informations, :entreprise_id
create_view :searches, materialized: true
create_view :searches #, materialized: true
end
def down
@ -22,6 +22,6 @@ class CreateSearches < ActiveRecord::Migration
remove_index :individuals, :dossier_id
remove_index :pieces_justificatives, :dossier_id
remove_index :rna_informations, :entreprise_id
drop_view :searches
drop_view :searches #, materialized: true
end
end

View file

@ -954,45 +954,68 @@ CREATE TABLE schema_migrations (
--
-- Name: searches; Type: MATERIALIZED VIEW; Schema: public; Owner: -
-- Name: users; Type: TABLE; Schema: public; Owner: -
--
CREATE MATERIALIZED VIEW searches AS
CREATE TABLE users (
id integer NOT NULL,
email character varying DEFAULT ''::character varying NOT NULL,
encrypted_password character varying DEFAULT ''::character varying NOT NULL,
reset_password_token character varying,
reset_password_sent_at timestamp without time zone,
remember_created_at timestamp without time zone,
sign_in_count integer DEFAULT 0 NOT NULL,
current_sign_in_at timestamp without time zone,
last_sign_in_at timestamp without time zone,
current_sign_in_ip inet,
last_sign_in_ip inet,
created_at timestamp without time zone,
updated_at timestamp without time zone,
siret character varying,
loged_in_with_france_connect character varying DEFAULT false
);
--
-- Name: searches; Type: VIEW; Schema: public; Owner: -
--
CREATE VIEW searches AS
SELECT dossiers.id AS dossier_id,
(dossiers.id)::text AS term
FROM dossiers
(((dossiers.id)::text || ' '::text) || (COALESCE(users.email, ''::character varying))::text) AS term
FROM (dossiers
JOIN users ON ((users.id = dossiers.user_id)))
UNION
SELECT cerfas.dossier_id,
cerfas.content AS term
COALESCE(cerfas.content, ''::character varying) AS term
FROM cerfas
UNION
SELECT champs.dossier_id,
(((champs.value)::text || ' '::text) || (drop_down_lists.value)::text) AS term
(((COALESCE(champs.value, ''::character varying))::text || ' '::text) || (COALESCE(drop_down_lists.value, ''::character varying))::text) AS term
FROM (champs
JOIN drop_down_lists ON ((drop_down_lists.type_de_champ_id = champs.type_de_champ_id)))
UNION
SELECT entreprises.dossier_id,
(((((((((((((((((((((((entreprises.siren)::text || ' '::text) || (entreprises.numero_tva_intracommunautaire)::text) || ' '::text) || (entreprises.forme_juridique)::text) || ' '::text) || (entreprises.forme_juridique_code)::text) || ' '::text) || (entreprises.nom_commercial)::text) || ' '::text) || (entreprises.raison_sociale)::text) || ' '::text) || (entreprises.siret_siege_social)::text) || ' '::text) || (entreprises.nom)::text) || ' '::text) || (entreprises.prenom)::text) || ' '::text) || (rna_informations.association_id)::text) || ' '::text) || (rna_informations.titre)::text) || ' '::text) || rna_informations.objet) AS term
(((((((((((((((((((((((COALESCE(entreprises.siren, ''::character varying))::text || ' '::text) || (COALESCE(entreprises.numero_tva_intracommunautaire, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.forme_juridique, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.forme_juridique_code, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.nom_commercial, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.raison_sociale, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.siret_siege_social, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.nom, ''::character varying))::text) || ' '::text) || (COALESCE(entreprises.prenom, ''::character varying))::text) || ' '::text) || (COALESCE(rna_informations.association_id, ''::character varying))::text) || ' '::text) || (COALESCE(rna_informations.titre, ''::character varying))::text) || ' '::text) || COALESCE(rna_informations.objet, ''::text)) AS term
FROM (entreprises
JOIN rna_informations ON ((rna_informations.entreprise_id = entreprises.id)))
LEFT JOIN rna_informations ON ((rna_informations.entreprise_id = entreprises.id)))
UNION
SELECT etablissements.dossier_id,
(((((((((((((etablissements.siret)::text || ' '::text) || (etablissements.naf)::text) || ' '::text) || (etablissements.libelle_naf)::text) || ' '::text) || (etablissements.adresse)::text) || ' '::text) || (etablissements.code_postal)::text) || ' '::text) || (etablissements.localite)::text) || ' '::text) || (etablissements.code_insee_localite)::text) AS term
(((((((((((((COALESCE(etablissements.siret, ''::character varying))::text || ' '::text) || (COALESCE(etablissements.naf, ''::character varying))::text) || ' '::text) || (COALESCE(etablissements.libelle_naf, ''::character varying))::text) || ' '::text) || (COALESCE(etablissements.adresse, ''::character varying))::text) || ' '::text) || (COALESCE(etablissements.code_postal, ''::character varying))::text) || ' '::text) || (COALESCE(etablissements.localite, ''::character varying))::text) || ' '::text) || (COALESCE(etablissements.code_insee_localite, ''::character varying))::text) AS term
FROM etablissements
UNION
SELECT individuals.dossier_id,
(((individuals.nom)::text || ' '::text) || (individuals.prenom)::text) AS term
(((COALESCE(individuals.nom, ''::character varying))::text || ' '::text) || (COALESCE(individuals.prenom, ''::character varying))::text) AS term
FROM individuals
UNION
SELECT pieces_justificatives.dossier_id,
pieces_justificatives.content AS term
COALESCE(pieces_justificatives.content, ''::character varying) AS term
FROM pieces_justificatives
UNION
SELECT dossiers.id AS dossier_id,
(((france_connect_informations.given_name)::text || ' '::text) || (france_connect_informations.family_name)::text) AS term
(((COALESCE(france_connect_informations.given_name, ''::character varying))::text || ' '::text) || (COALESCE(france_connect_informations.family_name, ''::character varying))::text) AS term
FROM (france_connect_informations
JOIN dossiers ON ((dossiers.user_id = france_connect_informations.user_id)))
WITH NO DATA;
JOIN dossiers ON ((dossiers.user_id = france_connect_informations.user_id)));
--
@ -1065,29 +1088,6 @@ CREATE SEQUENCE types_de_piece_justificative_id_seq
ALTER SEQUENCE types_de_piece_justificative_id_seq OWNED BY types_de_piece_justificative.id;
--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE users (
id integer NOT NULL,
email character varying DEFAULT ''::character varying NOT NULL,
encrypted_password character varying DEFAULT ''::character varying NOT NULL,
reset_password_token character varying,
reset_password_sent_at timestamp without time zone,
remember_created_at timestamp without time zone,
sign_in_count integer DEFAULT 0 NOT NULL,
current_sign_in_at timestamp without time zone,
last_sign_in_at timestamp without time zone,
current_sign_in_ip inet,
last_sign_in_ip inet,
created_at timestamp without time zone,
updated_at timestamp without time zone,
siret character varying,
loged_in_with_france_connect character varying DEFAULT false
);
--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

View file

@ -1,54 +1,56 @@
SELECT dossiers.id AS dossier_id,
dossiers.id::text AS term
dossiers.id::text || ' ' ||
COALESCE(users.email, '') AS term
FROM dossiers
INNER JOIN users ON users.id = dossiers.user_id
UNION SELECT cerfas.dossier_id,
cerfas.content AS term
COALESCE(cerfas.content, '') AS term
FROM cerfas
UNION SELECT champs.dossier_id,
champs.value || ' ' ||
drop_down_lists.value AS term
COALESCE(champs.value, '') || ' ' ||
COALESCE(drop_down_lists.value, '') AS term
FROM champs
INNER JOIN drop_down_lists ON drop_down_lists.type_de_champ_id = champs.type_de_champ_id
UNION SELECT entreprises.dossier_id,
entreprises.siren || ' ' ||
entreprises.numero_tva_intracommunautaire || ' ' ||
entreprises.forme_juridique || ' ' ||
entreprises.forme_juridique_code || ' ' ||
entreprises.nom_commercial || ' ' ||
entreprises.raison_sociale || ' ' ||
entreprises.siret_siege_social || ' ' ||
entreprises.nom || ' ' ||
entreprises.prenom || ' ' ||
rna_informations.association_id || ' ' ||
rna_informations.titre || ' ' ||
rna_informations.objet AS term
COALESCE(entreprises.siren, '') || ' ' ||
COALESCE(entreprises.numero_tva_intracommunautaire, '') || ' ' ||
COALESCE(entreprises.forme_juridique, '') || ' ' ||
COALESCE(entreprises.forme_juridique_code, '') || ' ' ||
COALESCE(entreprises.nom_commercial, '') || ' ' ||
COALESCE(entreprises.raison_sociale, '') || ' ' ||
COALESCE(entreprises.siret_siege_social, '') || ' ' ||
COALESCE(entreprises.nom, '') || ' ' ||
COALESCE(entreprises.prenom, '') || ' ' ||
COALESCE(rna_informations.association_id, '') || ' ' ||
COALESCE(rna_informations.titre, '') || ' ' ||
COALESCE(rna_informations.objet, '') AS term
FROM entreprises
INNER JOIN rna_informations ON rna_informations.entreprise_id = entreprises.id
LEFT JOIN rna_informations ON rna_informations.entreprise_id = entreprises.id
UNION SELECT etablissements.dossier_id,
etablissements.siret || ' ' ||
etablissements.naf || ' ' ||
etablissements.libelle_naf || ' ' ||
etablissements.adresse || ' ' ||
etablissements.code_postal || ' ' ||
etablissements.localite || ' ' ||
etablissements.code_insee_localite AS term
COALESCE(etablissements.siret, '') || ' ' ||
COALESCE(etablissements.naf, '') || ' ' ||
COALESCE(etablissements.libelle_naf, '') || ' ' ||
COALESCE(etablissements.adresse, '') || ' ' ||
COALESCE(etablissements.code_postal, '') || ' ' ||
COALESCE(etablissements.localite, '') || ' ' ||
COALESCE(etablissements.code_insee_localite, '') AS term
FROM etablissements
UNION SELECT individuals.dossier_id,
individuals.nom || ' ' ||
individuals.prenom AS term
COALESCE(individuals.nom, '') || ' ' ||
COALESCE(individuals.prenom, '') AS term
FROM individuals
UNION SELECT pieces_justificatives.dossier_id,
pieces_justificatives.content AS term
COALESCE(pieces_justificatives.content, '') AS term
FROM pieces_justificatives
UNION SELECT dossiers.id,
france_connect_informations.given_name || ' ' ||
france_connect_informations.family_name AS term
COALESCE(france_connect_informations.given_name, '') || ' ' ||
COALESCE(france_connect_informations.family_name, '') AS term
FROM france_connect_informations
INNER JOIN dossiers ON dossiers.user_id = france_connect_informations.user_id

View file

@ -556,87 +556,6 @@ describe Dossier do
end
end
describe '.search' do
subject { liste_dossiers }
let(:liste_dossiers) { described_class.search(gestionnaire_1, terms) }
# let(:dossier) { described_class.search(gestionnaire_1, terms)[1] }
let(:administrateur_1) { create(:administrateur) }
let(:administrateur_2) { create(:administrateur) }
let(:gestionnaire_1) { create(:gestionnaire, administrateurs: [administrateur_1]) }
let(:gestionnaire_2) { create(:gestionnaire, administrateurs: [administrateur_2]) }
before do
create :assign_to, gestionnaire: gestionnaire_1, procedure: procedure_1
create :assign_to, gestionnaire: gestionnaire_2, procedure: procedure_2
end
let(:procedure_1) { create(:procedure, administrateur: administrateur_1) }
let(:procedure_2) { create(:procedure, administrateur: administrateur_2) }
let!(:dossier_0) { create(:dossier, state: 'draft', procedure: procedure_1, user: create(:user, email: 'brouillon@clap.fr')) }
let!(:dossier_1) { create(:dossier, state: 'initiated', procedure: procedure_1, user: create(:user, email: 'contact@test.com')) }
let!(:dossier_2) { create(:dossier, state: 'initiated', procedure: procedure_1, user: create(:user, email: 'plop@gmail.com')) }
let!(:dossier_3) { create(:dossier, state: 'initiated', procedure: procedure_2, user: create(:user, email: 'peace@clap.fr')) }
let!(:dossier_archived) { create(:dossier, state: 'initiated', procedure: procedure_1, archived: true, user: create(:user, email: 'brouillonArchived@clap.fr')) }
let!(:etablissement_1) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Academy', dossier: dossier_1), dossier: dossier_1, siret: '41636169600051') }
let!(:etablissement_2) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'Plop octo', dossier: dossier_2), dossier: dossier_2, siret: '41816602300012') }
let!(:etablissement_3) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Technology', dossier: dossier_3), dossier: dossier_3, siret: '41816609600051') }
describe 'search is empty' do
let(:terms) { '' }
it { expect(subject.size).to eq(0) }
end
describe 'search draft file' do
let(:terms) { 'brouillon' }
it { expect(subject.size).to eq(0) }
it { expect(subject.class).to eq Dossier::ActiveRecord_Relation }
end
describe 'search on contact email' do
let(:terms) { 'clap' }
it { expect(subject.size).to eq(0) }
end
describe 'search on ID dossier' do
let(:terms) { "#{dossier_2.id}" }
it { expect(subject.size).to eq(1) }
end
describe 'search on SIRET' do
context 'when is part of SIRET' do
let(:terms) { '4181' }
it { expect(subject.size).to eq(1) }
end
context 'when is a complet SIRET' do
let(:terms) { '41816602300012' }
it { expect(subject.size).to eq(1) }
end
end
describe 'search on raison social' do
let(:terms) { 'OCTO' }
it { expect(subject.size).to eq(2) }
end
describe 'search on multiple fields' do
let(:terms) { 'octo test' }
it { expect(subject.size).to eq(1) }
end
end
end
describe '#cerfa_available?' do

View file

@ -1,5 +1,90 @@
require 'rails_helper'
RSpec.describe Search, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe Search do
describe '.results' do
subject { liste_dossiers }
let(:liste_dossiers) do
#described_class.refresh
described_class.new(gestionnaire: gestionnaire_1, query: terms).results
end
#let(:dossier) do
# described_class.refresh
# described_class.search(gestionnaire: gestionnaire_1, query: terms)[1]
#end
let(:administrateur_1) { create(:administrateur) }
let(:administrateur_2) { create(:administrateur) }
let(:gestionnaire_1) { create(:gestionnaire, administrateurs: [administrateur_1]) }
let(:gestionnaire_2) { create(:gestionnaire, administrateurs: [administrateur_2]) }
before do
create :assign_to, gestionnaire: gestionnaire_1, procedure: procedure_1
create :assign_to, gestionnaire: gestionnaire_2, procedure: procedure_2
end
let(:procedure_1) { create(:procedure, administrateur: administrateur_1) }
let(:procedure_2) { create(:procedure, administrateur: administrateur_2) }
let!(:dossier_0) { create(:dossier, state: 'draft', procedure: procedure_1, user: create(:user, email: 'brouillon@clap.fr')) }
let!(:dossier_1) { create(:dossier, state: 'initiated', procedure: procedure_1, user: create(:user, email: 'contact@test.com')) }
let!(:dossier_2) { create(:dossier, state: 'initiated', procedure: procedure_1, user: create(:user, email: 'plop@gmail.com')) }
let!(:dossier_3) { create(:dossier, state: 'initiated', procedure: procedure_2, user: create(:user, email: 'peace@clap.fr')) }
let!(:dossier_archived) { create(:dossier, state: 'initiated', procedure: procedure_1, archived: true, user: create(:user, email: 'brouillonArchived@clap.fr')) }
let!(:etablissement_1) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Academy', dossier: dossier_1), dossier: dossier_1, siret: '41636169600051') }
let!(:etablissement_2) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'Plop octo', dossier: dossier_2), dossier: dossier_2, siret: '41816602300012') }
let!(:etablissement_3) { create(:etablissement, entreprise: create(:entreprise, raison_sociale: 'OCTO Technology', dossier: dossier_3), dossier: dossier_3, siret: '41816609600051') }
describe 'search is empty' do
let(:terms) { '' }
it { expect(subject.size).to eq(0) }
end
describe 'search draft file' do
let(:terms) { 'brouillon' }
it { expect(subject.size).to eq(0) }
end
describe 'search on contact email' do
let(:terms) { 'clap' }
it { expect(subject.size).to eq(0) }
end
#describe 'search on ID dossier' do
# let(:terms) { "#{dossier_2.id}" }
# it { expect(dossier.id).to eq(dossier_2.id) }
#end
describe 'search on SIRET' do
context 'when is part of SIRET' do
let(:terms) { '4181' }
it { expect(subject.size).to eq(1) }
end
context 'when is a complet SIRET' do
let(:terms) { '41816602300012' }
it { expect(subject.size).to eq(1) }
end
end
describe 'search on raison social' do
let(:terms) { 'OCTO' }
it { expect(subject.size).to eq(2) }
end
describe 'search on multiple fields' do
let(:terms) { 'octo test' }
pending { expect(subject.size).to eq(1) }
end
end
end