<commit_before>module FullTextSearch
  module PGroonga
    def self.prepended(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def search_result_ranks_and_ids(tokens, user=User.current, projects=nil, options={})
        @order_target = options[:params][:order_target] || "score"
        super
      end

      # Overwrite ActsAsSearchable
      def search_tokens_condition(columns, tokens, all_words)
        token_clauses = columns.map do |column|
          "#{column} @@ ?"
        end
        sql = token_clauses.join(' OR ')
        [sql, *([tokens.join(all_words ? " " : " OR ")] * columns.size)]
      end

      # Overwrite ActsAsSearchable
      def fetch_ranks_and_ids(scope, limit)
        target_column_name = "#{table_name}.#{order_column_name}"
        scope.select("pgroonga.score(#{self.table_name}) AS score, #{target_column_name} AS order_target", :id)
          .reorder("score DESC", id: :desc)
          .distinct
          .limit(limit)
          .map do |record|
          if @order_target == "score"
            [record.score, record.id]
          else
            [record.order_target.to_i, record.id]
          end
        end
      end

      #
      # searchable_options[:date_column] is not enough
      # Because almost all models does not use `acts_as_searchable :date_column` option,
      # and searchable_options[:data] default value is `:created_on`
      #
      def order_column_name
        timestamp_columns = ["created_on", "updated_on", "commited_on"]
        column_names.select {|column_name| timestamp_columns.include?(column_name) }.sort.last || "id"
      end
    end
  end
end
<commit_msg>Use `@@` operator instead of `ILIKE`<commit_after>module FullTextSearch
  module PGroonga
    def self.prepended(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def search_result_ranks_and_ids(tokens, user=User.current, projects=nil, options={})
        @order_target = options[:params][:order_target] || "score"
        super
      end

      # Overwrite ActsAsSearchable
      def search_tokens_condition(columns, tokens, all_words)
        token_clauses = columns.map do |column|
          "#{column} @@ ?"
        end
        sql = token_clauses.join(' OR ')
        [sql, *([tokens.join(all_words ? " " : " OR ")] * columns.size)]
      end

      def search_token_match_statement(column, value='?')
        "#{column} @@ #{value}"
      end

      # Overwrite ActsAsSearchable
      def fetch_ranks_and_ids(scope, limit)
        target_column_name = "#{table_name}.#{order_column_name}"
        scope.select("pgroonga.score(#{self.table_name}) AS score, #{target_column_name} AS order_target", :id)
          .reorder("score DESC", id: :desc)
          .distinct
          .limit(limit)
          .map do |record|
          if @order_target == "score"
            [record.score, record.id]
          else
            [record.order_target.to_i, record.id]
          end
        end
      end

      #
      # searchable_options[:date_column] is not enough
      # Because almost all models does not use `acts_as_searchable :date_column` option,
      # and searchable_options[:data] default value is `:created_on`
      #
      def order_column_name
        timestamp_columns = ["created_on", "updated_on", "commited_on"]
        column_names.select {|column_name| timestamp_columns.include?(column_name) }.sort.last || "id"
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11159420289855072, 'fraction_numerical': 0.0, 'mean_word_length': 2.5431211498973307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188659', 'n_tokens_mistral': 1070, 'n_tokens_neox': 1056, 'n_words': 228}