clear-code / redmine_full_text_search

Full text search for Redmine
MIT License
61 stars 24 forks source link

Support disabling Mroonga FTS dinamically #24

Closed kou closed 7 years ago

kou commented 7 years ago

普通の人は必要のない機能なのでアレなんですが、このプラグインの開発をする人とこのプラグインの紹介をする人にとってうれしい機能を提案します。(私はredmine.tokyo第12回勉強会で紹介するのであるとうれしい。)

Redmineでプラグインを無効にしなくても、リクエスト毎にMroongaでの全文検索機能を無効にして従来の検索機能を使う機能を提案します。たとえば、リクエストにenable_mroonga=falseと指定すると従来の検索機能を使う機能です。

この機能があると次のことがしやすくなり、このプラグインの開発をする人と紹介する人にとってうれしいです。

実装イメージです。( #23 がマージされた前提。)

--- a/lib/full_text_search/mroonga.rb
+++ b/lib/full_text_search/mroonga.rb
@@ -36,7 +36,7 @@ module FullTextSearch

     module ClassMethods
       # Overwrite ActsAsSearchable
-      def fetch_ranks_and_ids(scope, limit, attachments: false, order_target: "score", order_type: "desc")
+      def fetch_ranks_and_ids_mroonga(scope, limit, attachments: false, order_target: "score", order_type: "desc")
         if self == ::WikiPage && !attachments
           scope.
             joins("INNER JOIN fts_wiki_contents ON (wiki_contents.id = fts_wiki_contents.wiki_content_id)").
@@ -62,6 +62,7 @@ module FullTextSearch
         tokens = [] << tokens unless tokens.is_a?(Array)
         projects = [] << projects if projects.is_a?(::Project)
         params = options[:params]
+        return super if params[:enable_mroonga] == "false"
         target_column_name = "#{table_name}.#{order_column_name}"
         kw = {
           order_type: params[:order_type] || "desc",
@@ -85,7 +86,7 @@ module FullTextSearch
             c1 = search_tokens_condition_single(column1, tokens, options[:all_words])
             c2 = search_tokens_condition_single(column2, tokens, options[:all_words])
             c, t = c1.zip(c2).to_a
-            r = fetch_ranks_and_ids(
+            r = fetch_ranks_and_ids_mroonga(
               search_scope(user, projects, options).
               select(:id, "#{s1} * 100 + #{s2} AS score, #{target_column_name} AS order_target").
               joins(fts_relation).
@@ -116,7 +117,7 @@ module FullTextSearch
               c, t = conditions.first.zip(*conditions[1..-1]).to_a
               p [2, c, t]
             end
-            r = fetch_ranks_and_ids(
+            r = fetch_ranks_and_ids_mroonga(
               search_scope(user, projects, options).
               select(:id, "#{scores.join(" + ")} AS score, #{target_column_name} AS order_target").
               joins(fts_relation).
@@ -142,7 +143,7 @@ module FullTextSearch
               condition = search_tokens_condition_single("#{::CustomValue.table_name}.value", tokens, options[:all_words])
               score = ActiveRecord::Base.send(:sanitize_sql_array, condition)

-              r |= fetch_ranks_and_ids(
+              r |= fetch_ranks_and_ids_mroonga(
                 search_scope(user, projects, options).
                 select(:id, "#{score} AS score, #{target_column_name} AS order_target").
                 joins(fts_relation).
@@ -177,7 +178,7 @@ module FullTextSearch
             conditions << note_condition
             scores << note_score
             c, t = conditions.first.zip(*conditions[1..-1]).to_a
-            r |= fetch_ranks_and_ids(
+            r |= fetch_ranks_and_ids_mroonga(
               search_scope(user, projects, options).
               select(:id, "SUM(#{scores.join(" + ")}) AS score, #{target_column_name} AS order_target").
               joins(fts_relation).
@@ -201,7 +202,7 @@ module FullTextSearch
             ActiveRecord::Base.send(:sanitize_sql_array, _condition)
           end
           c, t = conditions.first.zip(*conditions[1..-1]).to_a
-          r |= fetch_ranks_and_ids(
+          r |= fetch_ranks_and_ids_mroonga(
             search_scope(user, projects, options).
             select(:id, "#{scores.join(" + ")} AS score, #{target_column_name} AS order_target").
             joins(fts_relation).