<commit_before>class RubyGem < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks # auto imports to active record to keep them synchronized

  index_name ["ruby_gem", Rails.env].join('_') # create separate indexes for each environment

  def self.update_score
    # TODO: Clean data somewhere before this
    avg_downloads = self.average(:downloads)
    avg_stars = self.average(:stars)
    binding.pry
    star_multiplier = (avg_downloads / avg_stars) + 200

    self.all.each do |gem|
      score = gem.downloads + gem.stars * star_multiplier
      gem.update(score: score)
    end
  end

  def self.search(query, options={})
    search_definition = {
      query: {
        multi_match: {
          fuzziness: 2,
          query: query,
          fields: ["name^100", "description^50"],
          operator: "and"
        }
      },
      highlight: {
        fields: {
          description: {},
          name: {}
        }
      }
    }

    __elasticsearch__.search(search_definition)<commit_msg>cleaned debugging statements left in<commit_after>class RubyGem < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks # auto imports to active record to keep them synchronized

  index_name ["ruby_gem", Rails.env].join('_') # create separate indexes for each environment

  def self.update_score
    avg_downloads = self.average(:downloads)
    avg_stars = self.average(:stars)

    star_multiplier = (avg_downloads / avg_stars) + 200

    self.all.each do |gem|
      score = gem.downloads + gem.stars * star_multiplier
      gem.update(score: score)
    end
  end

  def self.search(query, options={})
    search_definition = {
      query: {
        multi_match: {
          fuzziness: 2,
          query: query,
          fields: ["name^100", "description^50"],
          operator: "and"
        }
      },
      highlight: {
        fields: {
          description: {},
          name: {}
        }
      }
    }

    __elasticsearch__.search(search_definition)
{'fraction_non_alphanumeric': 0.10395314787701318, 'fraction_numerical': 0.008784773060029283, 'mean_word_length': 2.5283993115318415, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2448393', 'n_tokens_mistral': 628, 'n_tokens_neox': 588, 'n_words': 142}