diff options
author | Marius Halden <marius.h@lden.org> | 2021-10-07 13:32:40 +0200 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2021-10-07 13:32:40 +0200 |
commit | 09dacfc6b8bf62addeee16c20b1d90c2a256da96 (patch) | |
tree | 7caa2bf9e92227ab74448f9b746dd28bbcb81b2a /perllib/FixMyStreet/Roles/FullTextSearch.pm | |
parent | 585e57484f9c6332668bf1ac0a6a3b39dbe32223 (diff) | |
parent | cea89fb87a96943708a1db0f646492fbfaaf000f (diff) |
Merge tag 'v3.1' into fiksgatami-devfiksgatami-dev
Diffstat (limited to 'perllib/FixMyStreet/Roles/FullTextSearch.pm')
-rw-r--r-- | perllib/FixMyStreet/Roles/FullTextSearch.pm | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Roles/FullTextSearch.pm b/perllib/FixMyStreet/Roles/FullTextSearch.pm new file mode 100644 index 000000000..871b1d185 --- /dev/null +++ b/perllib/FixMyStreet/Roles/FullTextSearch.pm @@ -0,0 +1,29 @@ +package FixMyStreet::Roles::FullTextSearch; + +use Moo::Role; +use FixMyStreet; + +requires 'text_search_columns'; +requires 'text_search_nulls'; +requires 'text_search_translate'; + +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 = 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', $bind)", $query ]); +} + +1; + |