<commit_before>Spree::Product.class_eval do
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  index_name "#{ENV['RAILS_ENV'] || "development"}_#{Spree::Config.site_name.downcase.gsub " ","_"}"
  document_type 'product'

  add_simple_scopes [:descend_by_created_at]
  add_search_scope :has_images do
    joins(master: :images)
  end

  def self.es_search(query)
    response = self.__elasticsearch__.search query
    response
  end
  def as_indexed_json(options={})
    if !self.images.empty?
      image_url = self.images.first.attachment.url
    end
    taxons = self.taxons.map { |t| t.name }
    product = {
      name: self.name,
      taxons: taxons,
      price: self.price,
      image_url: image_url
    }
    product.to_json
  end

  after_commit on: [:create] do
    index_document
  end

  after_commit on: [:update] do
    update_document
  end

  after_commit on: [:destroy] do
    delete_document
  end

  #this can be modified to call a background worker
  def index_document
    __elasticsearch__.index_document
  end

  def delete_document
    __elasticsearch__.delete_document
  end

  #alias this method so that an update completely overwrites the document and
  #doesn't update only the updated fields
  alias_method :update_document,:index_document

end
<commit_msg>Added search scopes for brand and merchant<commit_after>Spree::Product.class_eval do
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  index_name "#{ENV['RAILS_ENV'] || "development"}_#{Spree::Config.site_name.downcase.gsub " ","_"}"
  document_type 'product'

  add_simple_scopes [:descend_by_created_at]
  add_search_scope :has_images do
    joins(master: :images)
  end

  add_search_scope :brand do |brand|
    where("data -> 'brand' = ?", brand)
  end
  add_search_scope :merchant do |merchant|
    where("data -> 'merchant' = ?", merchant)
  end

  def self.es_search(query)
    response = self.__elasticsearch__.search query
    response
  end
  def as_indexed_json(options={})
    if !self.images.empty?
      image_url = self.images.first.attachment.url
    end
    taxons = self.taxons.map { |t| t.name }
    product = {
      name: self.name,
      taxons: taxons,
      price: self.price,
      image_url: image_url
    }
    product.to_json
  end

  after_commit on: [:create] do
    index_document
  end

  after_commit on: [:update] do
    update_document
  end

  after_commit on: [:destroy] do
    delete_document
  end

  #this can be modified to call a background worker
  def index_document
    __elasticsearch__.index_document
  end

  def delete_document
    __elasticsearch__.delete_document
  end

  #alias this method so that an update completely overwrites the document and
  #doesn't update only the updated fields
  alias_method :update_document,:index_document

end

{'fraction_non_alphanumeric': 0.08750882145377559, 'fraction_numerical': 0.0, 'mean_word_length': 3.6323529411764706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4609749', 'n_tokens_mistral': 926, 'n_tokens_neox': 912, 'n_words': 230}