Skip to content

Commit

Permalink
二語以上のキーワードでも検索できるようにした
Browse files Browse the repository at this point in the history
「マルマル シャインカ」のようなスペースを含む検索ワードがそのまま1つの文字列として扱われ、部分一致検索ができるようにした
  • Loading branch information
nakamu-kazu222 committed Feb 24, 2025
1 parent e79bc85 commit 0c193c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
19 changes: 6 additions & 13 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,24 @@ def search_by_keywords(searched_values = {})
end

def columns_for_keyword_search(*column_names)
define_singleton_method(:_join_column_names) { "#{column_names.join('_or_')}_cont_any" }
define_singleton_method(:_join_column_names) { "#{(column_names + %i[kana_name]).join('_or_')}_cont_any" }
end

private

def params_for_keyword_search(searched_values = {})
return {} if searched_values[:word].blank?

groupings = searched_values[:word].split(/[[:blank:]]+/).map do |word|
if word.start_with?('user:')
create_parameter_for_search_user_id(word.delete_prefix('user:'))
else
{ _join_column_names => word }
end
end
word = searched_values[:word].strip

{ combinator: 'or', groupings: }
{ combinator: 'or', groupings: [word_to_groupings(word)] }
end

def word_to_groupings(word)
return { _join_column_names => word } if COLUMN_NAMES_FOR_SEARCH_USER_ID.none? { |column_name| has_attribute?(column_name) }

case word
when /user:(.*)/
create_parameter_for_search_user_id(Regexp.last_match(1))
if word.match?(/^user:/)
create_parameter_for_search_user_id(word.delete_prefix("user:"))
else
{ _join_column_names => word }
end
Expand All @@ -51,7 +44,7 @@ def split_keyword_by_blank(word)

def create_parameter_for_search_user_id(username)
user = User.find_by(login_name: username)
{ "#{COLUMN_NAMES_FOR_SEARCH_USER_ID.join('_or_')}_eq" => user&.id || 0 }
{ "#{%i[user_id last_updated_user_id].join('_or_')}_eq" => user&.id || 0 }
end
end

Expand Down
6 changes: 5 additions & 1 deletion app/models/search_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def initialize(word:, users: nil, target: nil, require_retire_user: false)

def search
validated_search_word = validate_search_word
searched_user = @users ? @users.merge(User.search_by_keywords(word: validated_search_word)) : User.search_by_keywords(word: validated_search_word)
return User.none if validated_search_word.blank?

searched_user = @users ? @users.merge(User.search_by_keywords(word: validated_search_word, exact_match: true)) : User.search_by_keywords(word: validated_search_word, exact_match: true)

if @target == 'retired'
searched_user.unscope(where: :retired_on).retired
Expand All @@ -23,6 +25,8 @@ def search

def validate_search_word
stripped_word = @word.strip
return nil if stripped_word.blank?

if stripped_word.match?(/^[\w-]+$/)
stripped_word if stripped_word.length >= 3
elsif stripped_word.length >= 2
Expand Down

0 comments on commit 0c193c1

Please sign in to comment.