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 performance of Products API with database adapter for large S… (#13945)

Damian Legawiecedba094f9fbb
diff --git a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
index 01517bb684..8e6ff7c8d5 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
@@ -7,24 +7,48 @@ module Spree
include Spree::Api::V3::Store::SearchProviderSupport
def index
- result = search_provider.search_and_filter(
- scope: filters_scope,
- query: search_query,
- filters: (search_filters || {}).merge('_category' => category),
- page: 1,
- limit: 0
- )
-
- render json: {
- filters: result.filters,
- sort_options: result.sort_options,
- default_sort: result.default_sort,
- total_count: result.total_count
- }
+ json = Rails.cache.fetch(filters_cache_key, expires_in: 15.minutes) do
+ result = search_provider.filters(
+ scope: filters_scope,
+ query: search_query,
+ filters: (search_filters || {}).merge('_category' => category)
+ )
+
+ {
+ filters: result.filters,
+ sort_options: result.sort_options,
+ default_sort: result.default_sort,
+ total_count: result.total_count
+ }
+ end
+
+ render json: json
end
private
+ def filters_cache_key
+ products_table = Spree::Product.table_name
+ stats = current_store.products.active(current_currency)
+ .pick(Arel.sql("MAX(#{products_table}.updated_at)"), Arel.sql("COUNT(DISTINCT #{products_table}.id)"))
+ max_updated = stats&.first&.to_i
+ product_count = stats&.last || 0
+
+ parts = [
+ 'spree/api/v3/store/filters',
+ current_store.id,
+ current_currency,
+ current_locale,
+ category&.cache_key_with_version,
+ search_query,
+ search_filters&.sort_by(&:first)&.to_json,
+ max_updated,
+ product_count
+ ]
+
+ parts.compact.join('/')
+ end
+
def filters_scope
scope = current_store.products.active(current_currency)
scope = scope.in_category(category) if category.present?
diff --git a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
index 6bca910bf5..01eb471db7 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -55,7 +55,6 @@ module Spree
limit: limit
)
- @search_result = result
@pagy = result.pagy
@collection = result.products
end
diff --git a/spree/api/app/services/spree/api/v3/filters_aggregator.rb b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
index b1bcc6b492..6ad833f00d 100644
--- a/spree/api/app/services/spree/api/v3/filters_aggregator.rb
+++ b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
@@ -81,13 +81,106 @@ module Spree
end
def option_type_filters
- Spree::OptionType.filterable.includes(:option_values).order(:position).filter_map do |option_type|
- # Disjunctive: count against scope WITHOUT this option type's filter
- count_scope = disjunctive_scope_for(option_type)
- values = option_type.option_values.for_products(count_scope).distinct.order(:position)
- next if values.empty?
+ option_types = Spree::OptionType.filterable.order(:position).to_a
+ return [] if option_types.empty?
- count_ids = count_scope.reorder('').distinct.pluck(:id)
+ type_ids = option_types.map(&:id)
+
+ # Pluck only the columns we need — avoids instantiating thousands of AR models.
+ # Force default locale so Mobility returns column values (not translations);
+ # translated labels are overlaid separately via load_option_value_translations.
+ ov_rows = Mobility.with_locale(I18n.default_locale) do
+ Spree::OptionValue
+ .where(option_type_id: type_ids)
+ .order(:position)
+ .pluck(:id, :option_type_id, :name, :presentation, :position, :color_code)
+ end
+ return [] if ov_rows.empty?
+
+ all_ov_ids = ov_rows.map(&:first)
+
+ # Batch-load image attachment existence (single query)
+ ov_ids_with_images = ActiveStorage::Attachment
+ .where(record_type: 'Spree::OptionValue', name: 'image', record_id: all_ov_ids)
+ .pluck(:record_id)
+ .to_set
+
+ # Batch-load translations for current locale (single query, skip for default locale with column_fallback)
+ ov_translations = load_option_value_translations(all_ov_ids)
+
+ # Group rows by option type
+ ov_rows_by_type = ov_rows.group_by { |row| row[1] }
+
+ # Batch counts
+ if grouped_selected_options.empty?
+ counts = batch_option_value_counts(@scope_before_options, all_ov_ids)
+ else
+ scope_groups = option_types.group_by { |ot| disjunctive_scope_for(ot) }
+ counts = {}
+ scope_groups.each do |scope, types|
+ ov_ids = types.flat_map { |t| ov_rows_by_type[t.id]&.map(&:first) || [] }
+ counts.merge!(batch_option_value_counts(scope, ov_ids))
+ end
+ end
+
+ build_option_type_results(option_types, ov_rows_by_type, counts, ov_ids_with_images, ov_translations)
+ end
+
+ # Single grouped COUNT query for all option value IDs against a product scope.
+ # Returns { option_value_id => product_count }
+ def batch_option_value_counts(product_scope, option_value_ids)
+ return {} if option_value_ids.empty?
+
+ ovv_table = Spree::OptionValueVariant.table_name
+ var_table = Spree::Variant.table_name
+
+ Spree::OptionValueVariant
+ .joins("INNER JOIN #{var_table} ON #{var_table}.id = #{ovv_table}.variant_id AND #{var_table}.deleted_at IS NULL")
+ .where(var_table => { product_id: product_scope.reorder('').select(:id) })
+ .where(ovv_table => { option_value_id: option_value_ids })
+ .group("#{ovv_table}.option_value_id")
+ .distinct
+ .count("#{var_table}.product_id")
+ end
+
+ # Load translated presentations for option values.
+ # Returns { option_value_id => translated_presentation } or empty hash when using the default locale.
+ def load_option_value_translations(ov_ids)
+ locale = Spree::Current.locale || I18n.locale.to_s
+ return {} if locale.to_s == I18n.default_locale.to_s
+
+ Spree::OptionValue::Translation
+ .where(spree_option_value_id: ov_ids, locale: locale)
+ .pluck(:spree_option_value_id, :presentation)
+ .to_h
+ end
+
+ def build_option_type_results(option_types, ov_rows_by_type, counts, ov_ids_with_images, ov_translations)
+ # Pre-load image URLs for option values that have images (single batch)
+ image_urls = load_image_urls(ov_ids_with_images)
+
+ option_types.filter_map do |option_type|
+ rows = ov_rows_by_type[option_type.id]
+ next if rows.blank?
+
+ # rows: [id, option_type_id, name, presentation, position, color_code]
+ options = rows.filter_map do |id, _, name, presentation, position, color_code|
+ count = counts[id] || 0
+ next if count.zero?
+
+ label = ov_translations[id] || presentation
+
+ {
+ id: encode_prefixed_id(:optval, id),
+ name: name,
+ label: label,
+ position: position,
+ color_code: color_code,
+ image_url: image_urls[id],
+ count: count
+ }
+ end
+ next if options.empty?
{
id: option_type.prefixed_id,
@@ -95,28 +188,25 @@ module Spree
name: option_type.name,
label: option_type.label,
kind: option_type.kind,
- options: values.map { |ov| option_value_data(count_ids, ov) }
+ options: options
}
end
end
- def option_value_data(product_ids, option_value)
- count = Spree::Product
- .where(id: product_ids)
- .joins(:option_value_variants)
- .where(Spree::OptionValueVariant.table_name => { option_value_id: option_value.id })
- .distinct
- .count
+ # Load image URLs for option values that have images.
+ # Only instantiates AR models for the small subset with images (typically color swatches).
+ def load_image_urls(ov_ids_with_images)
+ return {} if ov_ids_with_images.empty?
- {
- id: option_value.prefixed_id,
- name: option_value.name,
- label: option_value.label,
- position: option_value.position,
- color_code: option_value.color_code,
- image_url: option_value.image.attached? ? Rails.application.routes.url_helpers.cdn_image_url(option_value.image) : nil,
- count: count
- }
+ Spree::OptionValue.where(id: ov_ids_with_images.to_a)
+ .includes(image_attachment: :blob)
+ .each_with_object({}) do |ov, urls|
+ urls[ov.id] = Rails.application.routes.url_helpers.cdn_image_url(ov.image)
+ end
+ end
+
+ def encode_prefixed_id(prefix, id)
+ "#{prefix}_#{Spree::PrefixedId::SQIDS.encode([id])}"
end
# Returns the scope with all option type filters EXCEPT the given one applied.
diff --git a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
index 6196e34c19..7168d470ff 100644
--- a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
+++ b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
@@ -153,6 +153,69 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
end
end
+ describe 'option value translations' do
+ before do
+ # Create translations directly to avoid Mobility's column writer side effects
+ Spree::OptionValue::Translation.create!(spree_option_value_id: option_value_s.id, locale: 'pl', presentation: 'Mały')
+ Spree::OptionValue::Translation.create!(spree_option_value_id: option_value_m.id, locale: 'pl', presentation: 'Średni')
+ Spree::OptionType::Translation.create!(spree_option_type_id: option_type.id, locale: 'pl', presentation: 'Rozmiar')
+ end
+
+ context 'with default locale' do
+ it 'returns English presentation as label' do
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+ labels = size_filter[:options].map { |o| o[:label] }
+
+ expect(labels).to contain_exactly('S', 'M')
+ end
+
+ it 'returns English option type label' do
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+
+ expect(size_filter[:label]).to eq('Size')
+ end
+ end
+
+ context 'with non-default locale' do
+ it 'returns translated option value labels' do
+ I18n.locale = :pl
+ Spree::Current.locale = 'pl'
+
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+ labels = size_filter[:options].map { |o| o[:label] }
+
+ expect(labels).to contain_exactly('Mały', 'Średni')
+ end
+
+ it 'returns translated option type label' do
+ I18n.locale = :pl
+ Spree::Current.locale = 'pl'
+
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+
+ expect(size_filter[:label]).to eq('Rozmiar')
+ end
+ end
+
+ context 'with non-default locale and partial translations' do
+ before do
+ Spree::OptionValue::Translation.where(spree_option_value_id: option_value_m.id, locale: 'pl').delete_all
+ end
+
+ it 'falls back to English column value for untranslated option values' do
+ I18n.locale = :pl
+ Spree::Current.locale = 'pl'
+
+ size_filter = result[:filters].find { |f| f[:name] == 'size' }
+ s_option = size_filter[:options].find { |o| o[:name] == 'small' }
+ m_option = size_filter[:options].find { |o| o[:name] == 'medium' }
+
+ expect(s_option[:label]).to eq('Mały')
+ expect(m_option[:label]).to eq('M')
+ end
+ end
+ end
+
describe 'disjunctive option facet counts' do
let(:color_type) { create(:option_type, name: 'color', presentation: 'Color', filterable: true) }
let(:blue) { create(:option_value, option_type: color_type, name: 'blue', presentation: 'Blue') }
diff --git a/spree/core/app/models/spree/search_provider/base.rb b/spree/core/app/models/spree/search_provider/base.rb
index c8e6b9da3e..a15e2e40e9 100644
--- a/spree/core/app/models/spree/search_provider/base.rb
+++ b/spree/core/app/models/spree/search_provider/base.rb
@@ -13,7 +13,7 @@ module Spree
@store = store
end
- # Search, filter, and return facets in one call.
+ # Search and paginate products. Does NOT compute filter facets — use #filters for that.
#
# @param scope [ActiveRecord::Relation] base scope (store-scoped, visibility-filtered, authorized)
# @param query [String, nil] text search query
@@ -26,6 +26,18 @@ module Spree
raise NotImplementedError
end
+ # 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.
+ #
+ # @param scope [ActiveRecord::Relation] base scope
+ # @param query [String, nil] text search query
+ # @param filters [Hash] structured filters
+ # @return [FiltersResult]
+ def filters(scope:, query: nil, filters: {})
+ raise NotImplementedError
+ end
+
# Index a product — called after product save. No-op for database provider.
#
# @param product [Spree::Product] the product to index
diff --git a/spree/core/app/models/spree/search_provider/database.rb b/spree/core/app/models/spree/search_provider/database.rb
index dd0e78f323..0535e4ce5f 100644
--- a/spree/core/app/models/spree/search_provider/database.rb
+++ b/spree/core/app/models/spree/search_provider/database.rb
@@ -8,35 +8,16 @@ module Spree
}.freeze
def search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25)
- # 1. Text search
- scope = scope.search(query) if query.present?
-
- # 2. Extract internal params before passing to Ransack
- category = filters.is_a?(Hash) ? filters.delete('_category') || filters.delete(:_category) : nil
-
- # 2b. Extract option value IDs — handled by scope (OR within type, AND across)
- option_value_ids = filters.is_a?(Hash) ? filters.delete('with_option_value_ids') || filters.delete(:with_option_value_ids) : nil
-
- # 3. Structured filtering via Ransack
- ransack_filters = sanitize_filters(filters)
- if ransack_filters.present?
- search = scope.ransack(ransack_filters)
- scope = search.result(distinct: true)
- end
-
- # Save scope before option filters for disjunctive facet counts
- scope_before_options = scope
- if option_value_ids.present?
- scope = scope.with_option_value_ids(Array(option_value_ids))
- end
+ filters = filters.is_a?(Hash) ? filters.dup : {}
+ option_value_ids = filters.delete('with_option_value_ids') || filters.delete(:with_option_value_ids)
- # 4. Facets (before sorting to avoid computed column conflicts with count)
- filter_facets = build_facets(scope, category: category, option_value_ids: Array(option_value_ids), scope_before_options: scope_before_options)
+ scope = apply_search_and_filters(scope, query: query, filters: filters)
+ scope = scope.with_option_value_ids(Array(option_value_ids)) if option_value_ids.present?
- # 5. Total count (before sorting to avoid computed column conflicts with count)
+ # Total count (before sorting to avoid computed column conflicts with count)
total = scope.distinct.count
- # 6. Sorting + pagination
+ # Sorting + pagination
scope = apply_sort(scope, sort)
page = [page.to_i, 1].max
limit = limit.to_i.clamp(1, 100)
@@ -44,22 +25,56 @@ module Spree
SearchResult.new(
products: products,
+ total_count: total,
+ pagy: build_pagy(total, page, limit)
+ )
+ end
+
+ def filters(scope:, query: nil, filters: {})
+ filters = filters.is_a?(Hash) ? filters.dup : {}
+ category = filters.delete('_category') || filters.delete(:_category)
+ option_value_ids = Array(filters.delete('with_option_value_ids') || filters.delete(:with_option_value_ids))
+
+ # Apply text search + ransack filters (without option values)
+ scope_before_options = apply_search_and_filters(scope, query: query, filters: filters)
+
+ # Apply option value filters for the final scope
+ scope_with_options = if option_value_ids.present?
+ scope_before_options.with_option_value_ids(option_value_ids)
+ else
+ scope_before_options
+ end
+
+ filter_facets = build_facets(scope_with_options, category: category, option_value_ids: option_value_ids, scope_before_options: scope_before_options)
+
+ FiltersResult.new(
filters: filter_facets[:filters],
sort_options: filter_facets[:sort_options],
default_sort: filter_facets[:default_sort],
- total_count: total,
- pagy: build_pagy(total, page, limit)
+ total_count: filter_facets[:total_count]
)
end
private
+ def apply_search_and_filters(scope, query: nil, filters: {})
+ scope = scope.search(query) if query.present?
+
+ ransack_filters = sanitize_filters(filters)
+ if ransack_filters.present?
+ search = scope.ransack(ransack_filters)
+ scope = search.result(distinct: true)
+ end
+
+ scope
+ end
+
def build_pagy(count, page, limit)
Pagy::Offset.new(count: count, page: page, limit: limit)
end
def build_facets(scope, category: nil, option_value_ids: [], scope_before_options: nil)
- return { filters: [], sort_options: available_sort_options, default_sort: 'manual' } unless defined?(Spree::Api::V3::FiltersAggregator)
+ return { filters: [], sort_options: available_sort_options, default_sort: 'manual', total_count: scope.distinct.count } unless defined?(Spree::Api::V3::FiltersAggregator)
Spree::Api::V3::FiltersAggregator.new(
scope: scope,
@@ -74,7 +89,7 @@ module Spree
return {} if filters.blank?
filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)
- filters.except('search', :search, '_category', :_category)
+ filters.except('search', :search, '_category', :_category, 'with_option_value_ids', :with_option_value_ids)
end
def apply_sort(scope, sort)
diff --git a/spree/core/app/models/spree/search_provider/filters_result.rb b/spree/core/app/models/spree/search_provider/filters_result.rb
new file mode 100644
index 0000000000..5faa9c465e
--- /dev/null
+++ b/spree/core/app/models/spree/search_provider/filters_result.rb
@@ -0,0 +1,5 @@
+module Spree
+ module SearchProvider
+ FiltersResult = Struct.new(:filters, :sort_options, :default_sort, :total_count, keyword_init: true)
+ end
+end
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index 8ef4d6774f..939d406eeb 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -20,84 +20,9 @@ module Spree
def search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25)
page = [page.to_i, 1].max
limit = limit.to_i.clamp(1, 100)
- filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)
- filters = (filters || {}).stringify_keys
-
- # Extract and group option values by option type for proper OR/AND semantics
- option_value_ids = extract_and_delete(filters, 'with_option_value_ids')
- grouped_options = group_option_values_by_type(Array(option_value_ids))
-
- base_conditions = build_filters(filters)
- option_conditions = build_grouped_option_conditions(grouped_options)
- all_conditions = base_conditions + option_conditions
-
- search_params = {
- filter: all_conditions,
- facets: facet_attributes,
- sort: build_sort(sort),
- offset: (page - 1) * limit,
- limit: limit
- }
-
- Rails.logger.debug { "[Meilisearch] index=#{index_name} query=#{query.inspect} #{search_params.compact.inspect}" }
-
- begin
- if grouped_options.any?
- # N+1 multi-search: 1 hit query + 1 disjunctive facet query per active option type
- queries = [{ indexUid: index_name, q: query.to_s, **search_params }]
- option_type_ids_ordered = grouped_options.keys
- option_type_ids_ordered.each do |option_type_id|
- without_this = build_grouped_option_conditions(grouped_options.except(option_type_id))
- queries << { indexUid: index_name, q: query.to_s, filter: base_conditions + without_this, facets: ['option_value_ids'], limit: 0 }
- end
-
- results = client.multi_search(queries)
- ms_result = results['results'][0]
-
- # Merge disjunctive counts per option type.
- # Each disjunctive query excluded one option type's filter.
- # Use that query's full option_value_ids distribution for that option type's values,
- # and the main query's distribution for everything else.
- main_ov_dist = ms_result.dig('facetDistribution', 'option_value_ids') || {}
-
- # Build a set of prefixed IDs per option type (including unselected values)
- # by looking up which option type each option value belongs to.
- all_ov_prefixed_ids = Set.new
- disjunctive_dists = {}
- results['results'][1..].each_with_index do |r, idx|
- dist = r.dig('facetDistribution', 'option_value_ids') || {}
- disjunctive_dists[option_type_ids_ordered[idx]] = dist
- all_ov_prefixed_ids.merge(dist.keys)
- end
- # Resolve which prefixed IDs belong to which option type
- all_raw_ids = all_ov_prefixed_ids.filter_map { |pid| Spree::OptionValue.decode_prefixed_id(pid) }
- ov_to_type = Spree::OptionValue.where(id: all_raw_ids).pluck(:id, :option_type_id).to_h
- prefixed_to_type = all_ov_prefixed_ids.each_with_object({}) do |pid, h|
- raw = Spree::OptionValue.decode_prefixed_id(pid)
- h[pid] = ov_to_type[raw] if raw
- end
-
- # Start with main query's distribution, overlay disjunctive counts for active option types
- merged_ov_dist = main_ov_dist.dup
- disjunctive_dists.each do |option_type_id, dist|
- dist.each do |pid, count|
- merged_ov_dist[pid] = count if prefixed_to_type[pid] == option_type_id
- end
- end
-
- facet_distribution = (ms_result['facetDistribution'] || {}).merge('option_value_ids' => merged_ov_dist)
- else
- ms_result = client.index(index_name).search(query.to_s, search_params)
- facet_distribution = ms_result['facetDistribution'] || {}
- end
- rescue ::Meilisearch::ApiError => e
- Rails.logger.warn { "[Meilisearch] Search failed: #{e.message}. Run `rake spree:search:reindex` to initialize the index." }
- Rails.error.report(e, handled: true, context: { index: index_name, query: query })
- return empty_result(scope, page, limit)
- end
-
- Rails.logger.debug { "[Meilisearch] #{ms_result['estimatedTotalHits']} hits in #{ms_result['processingTimeMs']}ms" }
+ ms_result, _ = execute_search(query: query, filters: filters, sort: sort, page: page, limit: limit)
+ return empty_result(scope, page, limit) unless ms_result
# Hits have composite prefixed_id (prod_abc_en_USD), extract product_id (prod_abc)
product_prefixed_ids = ms_result['hits'].map { |h| h['product_id'] }.uniq
@@ -115,11 +40,28 @@ module Spree
SearchResult.new(
products: products,
+ total_count: ms_result['estimatedTotalHits'] || 0,
+ pagy: pagy
+ )
+ end
+
+ def filters(scope:, query: nil, filters: {})
+ ms_result, facet_distribution = execute_search(query: query, filters: filters, sort: nil, page: 1, limit: 0, return_facets: true)
+
+ unless ms_result
+ return FiltersResult.new(
+ filters: [],
+ sort_options: available_sort_options.map { |id| { id: id } },
+ default_sort: 'manual',
+ total_count: 0
+ )
+ end
+
+ FiltersResult.new(
filters: build_facet_response(facet_distribution),
sort_options: available_sort_options.map { |id| { id: id } },
default_sort: 'manual',
- total_count: ms_result['estimatedTotalHits'] || 0,
- pagy: pagy
+ total_count: ms_result['estimatedTotalHits'] || 0
)
end
@@ -193,6 +135,84 @@ module Spree
private
+ # Execute a Meilisearch query. Returns [ms_result, facet_distribution].
+ # facet_distribution is empty when return_facets is false. Returns nil on API error.
+ def execute_search(query:, filters:, sort:, page:, limit:, return_facets: false)
+ filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)
+ filters = (filters || {}).stringify_keys
+
+ option_value_ids = extract_and_delete(filters, 'with_option_value_ids')
+ grouped_options = group_option_values_by_type(Array(option_value_ids))
+
+ base_conditions = build_filters(filters)
+ option_conditions = build_grouped_option_conditions(grouped_options)
+ all_conditions = base_conditions + option_conditions
+
+ search_params = {
+ filter: all_conditions,
+ facets: return_facets ? facet_attributes : nil,
+ sort: build_sort(sort),
+ offset: (page - 1) * limit,
+ limit: limit
+ }.compact
+
+ Rails.logger.debug { "[Meilisearch] index=#{index_name} query=#{query.inspect} #{search_params.compact.inspect}" }
+
+ begin
+ if return_facets && grouped_options.any?
+ queries = [{ indexUid: index_name, q: query.to_s, **search_params }]
+ option_type_ids_ordered = grouped_options.keys
+ option_type_ids_ordered.each do |option_type_id|
+ without_this = build_grouped_option_conditions(grouped_options.except(option_type_id))
+ queries << { indexUid: index_name, q: query.to_s, filter: base_conditions + without_this, facets: ['option_value_ids'], limit: 0 }
+ end
+
+ results = client.multi_search(queries)
+ ms_result = results['results'][0]
+ facet_distribution = merge_disjunctive_facets(ms_result, results['results'][1..], option_type_ids_ordered)
+ else
+ ms_result = client.index(index_name).search(query.to_s, search_params)
+ facet_distribution = ms_result['facetDistribution'] || {}
+ end
+ rescue ::Meilisearch::ApiError => e
+ Rails.logger.warn { "[Meilisearch] Search failed: #{e.message}. Run `rake spree:search:reindex` to initialize the index." }
+ Rails.error.report(e, handled: true, context: { index: index_name, query: query })
+ return nil
+ end
+
+ Rails.logger.debug { "[Meilisearch] #{ms_result['estimatedTotalHits']} hits in #{ms_result['processingTimeMs']}ms" }
+
+ [ms_result, facet_distribution]
+ end
+
+ def merge_disjunctive_facets(ms_result, disjunctive_results, option_type_ids_ordered)
+ main_ov_dist = ms_result.dig('facetDistribution', 'option_value_ids') || {}
+
+ all_ov_prefixed_ids = Set.new
+ disjunctive_dists = {}
+ disjunctive_results.each_with_index do |r, idx|
+ dist = r.dig('facetDistribution', 'option_value_ids') || {}
+ disjunctive_dists[option_type_ids_ordered[idx]] = dist
+ all_ov_prefixed_ids.merge(dist.keys)
+ end
+
+ all_raw_ids = all_ov_prefixed_ids.filter_map { |pid| Spree::OptionValue.decode_prefixed_id(pid) }
+ ov_to_type = Spree::OptionValue.where(id: all_raw_ids).pluck(:id, :option_type_id).to_h
+ prefixed_to_type = all_ov_prefixed_ids.each_with_object({}) do |pid, h|
+ raw = Spree::OptionValue.decode_prefixed_id(pid)
+ h[pid] = ov_to_type[raw] if raw
+ end
+
+ merged_ov_dist = main_ov_dist.dup
+ disjunctive_dists.each do |option_type_id, dist|
+ dist.each do |pid, count|
+ merged_ov_dist[pid] = count if prefixed_to_type[pid] == option_type_id
+ end
+ end
+
+ (ms_result['facetDistribution'] || {}).merge('option_value_ids' => merged_ov_dist)
+ end
+
def presenter_class
Spree::Dependencies.search_product_presenter_class
end
@@ -436,9 +456,6 @@ module Spree
def empty_result(scope, page, limit)
SearchResult.new(
products: scope.none,
- filters: [],
- sort_options: available_sort_options.map { |id| { id: id } },
- default_sort: 'manual',
total_count: 0,
pagy: Pagy::Offset.new(count: 0, page: page, limit: limit)
)
diff --git a/spree/core/app/models/spree/search_provider/search_result.rb b/spree/core/app/models/spree/search_provider/search_result.rb
index fa4739ac63..e5f96c5575 100644
--- a/spree/core/app/models/spree/search_provider/search_result.rb
+++ b/spree/core/app/models/spree/search_provider/search_result.rb
@@ -1,5 +1,5 @@
module Spree
module SearchProvider
- SearchResult = Struct.new(:products, :filters, :sort_options, :default_sort, :total_count, :pagy, keyword_init: true)
+ SearchResult = Struct.new(:products, :total_count, :pagy, keyword_init: true)
end
end
diff --git a/spree/core/spec/models/spree/search_provider/database_spec.rb b/spree/core/spec/models/spree/search_provider/database_spec.rb
index cd42e01ff0..46856981fe 100644
--- a/spree/core/spec/models/spree/search_provider/database_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/database_spec.rb
@@ -28,10 +28,8 @@ module Spree
expect(result.total_count).to eq(2)
end
- it 'returns sort options as objects' do
- expect(result.sort_options).to be_an(Array)
- ids = result.sort_options.map { |o| o[:id] }
- expect(ids).to include('price', '-price', 'best_selling')
+ it 'does not include filter facets' do
+ expect(result).not_to respond_to(:filters)
end
end
@@ -198,6 +196,34 @@ module Spree
end
end
+ describe '#filters' do
+ let(:scope) { store.products }
+
+ subject(:result) { provider.filters(scope: scope) }
+
+ it 'returns a FiltersResult' do
+ expect(result).to be_a(SearchProvider::FiltersResult)
+ end
+
+ it 'returns sort options as objects' do
+ expect(result.sort_options).to be_an(Array)
+ ids = result.sort_options.map { |o| o[:id] }
+ expect(ids).to include('price', '-price', 'best_selling')
+ end
+
+ it 'returns total count' do
+ expect(result.total_count).to eq(3)
+ end
+
+ context 'with text search' do
+ subject(:result) { provider.filters(scope: scope, query: 'blue') }
+
+ it 'returns filtered total count' do
+ expect(result.total_count).to eq(2)
+ end
+ end
+ end
+
describe '#index' do
it 'is a no-op' do
expect { provider.index(product_1) }.not_to raise_error
diff --git a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
index 1e58e25857..dc32c13781 100644
--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
@@ -58,63 +58,9 @@ module Spree
expect(result.total_count).to eq(2)
end
- it 'returns facets from Meilisearch' do
- result = provider.search_and_filter(scope: store.products, query: 'shirt')
- expect(result.filters).to be_an(Array)
-
- price_filter = result.filters.find { |f| f[:type] == 'price_range' }
- expect(price_filter).to be_present
- expect(price_filter[:min]).to eq(19.99)
- expect(price_filter[:max]).to eq(29.99)
- end
-
- it 'returns sort options as objects' do
- result = provider.search_and_filter(scope: store.products, query: 'shirt')
- ids = result.sort_options.map { |o| o[:id] }
- expect(ids).to include('price', '-price')
- end
-
- context 'with option type facets' do
- let(:color_type) { create(:option_type, :color_swatch, filterable: true) }
- let(:red_value) { create(:option_value, option_type: color_type, name: 'red', presentation: 'Red', color_code: '#FF0000') }
- let(:blue_value) { create(:option_value, option_type: color_type, name: 'blue', presentation: 'Blue', color_code: '#0000FF') }
-
- let(:ms_response) do
- {
- 'hits' => [{ 'product_id' => product_1.prefixed_id }],
- 'estimatedTotalHits' => 1,
- 'facetDistribution' => {
- 'option_value_ids' => {
- red_value.prefixed_id => 3,
- blue_value.prefixed_id => 1
- }
- }
- }
- end
-
- it 'includes kind on option type filters' do
- result = provider.search_and_filter(scope: store.products, query: '')
- color_filter = result.filters.find { |f| f[:name] == 'color' }
-
- expect(color_filter).to be_present
- expect(color_filter[:kind]).to eq('color_swatch')
- end
-
- it 'includes color_code on option values' do
- result = provider.search_and_filter(scope: store.products, query: '')
- color_filter = result.filters.find { |f| f[:name] == 'color' }
- red_option = color_filter[:options].find { |o| o[:name] == 'red' }
-
- expect(red_option[:color_code]).to eq('#FF0000')
- end
-
- it 'includes image_url as nil when no image attached' do
- result = provider.search_and_filter(scope: store.products, query: '')
- color_filter = result.filters.find { |f| f[:name] == 'color' }
- red_option = color_filter[:options].find { |o| o[:name] == 'red' }
-
- expect(red_option[:image_url]).to be_nil
- end
+ it 'does not request facets from Meilisearch' do
+ expect(mock_index).to receive(:search).with(anything, satisfy { |params| !params.key?(:facets) })
+ provider.search_and_filter(scope: store.products, query: 'shirt')
end
context 'with no results' do
@@ -202,6 +148,112 @@ module Spree
end
end
+ describe '#filters' do
+ let(:ms_response) do
+ {
+ 'hits' => [],
+ 'estimatedTotalHits' => 2,
+ 'processingTimeMs' => 1,
+ 'facetDistribution' => {
+ 'in_stock' => { 'true' => 2 },
+ 'price' => { '19.99' => 1, '29.99' => 1 }
+ }
+ }
+ end
+
+ before do
+ allow(mock_index).to receive(:search).and_return(ms_response)
+ end
+
+ it 'returns a FiltersResult' do
+ result = provider.filters(scope: store.products, query: 'shirt')
+ expect(result).to be_a(SearchProvider::FiltersResult)
+ end
+
+ it 'returns facets from Meilisearch' do
+ result = provider.filters(scope: store.products, query: 'shirt')
+ expect(result.filters).to be_an(Array)
+
+ price_filter = result.filters.find { |f| f[:type] == 'price_range' }
+ expect(price_filter).to be_present
+ expect(price_filter[:min]).to eq(19.99)
+ expect(price_filter[:max]).to eq(29.99)
+ end
+
+ it 'returns sort options as objects' do
+ result = provider.filters(scope: store.products, query: 'shirt')
+ ids = result.sort_options.map { |o| o[:id] }
+ expect(ids).to include('price', '-price')
+ end
+
+ it 'returns total count' do
+ result = provider.filters(scope: store.products, query: 'shirt')
+ expect(result.total_count).to eq(2)
+ end
+
+ it 'requests facets from Meilisearch' do
+ expect(mock_index).to receive(:search).with(anything, hash_including(:facets))
+ provider.filters(scope: store.products, query: 'shirt')
+ end
+
+ context 'with option type facets' do
+ let(:color_type) { create(:option_type, :color_swatch, filterable: true) }
+ let(:red_value) { create(:option_value, option_type: color_type, name: 'red', presentation: 'Red', color_code: '#FF0000') }
+ let(:blue_value) { create(:option_value, option_type: color_type, name: 'blue', presentation: 'Blue', color_code: '#0000FF') }
+
+ let(:ms_response) do
+ {
+ 'hits' => [],
+ 'estimatedTotalHits' => 1,
+ 'processingTimeMs' => 1,
+ 'facetDistribution' => {
+ 'option_value_ids' => {
+ red_value.prefixed_id => 3,
+ blue_value.prefixed_id => 1
+ }
+ }
+ }
+ end
+
+ it 'includes kind on option type filters' do
+ result = provider.filters(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+
+ expect(color_filter).to be_present
+ expect(color_filter[:kind]).to eq('color_swatch')
+ end
+
+ it 'includes color_code on option values' do
+ result = provider.filters(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+ red_option = color_filter[:options].find { |o| o[:name] == 'red' }
+
+ expect(red_option[:color_code]).to eq('#FF0000')
+ end
+
+ it 'includes image_url as nil when no image attached' do
+ result = provider.filters(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+ red_option = color_filter[:options].find { |o| o[:name] == 'red' }
+
+ expect(red_option[:image_url]).to be_nil
+ end
+ end
+
+ context 'when Meilisearch API fails' do
+ before do
+ error = ::Meilisearch::ApiError.new(500, 'internal error', 'internal')
+ allow(mock_index).to receive(:search).and_raise(error)
+ end
+
+ it 'returns empty FiltersResult' do
+ result = provider.filters(scope: store.products, query: 'shirt')
+ expect(result.filters).to eq([])
+ expect(result.total_count).to eq(0)
+ end
+ end
+ end
+
describe '#index' do
it 'adds documents to Meilisearch index with id as primary key' do
expect(mock_index).to receive(:add_documents).with(