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_indexObject

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_indexObject



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_removalObject



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_indexObject

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_indexingObject



71
72
73
74
75
76
77
78
79
# File 'app/models/concerns/spree/search_indexable.rb', line 71

def store_ids_for_indexing
  if respond_to?(:store_ids)
    store_ids
  elsif respond_to?(:store_id)
    [store_id].compact
  else
    Spree::Store.pluck(:id)
  end
end