Feature #35764 ยป 0001-tokenize-query-strings-for-Issue.like-and-Query-sql_.patch
| app/models/issue.rb | ||
|---|---|---|
|
ids.any? ? where(:assigned_to_id => ids) : none
|
||
|
end)
|
||
|
scope :like, (lambda do |q|
|
||
|
q = q.to_s
|
||
|
if q.present?
|
||
|
where("LOWER(#{table_name}.subject) LIKE LOWER(?)", "%#{sanitize_sql_like q}%")
|
||
|
where(*::Query.tokenized_like_conditions("#{table_name}.subject", q))
|
||
|
end
|
||
|
end)
|
||
| app/models/query.rb | ||
|---|---|---|
|
prefix = suffix = nil
|
||
|
prefix = '%' if options[:ends_with]
|
||
|
suffix = '%' if options[:starts_with]
|
||
|
prefix = suffix = '%' if prefix.nil? && suffix.nil?
|
||
|
value = queried_class.sanitize_sql_like value
|
||
|
queried_class.sanitize_sql_for_conditions([
|
||
|
Redmine::Database.like(db_field, '?', :match => options[:match]),
|
||
|
"#{prefix}#{value}#{suffix}"
|
||
|
])
|
||
|
if prefix || suffix
|
||
|
value = queried_class.sanitize_sql_like value
|
||
|
queried_class.sanitize_sql_for_conditions([
|
||
|
Redmine::Database.like(db_field, '?', :match => options[:match]),
|
||
|
"#{prefix}#{value}#{suffix}"
|
||
|
])
|
||
|
else
|
||
|
queried_class.sanitize_sql_for_conditions(
|
||
|
::Query.tokenized_like_conditions(db_field, value, **options)
|
||
|
)
|
||
|
end
|
||
|
end
|
||
|
def self.tokenized_like_conditions(db_field, value, **options)
|
||
|
tokens = Redmine::Search::Tokenizer.new(value).tokens
|
||
|
tokens = [value] unless tokens.present?
|
||
|
sql, values = tokens.map do |token|
|
||
|
[Redmine::Database.like(db_field, '?', options), "%#{sanitize_sql_like token}%"]
|
||
|
end.transpose
|
||
|
[sql.join(" AND "), *values]
|
||
|
end
|
||
|
# Adds a filter for the given custom field
|
||
| lib/redmine/search.rb | ||
|---|---|---|
|
@projects = projects
|
||
|
@cache = options.delete(:cache)
|
||
|
@options = options
|
||
|
# extract tokens from the question
|
||
|
# eg. hello "bye bye" => ["hello", "bye bye"]
|
||
|
@tokens = @question.scan(%r{((\s|^)"[^"]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
|
||
|
# tokens must be at least 2 characters long
|
||
|
# but for Chinese characters (Chinese HANZI/Japanese KANJI), tokens can be one character
|
||
|
@tokens = @tokens.uniq.select {|w| w.length > 1 || w =~ /\p{Han}/}
|
||
|
# no more than 5 tokens to search for
|
||
|
@tokens.slice! 5..-1
|
||
|
@tokens = Tokenizer.new(@question).tokens
|
||
|
end
|
||
|
# Returns the total result count
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
class Tokenizer
|
||
|
def initialize(question)
|
||
|
@question = question.to_s
|
||
|
end
|
||
|
def tokens
|
||
|
# extract tokens from the question
|
||
|
# eg. hello "bye bye" => ["hello", "bye bye"]
|
||
|
tokens = @question.scan(%r{((\s|^)"[^"]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
|
||
|
# tokens must be at least 2 characters long
|
||
|
# but for Chinese characters (Chinese HANZI/Japanese KANJI), tokens can be one character
|
||
|
# no more than 5 tokens to search for
|
||
|
tokens.uniq.select{|w| w.length > 1 || w =~ /\p{Han}/}.first 5
|
||
|
end
|
||
|
end
|
||
|
module Controller
|
||
|
def self.included(base)
|
||
|
base.extend(ClassMethods)
|
||