From: Dan Pearl Date: Thu, 1 Oct 2015 13:49:17 +0000 (-0400) Subject: LP#1501781 - Make patron name search diacritic/space insensitive. X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=07c31e598c6777eb4c3652f62b5371084b26542c;p=working%2FEvergreen.git LP#1501781 - Make patron name search diacritic/space insensitive. Diacritical marks may exist in the patron record -- as they should, but this makes patron name search difficult for librarians who may be unfamiliar with all the special characters used and also how to elicit them from a keyboard. To ease this, accented characters will be converted into their 'plain' analogs for comparison purposes. So, for example, if the patron's last name is Chávez, typing "Chavez" in the Last Name box in Patron Search will match it. Spaces in a name (like "De la Croix") will be squashed out so that name would be matched by "Delacroix" or "De la Croix" or variants. The librarian can enter the accented characters or not. Signed-off-by: Dan Pearl Signed-off-by: Signed-off-by: --- diff --git a/Open-ILS/src/perlmods/Build.PL b/Open-ILS/src/perlmods/Build.PL index 5c323085b9..c732013bc4 100644 --- a/Open-ILS/src/perlmods/Build.PL +++ b/Open-ILS/src/perlmods/Build.PL @@ -82,6 +82,7 @@ my $build = Module::Build->new( 'Text::Aspell' => '0', 'Text::CSV' => '0', 'Text::Glob' => '0', + 'Text::Unaccent' => '0', 'Time::HiRes' => '0', 'Time::Local' => '0', 'Unicode::Normalize' => '0', diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/actor.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/actor.pm index f55da8c998..ff91435a1c 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/actor.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/actor.pm @@ -11,6 +11,8 @@ use DateTime::Format::ISO8601; use DateTime::Set; use DateTime::SpanSet; +use Text::Unaccent; + my $_dt_parser = DateTime::Format::ISO8601->new; my $log = 'OpenSRF::Utils::Logger'; @@ -614,6 +616,15 @@ __PACKAGE__->register_method( NOTE ); +sub _prepare_name_argument { + # Get rid of extra spaces, accents, and regex characters + my ($search) = _clean_regex_chars(@_); + $search =~ s/\s//g; + $search = unac_string('utf-8', $search); + + return $search; +}; + sub _clean_regex_chars { my ($search) = @_; @@ -661,8 +672,8 @@ sub patron_search { # group 2 = phone, ident # group 3 = barcode - my $usr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search; - my @usrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '0' } keys %$search; + my $usr = join ' AND ', map { "evergreen.unaccent_and_squash(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search; + my @usrv = map { "^" . _prepare_name_argument($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '0' } keys %$search; my $addr = join ' AND ', map { "evergreen.lowercase(CAST($_ AS text)) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search; my @addrv = map { "^" . _clean_regex_chars($$search{$_}{value}) } grep { ''.$$search{$_}{group} eq '1' } keys %$search; @@ -709,7 +720,7 @@ sub patron_search { my @namev; if (0 && $nv) { for my $n ( qw/first_given_name second_given_name family_name/ ) { - push @ns, "evergreen.lowercase($n) ~ ?"; + push @ns, "evergreen.unaccent_and_squash($n) ~ ?"; push @namev, "^$nv"; } $name = '(' . join(' OR ', @ns) . ')'; diff --git a/Open-ILS/src/sql/Pg/005.schema.actors.sql b/Open-ILS/src/sql/Pg/005.schema.actors.sql index 447d5957e3..5e087f2fe7 100644 --- a/Open-ILS/src/sql/Pg/005.schema.actors.sql +++ b/Open-ILS/src/sql/Pg/005.schema.actors.sql @@ -82,6 +82,9 @@ CREATE INDEX actor_usr_billing_address_idx ON actor.usr (billing_address); CREATE INDEX actor_usr_first_given_name_idx ON actor.usr (evergreen.lowercase(first_given_name)); CREATE INDEX actor_usr_second_given_name_idx ON actor.usr (evergreen.lowercase(second_given_name)); CREATE INDEX actor_usr_family_name_idx ON actor.usr (evergreen.lowercase(family_name)); +CREATE INDEX actor_usr_first_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(first_given_name)); +CREATE INDEX actor_usr_second_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(second_given_name)); +CREATE INDEX actor_usr_family_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(family_name)); CREATE INDEX actor_usr_usrname_idx ON actor.usr (evergreen.lowercase(usrname)); CREATE INDEX actor_usr_email_idx ON actor.usr (evergreen.lowercase(email)); diff --git a/Open-ILS/src/sql/Pg/create_database_extensions.sql b/Open-ILS/src/sql/Pg/create_database_extensions.sql index b73a87168e..33f75232df 100644 --- a/Open-ILS/src/sql/Pg/create_database_extensions.sql +++ b/Open-ILS/src/sql/Pg/create_database_extensions.sql @@ -20,3 +20,4 @@ CREATE EXTENSION tablefunc; CREATE EXTENSION xml2; CREATE EXTENSION hstore; CREATE EXTENSION intarray; +CREATE EXTENSION unaccent; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.patron_unaccent.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.patron_unaccent.sql new file mode 100644 index 0000000000..95d2e0d6d1 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.patron_unaccent.sql @@ -0,0 +1,14 @@ +CREATE EXTENSION unaccent; + +CREATE OR REPLACE FUNCTION evergreen.unaccent_and_squash ( IN arg text) RETURNS text + IMMUTABLE STRICT AS $$ + BEGIN + RETURN evergreen.lowercase(evergreen.unaccent(regexp_replace(arg, '\s','','g'))); + END; +$$ LANGUAGE PLPGSQL; + +-- The unaccented indices for patron name fields are best created CONCURRENTLY, +-- as they could be time-consuming. +CREATE INDEX CONCURRENTLY actor_usr_first_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(first_given_name)); +CREATE INDEX CONCURRENTLY actor_usr_second_given_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(second_given_name)); +CREATE INDEX CONCURRENTLY actor_usr_family_name_unaccent_idx ON actor.usr (evergreen.unaccent_and_squash(family_name)); diff --git a/docs/RELEASE_NOTES_NEXT/Client/accent_insensitive_patron_search b/docs/RELEASE_NOTES_NEXT/Client/accent_insensitive_patron_search new file mode 100644 index 0000000000..30200eacdb --- /dev/null +++ b/docs/RELEASE_NOTES_NEXT/Client/accent_insensitive_patron_search @@ -0,0 +1,16 @@ +Accent Insensitive Patron Search +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +When performing a patron search, in addition to the (existing) +case-insensitivity, these additional characteristics will govern the +search: + +Accent (diacritic) insensitivity:: +Diacritics will be transformed into a plain character equivalent for comparison purposes. So if the patron name is Eugène Delacroix, for example, you could enter +euge for the First Name, and it would match. Ligatures such as Œ are expanded into the constituent characters "OE". + +Space insensitivity:: +Spaces will be squashed out for comparison purposes. If the patron is, again, +Eugène Delacroix, you could enter "de la croix" in the Last Name field and it would match. + +This behavior affects the Last Name, First Name, and Middle Name fields +of the search.