Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve extensibility of Meilisearch search provider (#13810)

Damian Legawiec3d3b70f49de6
diff --git a/docs/developer/how-to/custom-search-provider.mdx b/docs/developer/how-to/custom-search-provider.mdx
index 5c46a6cdce..6d01f1e354 100644
--- a/docs/developer/how-to/custom-search-provider.mdx
+++ b/docs/developer/how-to/custom-search-provider.mdx
@@ -135,7 +135,7 @@ The `ProductPresenter` returns an array of documents. For a store with US (USD/E
## Step 4: Implement Bulk Reindex
-Use `ProductPresenter::REQUIRED_PRELOADS` to avoid N+1 queries:
+Use `preload_associations_lazily` to avoid N+1 queries:
```ruby app/models/my_app/search_provider/typesense.rb
def reindex(scope = nil)
@@ -143,9 +143,9 @@ def reindex(scope = nil)
ensure_index_settings!
scope.reorder(id: :asc)
- .preload(*Spree::SearchProvider::ProductPresenter::REQUIRED_PRELOADS)
+ .preload_associations_lazily
.find_in_batches(batch_size: 500) do |batch|
- documents = batch.flat_map { |p| Spree::SearchProvider::ProductPresenter.new(p, store).call }
+ documents = batch.flat_map { |p| presenter_class.new(p, store).call }
index_batch(documents)
end
end
diff --git a/spree/core/app/models/concerns/spree/search_indexable.rb b/spree/core/app/models/concerns/spree/search_indexable.rb
index 1acd93e385..3912803d62 100644
--- a/spree/core/app/models/concerns/spree/search_indexable.rb
+++ b/spree/core/app/models/concerns/spree/search_indexable.rb
@@ -29,7 +29,7 @@ module Spree
# => { id: 1, name: "Shirt", price_USD: 19.99, ... }
def search_presentation(store = nil)
store ||= Spree::Current.store
- Spree::SearchProvider::ProductPresenter.new(self, store).call
+ Spree::Dependencies.search_product_presenter_class.new(self, store).call
end
# Remove this record from search index synchronously (inline, no job).
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index 3dcd8cff87..677e618e62 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -1,3 +1,5 @@
+require 'pagy/toolbox/paginators/meilisearch'
+
module Spree
module SearchProvider
class Meilisearch < Base
@@ -66,7 +68,7 @@ module Spree
end
def index(product)
- documents = ProductPresenter.new(product, store).call
+ documents = presenter_class.new(product, store).call
client.index(index_name).add_documents(documents, 'prefixed_id')
end
@@ -91,9 +93,9 @@ module Spree
ensure_index_settings!
scope.reorder(id: :asc)
- .preload(*ProductPresenter::REQUIRED_PRELOADS)
+ .preload_associations_lazily
.find_in_batches(batch_size: 500) do |batch|
- documents = batch.flat_map { |product| ProductPresenter.new(product, store).call }
+ documents = batch.flat_map { |product| presenter_class.new(product, store).call }
index_batch(documents)
end
end
@@ -109,6 +111,10 @@ module Spree
private
+ def presenter_class
+ Spree::Dependencies.search_product_presenter_class
+ end
+
def client
@client ||= ::Meilisearch::Client.new(
ENV.fetch('MEILISEARCH_URL', 'http://localhost:7700'),
@@ -140,152 +146,165 @@ module Spree
%w[price -price name -name -available_on available_on best_selling]
end
- # Convert Ransack-style filters to Meilisearch filter syntax.
- # Always applies store scoping, active status, and currency availability
- # so that Meilisearch results match what the AR scope would return.
- # All values are sanitized to prevent filter injection.
+ # Build Meilisearch filter conditions from API params.
+ # Combines system scoping (always applied) with user-facing filters.
def build_filters(filters)
- conditions = []
+ conditions = system_filter_conditions
+ conditions.concat(user_filter_conditions(filters))
+ conditions
+ end
- # Always scope to current store, locale, currency, active, not discontinued.
- # This mirrors the AR scope: store.products.active(currency) with locale
+ # System scoping — always applied. Rarely overridden.
+ # Mirrors the AR scope: store.products.active(currency) with locale.
+ def system_filter_conditions
+ conditions = []
conditions << "store_ids = '#{store.id}'"
conditions << "status = 'active'"
conditions << "locale = '#{locale.to_s.gsub(/[^a-zA-Z_-]/, '')}'"
conditions << "currency = '#{currency.to_s.gsub(/[^A-Z]/, '')}'"
conditions << "(discontinue_on = 0 OR discontinue_on > #{Time.current.to_i})"
+ conditions
+ end
+ # User-facing filters — override to add custom filter pre/post processing.
+ def user_filter_conditions(filters)
+ conditions = []
filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)
return conditions if filters.blank?
filters.each do |key, value|
next if value.blank?
- key = key.to_s
- case key
- when 'price_gte'
- conditions << "price >= #{value.to_f}"
- when 'price_lte'
- conditions << "price <= #{value.to_f}"
- when 'in_stock'
- conditions << 'in_stock = true' if value.to_s != '0'
- when 'out_of_stock'
- conditions << 'in_stock = false' if value.to_s != '0'
- when 'categories_id_eq'
- conditions << "category_ids = '#{sanitize_prefixed_id(value)}'" if valid_prefixed_id?(value)
- when 'with_option_value_ids'
- Array(value).each do |ov|
- conditions << "option_value_ids = '#{sanitize_prefixed_id(ov)}'" if valid_prefixed_id?(ov)
- end
+ condition = build_filter_condition(key.to_s, value)
+ if condition.is_a?(Array)
+ conditions.concat(condition)
+ elsif condition
+ conditions << condition
end
end
conditions
end
+ # Translate a single Ransack-style filter param into Meilisearch filter syntax.
+ # Override in subclasses to handle custom filter keys — call super for built-in filters.
+ def build_filter_condition(key, value)
+ case key
+ when 'price_gte'
+ "price >= #{value.to_f}"
+ when 'price_lte'
+ "price <= #{value.to_f}"
+ when 'in_stock'
+ 'in_stock = true' if value.to_s != '0'
+ when 'out_of_stock'
+ 'in_stock = false' if value.to_s != '0'
+ when 'categories_id_eq'
+ "category_ids = '#{sanitize_prefixed_id(value)}'" if valid_prefixed_id?(value)
+ when 'with_option_value_ids'
+ Array(value).filter_map { |ov| "option_value_ids = '#{sanitize_prefixed_id(ov)}'" if valid_prefixed_id?(ov) }
+ end
+ end
+
+ # Sort param to Meilisearch sort syntax.
+ # Override in subclasses to handle custom sort keys — call super for built-in sorts.
def build_sort(sort)
return nil if sort.blank?
+ sort_mapping(sort)
+ end
+
+ # Map a sort param to Meilisearch sort syntax.
+ # Override in subclasses to add custom sorts — call super for built-in sorts.
+ def sort_mapping(sort)
case sort
- when 'price'
- ['price:asc']
- when '-price'
- ['price:desc']
- when 'name'
- ['name:asc']
- when '-name'
- ['name:desc']
- when '-available_on'
- ['available_on:desc']
- when 'available_on'
- ['available_on:asc']
- when 'best_selling'
- ['units_sold_count:desc']
+ when 'price' then ['price:asc']
+ when '-price' then ['price:desc']
+ when 'name' then ['name:asc']
+ when '-name' then ['name:desc']
+ when '-available_on' then ['available_on:desc']
+ when 'available_on' then ['available_on:asc']
+ when 'best_selling' then ['units_sold_count:desc']
end
end
- # Transform Meilisearch facetDistribution into our standard filter response format
+ # Transform Meilisearch facetDistribution into standard filter response format.
+ # Override in subclasses to add custom facets — call super and append.
def build_facet_response(facet_distribution)
- filters = []
-
- # Price range
- if facet_distribution['price'].present?
- amounts = facet_distribution['price'].keys.map(&:to_f)
- filters << {
- id: 'price',
- type: 'price_range',
- min: amounts.min,
- max: amounts.max,
- currency: currency
- }
- end
+ facets = []
+ facets << build_price_facet(facet_distribution['price']) if facet_distribution['price'].present?
+ facets << build_availability_facet(facet_distribution['in_stock']) if facet_distribution['in_stock'].present?
+ facets.concat(build_option_facets(facet_distribution['option_value_ids'])) if facet_distribution['option_value_ids'].present?
+ facets << build_category_facet(facet_distribution['category_ids']) if facet_distribution['category_ids'].present?
+ facets.compact
+ end
- # Availability
- if facet_distribution['in_stock'].present?
- in_stock = facet_distribution['in_stock']['true'] || 0
- out_of_stock = facet_distribution['in_stock']['false'] || 0
- filters << {
- id: 'availability',
- type: 'availability',
- options: [
- { id: 'in_stock', count: in_stock },
- { id: 'out_of_stock', count: out_of_stock }
- ]
- }
- end
+ def build_price_facet(distribution)
+ amounts = distribution.keys.map(&:to_f)
+ {
+ id: 'price',
+ type: 'price_range',
+ min: amounts.min,
+ max: amounts.max,
+ currency: currency
+ }
+ end
- # Option values — group by option type for faceted display
- if facet_distribution['option_value_ids'].present?
- prefixed_ids = facet_distribution['option_value_ids'].keys
- raw_ids = prefixed_ids.filter_map { |pid| Spree::OptionValue.decode_prefixed_id(pid) }
- option_values = Spree::OptionValue.where(id: raw_ids).includes(:option_type).preload_associations_lazily.index_by(&:prefixed_id)
-
- # Group by option type
- by_option_type = {}
- facet_distribution['option_value_ids'].each do |ov_prefixed_id, count|
- ov = option_values[ov_prefixed_id]
- next unless ov
-
- ot = ov.option_type
- by_option_type[ot] ||= []
- by_option_type[ot] << { id: ov.prefixed_id, name: ov.name, label: ov.label, position: ov.position, count: count }
- end
+ def build_availability_facet(distribution)
+ {
+ id: 'availability',
+ type: 'availability',
+ options: [
+ { id: 'in_stock', count: distribution['true'] || 0 },
+ { id: 'out_of_stock', count: distribution['false'] || 0 }
+ ]
+ }
+ end
- by_option_type.each do |option_type, values|
- filters << {
- id: option_type.prefixed_id,
- type: 'option',
- name: option_type.name,
- label: option_type.label,
- options: values.sort_by { |o| o[:position] }
- }
- end
+ def build_option_facets(distribution)
+ prefixed_ids = distribution.keys
+ raw_ids = prefixed_ids.filter_map { |pid| Spree::OptionValue.decode_prefixed_id(pid) }
+ option_values = Spree::OptionValue.where(id: raw_ids).includes(:option_type).preload_associations_lazily.index_by(&:prefixed_id)
+
+ # Group by option type
+ by_option_type = {}
+ distribution.each do |ov_prefixed_id, count|
+ ov = option_values[ov_prefixed_id]
+ next unless ov
+
+ ot = ov.option_type
+ by_option_type[ot] ||= []
+ by_option_type[ot] << { id: ov.prefixed_id, name: ov.name, label: ov.label, position: ov.position, count: count }
end
- # Categories
- if facet_distribution['category_ids'].present?
- prefixed_ids = facet_distribution['category_ids'].keys
- raw_ids = prefixed_ids.filter_map { |pid| Spree::Taxon.decode_prefixed_id(pid) }
- categories = Spree::Taxon.where(id: raw_ids).index_by(&:prefixed_id)
-
- filters << {
- id: 'categories',
- type: 'category',
- options: facet_distribution['category_ids'].filter_map do |prefixed_id, count|
- cat = categories[prefixed_id]
- next unless cat
-
- { id: cat.prefixed_id, name: cat.name, permalink: cat.permalink, count: count }
- end
+ by_option_type.map do |option_type, values|
+ {
+ id: option_type.prefixed_id,
+ type: 'option',
+ name: option_type.name,
+ label: option_type.label,
+ options: values.sort_by { |o| o[:position] }
}
end
+ end
+
+ def build_category_facet(distribution)
+ prefixed_ids = distribution.keys
+ raw_ids = prefixed_ids.filter_map { |pid| Spree::Taxon.decode_prefixed_id(pid) }
+ categories = Spree::Taxon.where(id: raw_ids).index_by(&:prefixed_id)
- filters
+ {
+ id: 'categories',
+ type: 'category',
+ options: distribution.filter_map do |prefixed_id, count|
+ cat = categories[prefixed_id]
+ next unless cat
+
+ { id: cat.prefixed_id, name: cat.name, permalink: cat.permalink, count: count }
+ end
+ }
end
def build_pagy(ms_result, page, limit)
- require 'pagy/toolbox/paginators/meilisearch'
-
fake_result = Struct.new(:raw_answer).new({
'totalHits' => ms_result['estimatedTotalHits'] || 0,
'hitsPerPage' => limit,
diff --git a/spree/core/app/presenters/spree/search_provider/product_presenter.rb b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
index 3c3c145d46..ffbe2be8c9 100644
--- a/spree/core/app/presenters/spree/search_provider/product_presenter.rb
+++ b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
@@ -1,12 +1,6 @@
module Spree
module SearchProvider
class ProductPresenter
- # Associations needed by this presenter — used by reindex and rake task for preloading
- REQUIRED_PRELOADS = [
- :taxons, :option_types, :primary_media, :store_products,
- { variants_including_master: [:prices, :option_values] }
- ].freeze
-
attr_reader :product, :store
def initialize(product, store)
diff --git a/spree/core/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rb
index 3d37722c4f..c5b52f557b 100644
--- a/spree/core/lib/spree/core/dependencies.rb
+++ b/spree/core/lib/spree/core/dependencies.rb
@@ -103,7 +103,10 @@ module Spree
products_finder: 'Spree::Products::Find',
taxon_finder: 'Spree::Taxons::Find',
line_item_by_variant_finder: 'Spree::LineItems::FindByVariant',
- variant_finder: 'Spree::Variants::Find'
+ variant_finder: 'Spree::Variants::Find',
+
+ # search
+ search_product_presenter: 'Spree::SearchProvider::ProductPresenter'
}.freeze
include Spree::DependenciesHelper