<commit_before># Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/

module HasSearchIndexBackend
  extend ActiveSupport::Concern

  included do
    after_create  :search_index_update
    after_update  :search_index_update
    after_touch   :search_index_update_touch
    after_destroy :search_index_destroy
  end

=begin

update search index, if configured - will be executed automatically

  model = Model.find(123)
  model.search_index_update

=end

  def search_index_update
    return true if ignore_search_indexing?(:update)

    # start background job to transfer data to search index
    return true if !SearchIndexBackend.enabled?

    return true if saved_changes.blank?

    SearchIndexJob.perform_later(self.class.to_s, id)
    SearchIndexAssociationsJob.perform_later(self.class.to_s, id)
    true
  end

=begin

update search index, if configured - will be executed automatically

  model = Model.find(123)
  model.search_index_update_touch

=end

  def search_index_update_touch
    return true if ignore_search_indexing?(:update)

    # start background job to transfer data to search index
    return true if !SearchIndexBackend.enabled?

    SearchIndexJob.perform_later(self.class.to_s, id)
    true
  end

  def search_index_indexable
    Models.indexable.reject { |local_class| local_class == self.class }
  end

  def search_index_indexable_attributes(index_class)
    result = []
    index_class.new.attributes.each do |key, _value|
      attribute_name = key.to_s
      next if attribute_name.blank?

      # due to performance reasons, we only want to process some attributes for specific classes (e.g. tickets)
      next if !index_class.search_index_attribute_relevant?(attribute_name)

      attribute_ref_name = index_class.search_index_attribute_ref_name(attribute_name)
      next if attribute_ref_name.blank?

      association = index_class.reflect_on_association(attribute_ref_name)
      next if association.blank?
      next if association.options[:polymorphic]<commit_msg>Revert "Follow up 886cd320c8bced0b48c1992ce2bf8b383ef149f9 - Revert after_commit changes because previous_changes hash is broken/empty by a bug when transaction/locks are used."<commit_after># Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/

module HasSearchIndexBackend
  extend ActiveSupport::Concern

  included do
    after_commit  :search_index_update, if: :persisted?
    after_destroy :search_index_destroy
  end

=begin

update search index, if configured - will be executed automatically

  model = Model.find(123)
  model.search_index_update

=end

  def search_index_update
    return true if ignore_search_indexing?(:update)

    # start background job to transfer data to search index
    return true if !SearchIndexBackend.enabled?

    return true if previous_changes.blank?

    SearchIndexJob.perform_later(self.class.to_s, id)
    SearchIndexAssociationsJob.perform_later(self.class.to_s, id)
    true
  end

  def search_index_indexable
    Models.indexable.reject { |local_class| local_class == self.class }
  end

  def search_index_indexable_attributes(index_class)
    result = []
    index_class.new.attributes.each do |key, _value|
      attribute_name = key.to_s
      next if attribute_name.blank?

      # due to performance reasons, we only want to process some attributes for specific classes (e.g. tickets)
      next if !index_class.search_index_attribute_relevant?(attribute_name)

      attribute_ref_name = index_class.search_index_attribute_ref_name(attribute_name)
      next if attribute_ref_name.blank?

      association = index_class.reflect_on_association(attribute_ref_name)
      next if association.blank?
      next if association.options[:polymorphic]
{'fraction_non_alphanumeric': 0.06256656017039404, 'fraction_numerical': 0.012779552715654952, 'mean_word_length': 4.284106891701828, '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': '4884234', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1095, 'n_words': 284}