diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-11-24 12:23:47 +0100 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-11-27 11:29:40 +0100 |
commit | d4eea275310867eccc927d0e92a1d19a165f0668 (patch) | |
tree | 2909c3b95d69aadfd6dba5a3d9f2e18132960db8 /lib/gitlab/sql | |
parent | b2c5363da1bdfb4df8693de38f9d83fe203e6e99 (diff) | |
download | gitlab-ce-d4eea275310867eccc927d0e92a1d19a165f0668.tar.gz |
Modify fuzzy_arel_match to search for equality when a term shorter than 3 characters is provided
Diffstat (limited to 'lib/gitlab/sql')
-rw-r--r-- | lib/gitlab/sql/pattern.rb | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/gitlab/sql/pattern.rb b/lib/gitlab/sql/pattern.rb index 8741aa0f1c4..20ca36efb29 100644 --- a/lib/gitlab/sql/pattern.rb +++ b/lib/gitlab/sql/pattern.rb @@ -4,7 +4,7 @@ module Gitlab extend ActiveSupport::Concern MIN_CHARS_FOR_PARTIAL_MATCHING = 3 - REGEX_QUOTED_WORD = /(?<=^| )"[^"]+"(?= |$)/ + REGEX_QUOTED_WORD = /(?<=\A| )"[^"]+"(?= |\z)/ class_methods do def to_pattern(query) @@ -20,11 +20,18 @@ module Gitlab end def fuzzy_arel_match(column, query) - words = select_fuzzy_words(query) + query = query.squish + return nil unless query.present? - matches = words.map { |word| arel_table[column].matches(to_pattern(word)) } + words = select_fuzzy_words(query) - matches.reduce { |result, match| result.and(match) } + if words.any? + words.map { |word| arel_table[column].matches(to_pattern(word)) }.reduce(:and) + else + # No words of at least 3 chars, but we can search for an exact + # case insensitive match with the query as a whole + arel_table[column].matches(sanitize_sql_like(query)) + end end def select_fuzzy_words(query) @@ -32,7 +39,7 @@ module Gitlab query = quoted_words.reduce(query) { |q, quoted_word| q.sub(quoted_word, '') } - words = query.split(/\s+/) + words = query.split quoted_words.map! { |quoted_word| quoted_word[1..-2] } |