debian-mirror-gitlab/lib/gitlab/sql/pattern.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.9 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module SQL
module Pattern
extend ActiveSupport::Concern
MIN_CHARS_FOR_PARTIAL_MATCHING = 3
2023-01-13 00:05:48 +05:30
REGEX_QUOTED_TERM = /(?<=\A| )"[^"]+"(?= |\z)/.freeze
2018-03-17 18:26:18 +05:30
class_methods do
2019-09-30 21:07:59 +05:30
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)
2018-03-17 18:26:18 +05:30
where(matches)
end
2019-09-30 21:07:59 +05:30
def to_pattern(query, use_minimum_char_limit: true)
if partial_matching?(query, use_minimum_char_limit: use_minimum_char_limit)
2018-03-17 18:26:18 +05:30
"%#{sanitize_sql_like(query)}%"
else
sanitize_sql_like(query)
end
end
2019-07-07 11:18:12 +05:30
def min_chars_for_partial_matching
MIN_CHARS_FOR_PARTIAL_MATCHING
end
2019-09-30 21:07:59 +05:30
def partial_matching?(query, use_minimum_char_limit: true)
return true unless use_minimum_char_limit
2019-07-07 11:18:12 +05:30
query.length >= min_chars_for_partial_matching
2018-03-17 18:26:18 +05:30
end
2020-01-01 13:55:28 +05:30
# column - The column name / Arel column to search in.
2018-03-17 18:26:18 +05:30
# 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-09-30 21:07:59 +05:30
def fuzzy_arel_match(column, query, lower_exact_match: false, use_minimum_char_limit: true)
2023-01-13 00:05:48 +05:30
return unless query.is_a?(String)
2018-03-17 18:26:18 +05:30
query = query.squish
2019-07-07 11:18:12 +05:30
return unless query.present?
2018-03-17 18:26:18 +05:30
2020-01-01 13:55:28 +05:30
arel_column = column.is_a?(Arel::Attributes::Attribute) ? column : arel_table[column]
2023-01-13 00:05:48 +05:30
words = select_fuzzy_terms(query, use_minimum_char_limit: use_minimum_char_limit)
2018-03-17 18:26:18 +05:30
if words.any?
2020-01-01 13:55:28 +05:30
words.map { |word| arel_column.matches(to_pattern(word, use_minimum_char_limit: use_minimum_char_limit)) }.reduce(:and)
2023-03-04 22:38:38 +05:30
elsif lower_exact_match
2018-03-17 18:26:18 +05:30
# No words of at least 3 chars, but we can search for an exact
# case insensitive match with the query as a whole
2023-03-04 22:38:38 +05:30
Arel::Nodes::NamedFunction
2020-01-01 13:55:28 +05:30
.new('LOWER', [arel_column])
2018-03-17 18:26:18 +05:30
.eq(query)
2023-03-04 22:38:38 +05:30
else
arel_column.matches(sanitize_sql_like(query))
2018-03-17 18:26:18 +05:30
end
end
2023-01-13 00:05:48 +05:30
def select_fuzzy_terms(query, use_minimum_char_limit: true)
terms = Gitlab::SQL::Pattern.split_query_to_search_terms(query)
terms.select { |term| partial_matching?(term, use_minimum_char_limit: use_minimum_char_limit) }
end
end
2018-03-17 18:26:18 +05:30
2023-01-13 00:05:48 +05:30
def self.split_query_to_search_terms(query)
quoted_terms = []
2018-03-17 18:26:18 +05:30
2023-01-13 00:05:48 +05:30
query = query.gsub(REGEX_QUOTED_TERM) do |quoted_term|
quoted_terms << quoted_term
""
2018-03-17 18:26:18 +05:30
end
2023-01-13 00:05:48 +05:30
query.split + quoted_terms.map { |quoted_term| quoted_term[1..-2] }
2018-03-17 18:26:18 +05:30
end
end
end
end
2023-03-17 16:20:25 +05:30
Gitlab::SQL::Pattern.prepend_mod
Gitlab::SQL::Pattern::ClassMethods.prepend_mod_with('Gitlab::SQL::Pattern::ClassMethods')