BigW Consortium Gitlab

pattern.rb 1.57 KB
Newer Older
1 2
module Gitlab
  module SQL
3 4
    module Pattern
      extend ActiveSupport::Concern
5

6
      MIN_CHARS_FOR_PARTIAL_MATCHING = 3
7
      REGEX_QUOTED_WORD = /(?<=\A| )"[^"]+"(?= |\z)/
8

9
      class_methods do
10 11 12 13 14 15
        def fuzzy_search(query, columns)
          matches = columns.map { |col| fuzzy_arel_match(col, query) }.compact.reduce(:or)

          where(matches)
        end

16
        def to_pattern(query)
Hiroyuki Sato committed
17
          if partial_matching?(query)
18
            "%#{sanitize_sql_like(query)}%"
Hiroyuki Sato committed
19 20
          else
            sanitize_sql_like(query)
21
          end
22 23
        end

24 25 26
        def partial_matching?(query)
          query.length >= MIN_CHARS_FOR_PARTIAL_MATCHING
        end
27

28
        def fuzzy_arel_match(column, query)
29 30
          query = query.squish
          return nil unless query.present?
31

32
          words = select_fuzzy_words(query)
33

34 35 36 37 38 39 40
          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
41 42 43 44 45 46 47
        end

        def select_fuzzy_words(query)
          quoted_words = query.scan(REGEX_QUOTED_WORD)

          query = quoted_words.reduce(query) { |q, quoted_word| q.sub(quoted_word, '') }

48
          words = query.split
49 50 51 52 53 54 55

          quoted_words.map! { |quoted_word| quoted_word[1..-2] }

          words.concat(quoted_words)

          words.select { |word| partial_matching?(word) }
        end
56
      end
57 58 59
    end
  end
end