<commit_before>module Tire
  module Model

    module Search

      def self.included(base)
        base.class_eval do
          extend  Tire::Model::Naming::ClassMethods
          include Tire::Model::Naming::InstanceMethods

          extend  Tire::Model::Indexing::ClassMethods
          extend  Tire::Model::Import::ClassMethods

          extend  Tire::Model::Percolate::ClassMethods
          include Tire::Model::Percolate::InstanceMethods

          extend  ClassMethods
          include InstanceMethods

          ['_score', '_type', '_index', '_version', 'sort', 'highlight', 'matches'].each do |attr|
            # TODO: Find a sane way to add attributes like _score for ActiveRecord -
            #       `define_attribute_methods [attr]` does not work in AR.
            define_method("#{attr}=") { |value| @attributes ||= {}; @attributes[attr] = value }
            define_method("#{attr}")  { @attributes[attr] }
          end

          def to_hash
            self.serializable_hash
          end unless instance_methods.map(&:to_sym).include?(:to_hash)
        end
      end

      module ClassMethods

        def search(query=nil, options={}, &block)
          old_wrapper = Tire::Configuration.wrapper
          Tire::Configuration.wrapper self
          sort  = options[:order] || options[:sort]
          sort  = Array(sort)
          unless block_given?
            s = Tire::Search::Search.new(elasticsearch_index.name, options)
            s.query { string query }
            s.sort do
              sort.each do |t|
                field_name, direction = t.split(' ')
                field_name.include?('.') ? field(field_name, direction) : send(field_name, direction)
              end
            end unless sort.empty?
            s.size( options[:per_page].to_i ) if options[:per_page]
            s.from( options[:page].to_i <= 1 ? 0 : (options[:per_page].to_i * (options[:page].to_i-1)) ) if options[:page] && options[:per_page]
            s.perform.results
          else
            s = Tire::Search::Search.new(elasticsearch_index.name, options)
            block.arity < 1 ? s.instance_eval(&block) : block.call(s)
            s.perform.results
          end
        ensure
          Tire::Configuration.wrapper old_wrapper
        end

        # Wrapper for the ES index for this class
        #
        # TODO: Implement some "forwardable" object named +tire+ for Tire mixins,
        #       and proxy everything via this object. If we're not stepping on
        #       other libs toes, extend/include also to the top level.
        #
        #       The original culprit is Mongoid here, see https://github.com/karmi/tire/issues/7
        #
        def elasticsearch_index
          @index = Index.new(index_name)
        end

      end

      module InstanceMethods

        def score
          Tire.warn "#{self.class}#score has been deprecated, please use #{self.class}#_score instead."
          attributes['_score']
        end

        def index
          self.class.elasticsearch_index
        end

        def update_elastic_search_index
          _run_update_elastic_search_index_callbacks do
            if destroyed?
              index.remove self
            else
              response  = index.store( self, {:percolate => self.percolator} )
              self.id ||= response['_id'] if self.respond_to?(:id=)
              self._index   = response['_index']
              self._type    = response['_type']
              self._version = response['_version']
              self.matches  = response['matches']
              self
            end
          end
        end

        def to_indexed_json
          if self.class.mapping.empty?
            to_hash.to_json
          else
            to_hash.
            reject { |key, value| ! self.class.mapping.keys.map(&:to_s).include?(key.to_s) }.
            to_json
          end
        end

      end

      extend ClassMethods
    end

  end
end
<commit_msg>[REFACTORING] Add alias Model#update_elasticsearch_index for Model#update_elastic_search_index<commit_after>module Tire
  module Model

    module Search

      def self.included(base)
        base.class_eval do
          extend  Tire::Model::Naming::ClassMethods
          include Tire::Model::Naming::InstanceMethods

          extend  Tire::Model::Indexing::ClassMethods
          extend  Tire::Model::Import::ClassMethods

          extend  Tire::Model::Percolate::ClassMethods
          include Tire::Model::Percolate::InstanceMethods

          extend  ClassMethods
          include InstanceMethods

          ['_score', '_type', '_index', '_version', 'sort', 'highlight', 'matches'].each do |attr|
            # TODO: Find a sane way to add attributes like _score for ActiveRecord -
            #       `define_attribute_methods [attr]` does not work in AR.
            define_method("#{attr}=") { |value| @attributes ||= {}; @attributes[attr] = value }
            define_method("#{attr}")  { @attributes[attr] }
          end

          def to_hash
            self.serializable_hash
          end unless instance_methods.map(&:to_sym).include?(:to_hash)
        end
      end

      module ClassMethods

        def search(query=nil, options={}, &block)
          old_wrapper = Tire::Configuration.wrapper
          Tire::Configuration.wrapper self
          sort  = options[:order] || options[:sort]
          sort  = Array(sort)
          unless block_given?
            s = Tire::Search::Search.new(elasticsearch_index.name, options)
            s.query { string query }
            s.sort do
              sort.each do |t|
                field_name, direction = t.split(' ')
                field_name.include?('.') ? field(field_name, direction) : send(field_name, direction)
              end
            end unless sort.empty?
            s.size( options[:per_page].to_i ) if options[:per_page]
            s.from( options[:page].to_i <= 1 ? 0 : (options[:per_page].to_i * (options[:page].to_i-1)) ) if options[:page] && options[:per_page]
            s.perform.results
          else
            s = Tire::Search::Search.new(elasticsearch_index.name, options)
            block.arity < 1 ? s.instance_eval(&block) : block.call(s)
            s.perform.results
          end
        ensure
          Tire::Configuration.wrapper old_wrapper
        end

        # Wrapper for the ES index for this class
        #
        # TODO: Implement some "forwardable" object named +tire+ for Tire mixins,
        #       and proxy everything via this object. If we're not stepping on
        #       other libs toes, extend/include also to the top level.
        #
        #       The original culprit is Mongoid here, see https://github.com/karmi/tire/issues/7
        #
        def elasticsearch_index
          @index = Index.new(index_name)
        end

      end

      module InstanceMethods

        def score
          Tire.warn "#{self.class}#score has been deprecated, please use #{self.class}#_score instead."
          attributes['_score']
        end

        def index
          self.class.elasticsearch_index
        end

        def update_elastic_search_index
          _run_update_elastic_search_index_callbacks do
            if destroyed?
              index.remove self
            else
              response  = index.store( self, {:percolate => self.percolator} )
              self.id ||= response['_id'] if self.respond_to?(:id=)
              self._index   = response['_index']
              self._type    = response['_type']
              self._version = response['_version']
              self.matches  = response['matches']
              self
            end
          end
        end
        alias :update_elasticsearch_index :update_elastic_search_index

        def to_indexed_json
          if self.class.mapping.empty?
            to_hash.to_json
          else
            to_hash.
            reject { |key, value| ! self.class.mapping.keys.map(&:to_s).include?(key.to_s) }.
            to_json
          end
        end

      end

      extend ClassMethods
    end

  end
end

{'fraction_non_alphanumeric': 0.09834445268099827, 'fraction_numerical': 0.001235483073881888, 'mean_word_length': 1.8594136347580361, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1647861', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2174, 'n_words': 526}