Class: Spree::SearchProvider::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/search_provider/base.rb

Direct Known Subclasses

Database

Constant Summary collapse

SORT_FIELDS =

Sort keys a provider accepts beyond the product's sortable attributes. price also sorts descending as -price.

%w[price best_selling manual].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, audience: :store) ⇒ Base

Returns a new instance of Base.

Parameters:

  • store (Spree::Store)
  • audience (Symbol, nil) (defaults to: :store) —

    who searches: :store (shoppers) limits filters and sorts to what the storefront may see; nil is the back office



20
21
22
23
# File 'app/models/spree/search_provider/base.rb', line 20

def initialize(store, audience: :store)
  @store = store
  @audience = audience
end

Instance Attribute Details

#audience ⇒ Object (readonly)

Returns the value of attribute audience.



8
9
10
# File 'app/models/spree/search_provider/base.rb', line 8

def audience
  @audience
end

#store ⇒ Object (readonly)

Returns the value of attribute store.



8
9
10
# File 'app/models/spree/search_provider/base.rb', line 8

def store
  @store
end

Class Method Details

.indexing_required? ⇒ Boolean

Whether this provider requires background indexing jobs. Override in subclasses. Database provider returns false.

Returns:

  • (Boolean)


12
13
14
# File 'app/models/spree/search_provider/base.rb', line 12

def self.indexing_required?
  false
end

Instance Method Details

#custom_field_schema ⇒ CustomFieldSchema

Returns:



26
27
28
# File 'app/models/spree/search_provider/base.rb', line 26

def custom_field_schema
  @custom_field_schema ||= CustomFieldSchema.new(store, audience: audience)
end

#ensure_index_settings! ⇒ Object

Configure index settings (filterable, sortable, searchable attributes). Called by rake task before indexing. No-op for database provider.



91
92
93
# File 'app/models/spree/search_provider/base.rb', line 91

def ensure_index_settings!
  # no-op by default
end

#filters(scope:, query: nil, filters: {}) ⇒ FiltersResult

Compute filter facets, sort options, and total count for the given scope. Called by the dedicated filters endpoint — kept separate from search_and_filter to avoid expensive facet queries on every product listing.

Parameters:

  • scope (ActiveRecord::Relation) —

    base scope

  • query (String, nil) (defaults to: nil) —

    text search query

  • filters (Hash) (defaults to: {}) —

    structured filters

Returns:

Raises:

  • (NotImplementedError)


57
58
59
# File 'app/models/spree/search_provider/base.rb', line 57

def filters(scope:, query: nil, filters: {})
  raise NotImplementedError
end

#index(product) ⇒ Object

Index a product — called after product save. No-op for database provider.

Parameters:



64
65
66
# File 'app/models/spree/search_provider/base.rb', line 64

def index(product)
  # no-op by default
end

#index_batch(documents) ⇒ Object

Index a batch of documents. Called by rake task with pre-serialized documents.

Parameters:

  • documents (Array<Hash>) —

    serialized product documents



85
86
87
# File 'app/models/spree/search_provider/base.rb', line 85

def index_batch(documents)
  # no-op by default
end

#metafield_schema ⇒ Object

Deprecated 6.0 name, removed in 6.3.



31
32
33
34
# File 'app/models/spree/search_provider/base.rb', line 31

def metafield_schema
  Spree::Deprecation.warn('#metafield_schema is deprecated and will be removed in Spree 6.3. Use #custom_field_schema instead.')
  custom_field_schema
end

#reindex(scope = nil) ⇒ Object

Bulk reindex — full catalog sync. Called manually or via rake task.

Parameters:

  • scope (ActiveRecord::Relation) (defaults to: nil) —

    products to reindex (default: all in store)



98
99
100
# File 'app/models/spree/search_provider/base.rb', line 98

def reindex(scope = nil)
  # no-op by default
end

#remove(product) ⇒ Object

Remove a product from the index.

Parameters:



71
72
73
# File 'app/models/spree/search_provider/base.rb', line 71

def remove(product)
  # no-op by default
end

#remove_by_id(prefixed_id) ⇒ Object

Remove a document from the index by prefixed ID (used when record is already deleted).

Parameters:

  • prefixed_id (String) —

    the prefixed ID (e.g. 'prod_abc')



78
79
80
# File 'app/models/spree/search_provider/base.rb', line 78

def remove_by_id(prefixed_id)
  # no-op by default
end

#search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25) ⇒ SearchResult

Search and paginate products. Does NOT compute filter facets — use #filters for that.

Parameters:

  • scope (ActiveRecord::Relation) —

    base scope (store-scoped, visibility-filtered, authorized)

  • query (String, nil) (defaults to: nil) —

    text search query

  • filters (Hash) (defaults to: {}) —

    structured filters (price_gte, with_option_value_ids, in_category, in_categories, etc.)

  • sort (String, nil) (defaults to: nil) —

    sort param (e.g. 'price', '-price', 'best_selling')

  • page (Integer) (defaults to: 1) —

    page number

  • limit (Integer) (defaults to: 25) —

    results per page

Returns:

Raises:

  • (NotImplementedError)


45
46
47
# File 'app/models/spree/search_provider/base.rb', line 45

def search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25)
  raise NotImplementedError
end