Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full text admin user search #3133

Merged
merged 3 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/update-schema
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ else {
# (assuming schema change files are never half-applied, which should be the case)
sub get_db_version {
return 'EMPTY' if ! table_exists('problem');
return '0074' if index_exists('users_fulltext_idx');
return '0073' if index_exists('problem_fulltext_idx');
return '0072' if constraint_contains('contacts_state_check', 'staff');
return '0071' if table_exists('manifest_theme');
Expand Down
1 change: 1 addition & 0 deletions db/downgrade_0074---0073.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX users_fulltext_idx;
6 changes: 6 additions & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ create table users (
);
CREATE UNIQUE INDEX users_email_verified_unique ON users (email) WHERE email_verified;
CREATE UNIQUE INDEX users_phone_verified_unique ON users (phone) WHERE phone_verified;
create index users_fulltext_idx on users USING GIN(
to_tsvector(
'english',
translate(id || ' ' || coalesce(name,'') || ' ' || coalesce(email,'') || ' ' || coalesce(phone,''), '@.', ' ')
)
);

-- Record details of reporting bodies, including open311 configuration details
create table body (
Expand Down
6 changes: 6 additions & 0 deletions db/schema_0074-add-users-full-text-search-index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE INDEX CONCURRENTLY users_fulltext_idx on users USING GIN(
to_tsvector(
'DB_FULL_TEXT_SEARCH_CONFIG',
translate(id || ' ' || coalesce(name,'') || ' ' || coalesce(email,'') || ' ' || coalesce(phone,''), '@.', ' ')
)
);
4 changes: 4 additions & 0 deletions perllib/FixMyStreet/App/Controller/Admin/Reports.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ sub index : Path {
$c->stash->{dir} = $dir;
$order = $dir ? { -desc => "me.$order" } : "me.$order";

# The below is added so that PostgreSQL does not try and use other indexes
# besides the full text search. It should have no impact on results shown.
$order = [ $order, { -desc => "me.id" }, { -desc => "me.created" } ];

my $p_page = $c->get_param('p') || 1;
my $u_page = $c->get_param('u') || 1;

Expand Down
17 changes: 1 addition & 16 deletions perllib/FixMyStreet/App/Controller/Admin/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,11 @@ sub index :Path : Args(0) {
my $role = $c->get_param('role');
my $users = $c->cobrand->users;
if ($search || $role) {
my $isearch;
if ($search) {
$search = $self->trim($search);
$search =~ s/^<(.*)>$/$1/; # In case email wrapped in <...>
$c->stash->{searched} = $search;

$isearch = '%' . $search . '%';
my $search_n = 0;
$search_n = int($search) if $search =~ /^\d+$/;

$users = $users->search(
{
-or => [
email => { ilike => $isearch },
phone => { ilike => $isearch },
'me.name' => { ilike => $isearch },
from_body => $search_n,
]
}
);
$users = $users->search_text($search);
}
if ($role) {
$c->stash->{role_selected} = $role;
Expand Down
1 change: 1 addition & 0 deletions perllib/FixMyStreet/DB/ResultSet/Comment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ with 'FixMyStreet::Roles::FullTextSearch';
__PACKAGE__->load_components('Helper::ResultSet::Me');
sub text_search_columns { qw(id problem_id name text) }
sub text_search_nulls { qw(name) }
sub text_search_translate { '/.' }

sub to_body {
my ($rs, $bodies) = @_;
Expand Down
1 change: 1 addition & 0 deletions perllib/FixMyStreet/DB/ResultSet/Problem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ with 'FixMyStreet::Roles::FullTextSearch';
__PACKAGE__->load_components('Helper::ResultSet::Me');
sub text_search_columns { qw(id external_id bodies_str name title detail) }
sub text_search_nulls { qw(external_id bodies_str) }
sub text_search_translate { '/.' }

my $site_key;

Expand Down
5 changes: 5 additions & 0 deletions perllib/FixMyStreet/DB/ResultSet/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use strict;
use warnings;

use Moo;
with 'FixMyStreet::Roles::FullTextSearch';
__PACKAGE__->load_components('Helper::ResultSet::Me');
sub text_search_columns { qw(id name email phone) }
sub text_search_nulls { qw(name email phone) }
sub text_search_translate { '@.' }

# The database has a partial unique index on email (when email_verified is
# true), and phone (when phone_verified is true). In the code, we can only
Expand Down
11 changes: 9 additions & 2 deletions perllib/FixMyStreet/Roles/FullTextSearch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use FixMyStreet;

requires 'text_search_columns';
requires 'text_search_nulls';
requires 'text_search_translate';

sub search_text {
my ($rs, $query) = @_;
Expand All @@ -13,9 +14,15 @@ sub search_text {
my $col = $rs->me($_);
$nulls{$_} ? "coalesce($col, '')" : $col;
} $rs->text_search_columns;
my $vector = "translate(" . join(" || ' ' || ", @cols) . ", '/.', ' ')";
my $vector = join(" || ' ' || ", @cols);
my $bind = '?';
if (my $trans = $rs->text_search_translate) {
my $replace = ' ' x length $trans;
$vector = "translate($vector, '$trans', '$replace')";
$bind = "translate(?, '$trans', '$replace')";
}
my $config = FixMyStreet->config('DB_FULL_TEXT_SEARCH_CONFIG') || 'english';
$rs->search(\[ "to_tsvector('$config', $vector) @@ plainto_tsquery('$config', ?)", $query ]);
$rs->search(\[ "to_tsvector('$config', $vector) @@ plainto_tsquery('$config', $bind)", $query ]);
}

1;
Expand Down
2 changes: 0 additions & 2 deletions t/app/controller/admin/users.t
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ subtest 'user search' => sub {
permissions => ['moderate', 'user_edit'],
});
$user->add_to_roles($role);
$mech->get_ok('/admin/users?search=' . $haringey->id );
$mech->content_contains('test@example.com');
$mech->get_ok('/admin/users?role=' . $role->id);
$mech->content_contains('selected>Role A');
$mech->content_contains('test@example.com');
Expand Down