<commit_before>module Searchkick
  module Reindex

    # https://gist.github.com/jarosan/3124884
    def reindex
      alias_name = tire.index.name
      new_index = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S")
      index = Tire::Index.new(new_index)

      index.create searchkick_index_options

      # use scope for import
      scope = respond_to?(:search_import) ? search_import : self
      scope.find_in_batches do |batch|
        index.import batch
      end

      if a = Tire::Alias.find(alias_name)
        old_indices = a.indices.dup
        old_indices.each do |index|
          a.indices.delete index
        end

        a.indices.add new_index
        response = a.save

        if response.success?
          old_indices.each do |index|
            i = Tire::Index.new(index)
            i.delete
          end
        end
      else
        i = Tire::Index.new(alias_name)
        i.delete
        response = Tire::Alias.create(name: alias_name, indices: [new_index])
      end

      response.success? || (raise response.to_s)
    end

    private

    def searchkick_index_options
      options = @searchkick_options

      settings = {
        analysis: {
          analyzer: {
            searchkick_keyword: {
              type: "custom",
              tokenizer: "keyword",
              filter: ["lowercase", "snowball"]
            },
            default_index: {
              type: "custom",
              tokenizer: "standard",
              # synonym should come last, after stemming and shingle
              # shingle must come before snowball
              filter: ["standard", "lowercase", "asciifolding", "stop", "snowball", "searchkick_index_shingle"]
            },
            searchkick_search: {<commit_msg>Cleaner code<commit_after>module Searchkick
  module Reindex

    # https://gist.github.com/jarosan/3124884
    def reindex
      alias_name = tire.index.name
      new_index = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S")
      index = Tire::Index.new(new_index)

      index.create searchkick_index_options

      # use scope for import
      scope = respond_to?(:search_import) ? search_import : self
      scope.find_in_batches do |batch|
        index.import batch
      end

      if a = Tire::Alias.find(alias_name)
        old_indices = a.indices.dup
        old_indices.each do |index|
          a.indices.delete index
        end

        a.indices.add new_index
        response = a.save

        if response.success?
          old_indices.each do |index|
            i = Tire::Index.new(index)
            i.delete
          end
        end
      else
        tire.index.delete
        response = Tire::Alias.create(name: alias_name, indices: [new_index])
      end

      response.success? || (raise response.to_s)
    end

    private

    def searchkick_index_options
      options = @searchkick_options

      settings = {
        analysis: {
          analyzer: {
            searchkick_keyword: {
              type: "custom",
              tokenizer: "keyword",
              filter: ["lowercase", "snowball"]
            },
            default_index: {
              type: "custom",
              tokenizer: "standard",
              # synonym should come last, after stemming and shingle
              # shingle must come before snowball
              filter: ["standard", "lowercase", "asciifolding", "stop", "snowball", "searchkick_index_shingle"]
            },
            searchkick_search: {
{'fraction_non_alphanumeric': 0.09620907524411258, 'fraction_numerical': 0.00402067777139575, 'mean_word_length': 1.8294069861900895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3363205', 'n_tokens_mistral': 1036, 'n_tokens_neox': 992, 'n_words': 221}