Module: Spree::SearchIndexable

Extended by:
ActiveSupport::Concern
Included in:
Product
Defined in:
app/models/concerns/spree/search_indexable.rb

Instance Method Summary collapse

Instance Method Details

#add_to_search_index ⇒ Object

Index this record synchronously (inline, no job). Useful for bulk imports, rake tasks, or when you need immediate indexing.



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/concerns/spree/search_indexable.rb', line 12

def add_to_search_index
  return unless search_indexing_enabled?

  store_ids_for_indexing.each do |store_id|
    store = Spree::Store.find_by(id: store_id)
    next unless store

    provider = Spree.search_provider.constantize.new(store)
    provider.index(self)
  end
end

#enqueue_search_index ⇒ Object



48
49
50
51
52
53
54
# File 'app/models/concerns/spree/search_indexable.rb', line 48

def enqueue_search_index
  return unless search_indexing_enabled?

  store_ids_for_indexing.each do |store_id|
    Spree::SearchProvider::IndexJob.perform_later(self.class.name, id.to_s, store_id.to_s)
  end
end

#enqueue_search_removal ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/models/concerns/spree/search_indexable.rb', line 56

def enqueue_search_removal
  return unless search_indexing_enabled?

  pid = prefixed_id
  store_ids_for_indexing.each do |store_id|
    Spree::SearchProvider::RemoveJob.perform_later(pid, store_id.to_s)
  end
end

#remove_from_search_index ⇒ Object

Remove this record from search index synchronously (inline, no job).



36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/concerns/spree/search_indexable.rb', line 36

def remove_from_search_index
  return unless search_indexing_enabled?

  store_ids_for_indexing.each do |store_id|
    store = Spree::Store.find_by(id: store_id)
    next unless store

    provider = Spree.search_provider.constantize.new(store)
    provider.remove(self)
  end
end

#search_indexing_enabled? ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'app/models/concerns/spree/search_indexable.rb', line 65

def search_indexing_enabled?
  Spree.search_provider.constantize.indexing_required?
rescue NameError
  false
end

#search_presentation(store = nil) ⇒ Object

Returns the hash that would be sent to the search provider for indexing. Useful for debugging and previewing what gets indexed.

product.search_presentation         # uses Spree::Current.store
product.search_presentation(store)  # explicit store
=> { id: 1, name: "Shirt", price_USD: 19.99, ... }


30
31
32
33
# File 'app/models/concerns/spree/search_indexable.rb', line 30

def search_presentation(store = nil)
  store ||= Spree::Current.store
  Spree::Dependencies.search_product_presenter_class.new(self, store).call
end

#store_ids_for_indexing ⇒ Object

Stores against which this record should be indexed/removed. By default we use the record's own store_id (Product, Customer, Order, …). The multi-store extension overrides this to fan out across every store the record is attached to. Falling back to Spree::Store.default covers records like categories/taxonomies that don't have a store_id column.



76
77
78
79
80
# File 'app/models/concerns/spree/search_indexable.rb', line 76

def store_ids_for_indexing
  return [store_id] if respond_to?(:store_id) && store_id.present?

  Array(Spree::Store.default&.id)
end