From 506fc9d74cdb67af2eaea27662420ec07e32b2f3 Mon Sep 17 00:00:00 2001 From: Marius Balteanu Date: Tue, 5 Oct 2021 19:54:31 +0000 Subject: Tokenize search parameter in order to allow multiple search terms in: * the "contains" operator of text filters * in issue autocomplete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Jens Krämer. git-svn-id: http://svn.redmine.org/redmine/trunk@21238 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- lib/redmine/search.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'lib/redmine') diff --git a/lib/redmine/search.rb b/lib/redmine/search.rb index 3aeedbcd2..7eae79b5e 100644 --- a/lib/redmine/search.rb +++ b/lib/redmine/search.rb @@ -57,15 +57,7 @@ module Redmine @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 @@ -135,6 +127,22 @@ module Redmine 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) -- cgit v1.2.3