2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-22 12:51:53 -04:00
|
|
|
module Gitlab
|
|
|
|
module SQL
|
2017-08-28 18:14:41 -04:00
|
|
|
module Pattern
|
|
|
|
extend ActiveSupport::Concern
|
2017-08-22 12:51:53 -04:00
|
|
|
|
2017-08-28 18:14:41 -04:00
|
|
|
MIN_CHARS_FOR_PARTIAL_MATCHING = 3
|
2019-05-05 06:19:14 -04:00
|
|
|
REGEX_QUOTED_WORD = /(?<=\A| )"[^"]+"(?= |\z)/.freeze
|
2017-08-22 12:51:53 -04:00
|
|
|
|
2017-08-28 18:14:41 -04:00
|
|
|
class_methods do
|
2019-06-12 20:08:44 -04:00
|
|
|
def fuzzy_search(query, columns, use_minimum_char_limit: true)
|
|
|
|
matches = columns.map do |col|
|
|
|
|
fuzzy_arel_match(col, query, use_minimum_char_limit: use_minimum_char_limit)
|
|
|
|
end.compact.reduce(:or)
|
2017-11-24 06:24:24 -05:00
|
|
|
|
|
|
|
where(matches)
|
|
|
|
end
|
|
|
|
|
2019-06-12 20:08:44 -04:00
|
|
|
def to_pattern(query, use_minimum_char_limit: true)
|
|
|
|
if partial_matching?(query, use_minimum_char_limit: use_minimum_char_limit)
|
2017-08-28 18:14:41 -04:00
|
|
|
"%#{sanitize_sql_like(query)}%"
|
2017-08-29 05:00:03 -04:00
|
|
|
else
|
|
|
|
sanitize_sql_like(query)
|
2017-08-28 18:14:41 -04:00
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
|
2019-03-12 04:13:21 -04:00
|
|
|
def min_chars_for_partial_matching
|
|
|
|
MIN_CHARS_FOR_PARTIAL_MATCHING
|
|
|
|
end
|
|
|
|
|
2019-06-12 20:08:44 -04:00
|
|
|
def partial_matching?(query, use_minimum_char_limit: true)
|
|
|
|
return true unless use_minimum_char_limit
|
|
|
|
|
2019-03-12 04:13:21 -04:00
|
|
|
query.length >= min_chars_for_partial_matching
|
2017-08-28 18:14:41 -04:00
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
|
2019-12-02 16:06:51 -05:00
|
|
|
# column - The column name / Arel column to search in.
|
2018-02-15 13:34:44 -05:00
|
|
|
# query - The text to search for.
|
|
|
|
# lower_exact_match - When set to `true` we'll fall back to using
|
|
|
|
# `LOWER(column) = query` instead of using `ILIKE`.
|
2019-06-12 20:08:44 -04:00
|
|
|
def fuzzy_arel_match(column, query, lower_exact_match: false, use_minimum_char_limit: true)
|
2017-11-24 06:23:47 -05:00
|
|
|
query = query.squish
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless query.present?
|
2017-08-23 06:54:14 -04:00
|
|
|
|
2019-12-02 16:06:51 -05:00
|
|
|
arel_column = column.is_a?(Arel::Attributes::Attribute) ? column : arel_table[column]
|
|
|
|
|
2019-06-12 20:08:44 -04:00
|
|
|
words = select_fuzzy_words(query, use_minimum_char_limit: use_minimum_char_limit)
|
2017-08-23 06:54:14 -04:00
|
|
|
|
2017-11-24 06:23:47 -05:00
|
|
|
if words.any?
|
2019-12-02 16:06:51 -05:00
|
|
|
words.map { |word| arel_column.matches(to_pattern(word, use_minimum_char_limit: use_minimum_char_limit)) }.reduce(:and)
|
2017-11-24 06:23:47 -05:00
|
|
|
else
|
|
|
|
# No words of at least 3 chars, but we can search for an exact
|
|
|
|
# case insensitive match with the query as a whole
|
2018-02-15 13:34:44 -05:00
|
|
|
if lower_exact_match
|
|
|
|
Arel::Nodes::NamedFunction
|
2019-12-02 16:06:51 -05:00
|
|
|
.new('LOWER', [arel_column])
|
2018-02-15 13:55:43 -05:00
|
|
|
.eq(query)
|
2018-02-15 13:34:44 -05:00
|
|
|
else
|
2019-12-02 16:06:51 -05:00
|
|
|
arel_column.matches(sanitize_sql_like(query))
|
2018-02-15 13:34:44 -05:00
|
|
|
end
|
2017-11-24 06:23:47 -05:00
|
|
|
end
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
|
|
|
|
2019-06-12 20:08:44 -04:00
|
|
|
def select_fuzzy_words(query, use_minimum_char_limit: true)
|
2017-08-23 06:54:14 -04:00
|
|
|
quoted_words = query.scan(REGEX_QUOTED_WORD)
|
|
|
|
|
|
|
|
query = quoted_words.reduce(query) { |q, quoted_word| q.sub(quoted_word, '') }
|
|
|
|
|
2017-11-24 06:23:47 -05:00
|
|
|
words = query.split
|
2017-08-23 06:54:14 -04:00
|
|
|
|
|
|
|
quoted_words.map! { |quoted_word| quoted_word[1..-2] }
|
|
|
|
|
|
|
|
words.concat(quoted_words)
|
|
|
|
|
2019-06-12 20:08:44 -04:00
|
|
|
words.select { |word| partial_matching?(word, use_minimum_char_limit: use_minimum_char_limit) }
|
2017-08-23 06:54:14 -04:00
|
|
|
end
|
2017-08-26 09:32:55 -04:00
|
|
|
end
|
2017-08-22 12:51:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|