From 6abae51e47753cc80b440c4234c14cd8232e235f Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Sat, 16 Nov 2019 07:17:15 -0600 Subject: Regular expressions can now be used in full-text searches. Wrapping a search term in double-quotes treats it as a whole-word match. --- app/helpers/search_helper.rb | 6 ++++++ app/lib/bangtags.rb | 4 +++- app/models/status.rb | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 app/helpers/search_helper.rb (limited to 'app') diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb new file mode 100644 index 000000000..96da161f1 --- /dev/null +++ b/app/helpers/search_helper.rb @@ -0,0 +1,6 @@ +module SearchHelper + + def expand_search_query(query) + query.gsub(/"(.*)"/, '\\y\1\\y') + end +end diff --git a/app/lib/bangtags.rb b/app/lib/bangtags.rb index 681fa4abc..60fb426b3 100644 --- a/app/lib/bangtags.rb +++ b/app/lib/bangtags.rb @@ -2,6 +2,8 @@ class Bangtags include ModerationHelper + include SearchHelper + attr_reader :status, :account def initialize(status) @@ -718,7 +720,7 @@ class Bangtags q = cmd[1..-1].join.strip next if q.blank? begin - data = @account.statuses.where('text ILIKE ?', "%#{sanitize_sql_like(q)}%") + data = @account.statuses.where('text ~* ?', expand_search_query(q)) .reorder(:created_at) .pluck(:created_at) .map { |d| d.strftime('%Y-%m') } diff --git a/app/models/status.rb b/app/models/status.rb index 47d5f982e..045a4a31a 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -336,6 +336,8 @@ class Status < ApplicationRecord after_create :process_bangtags, if: :local? class << self + include SearchHelper + def search_for(term, account = nil, limit = 33, offset = 0) return none if account.nil? if term.start_with?('me:') @@ -349,7 +351,7 @@ class Status < ApplicationRecord end return none if term.blank? || term.length < 3 query = query.without_reblogs - .where('text ILIKE ?', "%#{sanitize_sql_like(term)}%") + .where('text ~* ?', expand_search_query(term)) .offset(offset).limit(limit) apply_timeline_filters(query, account, true) rescue ActiveRecord::StatementInvalid -- cgit