diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-07-29 09:03:56 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-07-31 12:33:38 +0100 |
commit | bd42d6a1fcb6c8e6d89413e0ee22617625d95bad (patch) | |
tree | 2bdbeff779e44d14aa2409f9660ab3c91c9a8377 /perllib/FixMyStreet/Roles/FullTextSearch.pm | |
parent | 34dd7c253d337ea922049390dc6ba44b8686e516 (diff) |
Add database index for full text search.
Diffstat (limited to 'perllib/FixMyStreet/Roles/FullTextSearch.pm')
-rw-r--r-- | perllib/FixMyStreet/Roles/FullTextSearch.pm | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Roles/FullTextSearch.pm b/perllib/FixMyStreet/Roles/FullTextSearch.pm new file mode 100644 index 000000000..78dbd5c18 --- /dev/null +++ b/perllib/FixMyStreet/Roles/FullTextSearch.pm @@ -0,0 +1,22 @@ +package FixMyStreet::Roles::FullTextSearch; + +use Moo::Role; +use FixMyStreet; + +requires 'text_search_columns'; +requires 'text_search_nulls'; + +sub search_text { + my ($rs, $query) = @_; + my %nulls = map { $_ => 1 } $rs->text_search_nulls; + my @cols = map { + my $col = $rs->me($_); + $nulls{$_} ? "coalesce($col, '')" : $col; + } $rs->text_search_columns; + my $vector = "translate(" . join(" || ' ' || ", @cols) . ", '/.', ' ')"; + my $config = FixMyStreet->config('DB_FULL_TEXT_SEARCH_CONFIG') || 'english'; + $rs->search(\[ "to_tsvector('$config', $vector) @@ plainto_tsquery('$config', ?)", $query ]); +} + +1; + |