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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Index one document per market × locale for proper multi-locale/currency search
Damian Legawiec9a672ced64fa
diff --git a/docs/plans/6.0-search-provider.md b/docs/plans/5.4-search-provider.mdsimilarity index 99%rename from docs/plans/6.0-search-provider.mdrename to docs/plans/5.4-search-provider.mdindex 236fd3e3b6..285cca2b64 100644--- a/docs/plans/6.0-search-provider.md+++ b/docs/plans/5.4-search-provider.md@@ -1,7 +1,7 @@# Search Provider Interface-**Status:** Draft-**Target:** Spree 5.4 (provider + DB provider + PgSearch removal + add_search_scope removal), 6.0 (metafield faceting, Meilisearch gem)+**Status:** In progress+**Target:** Spree 5.4 (provider + DB provider + PgSearch removal + add_search_scope removal + Meilisearch provider), 6.0 (metafield faceting)**Depends on:** None for 5.4. ProductType (6.0-product-types.md) for metafield faceting in 6.0.**Author:** Damian + Claude**Last updated:** 2026-03-18diff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rbindex 03d0ca1f48..60de0a2f72 100644--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb@@ -441,7 +441,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doit 'searches via Meilisearch when q[search] is present' doallow(mock_index).to receive(:search).and_return({- 'hits' => [{ 'prefixed_id' => product.prefixed_id }],+ 'hits' => [{ 'product_id' => product.prefixed_id }],'estimatedTotalHits' => 1,'facetDistribution' => {}})@@ -458,7 +458,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doit 'respects AR scope visibility (does not return draft products from Meilisearch)' doallow(mock_index).to receive(:search).and_return({- 'hits' => [{ 'prefixed_id' => product.prefixed_id }, { 'prefixed_id' => draft_product.prefixed_id }],+ 'hits' => [{ 'product_id' => product.prefixed_id }, { 'product_id' => draft_product.prefixed_id }],'estimatedTotalHits' => 2,'facetDistribution' => {}})@@ -473,7 +473,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doit 'uses Meilisearch for browsing without search query' doallow(mock_index).to receive(:search).and_return({- 'hits' => [{ 'prefixed_id' => product.prefixed_id }, { 'prefixed_id' => product2.prefixed_id }],+ 'hits' => [{ 'product_id' => product.prefixed_id }, { 'product_id' => product2.prefixed_id }],'estimatedTotalHits' => 2,'facetDistribution' => {}})diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rbindex 802afa8f49..ea2f44e912 100644--- a/spree/core/app/models/spree/search_provider/meilisearch.rb+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb@@ -33,8 +33,9 @@ module SpreeRails.logger.debug { "[Meilisearch] #{ms_result['estimatedTotalHits']} hits in #{ms_result['processingTimeMs']}ms" }- prefixed_ids = ms_result['hits'].map { |h| h['prefixed_id'] }- raw_ids = prefixed_ids.filter_map { |pid| Spree::Product.decode_prefixed_id(pid) }+ # 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+ raw_ids = product_prefixed_ids.filter_map { |pid| Spree::Product.decode_prefixed_id(pid) }# Intersect with AR scope for security/visibility.# Since we filter by store/status/currency/discontinue_on in Meilisearch,@@ -59,12 +60,14 @@ module Spreeenddef index(product)- document = ProductPresenter.new(product, store).call- client.index(index_name).add_documents([document], 'prefixed_id')+ documents = ProductPresenter.new(product, store).call+ client.index(index_name).add_documents(documents, 'prefixed_id')enddef remove(product)- client.index(index_name).delete_document(product.prefixed_id)+ # Delete all locale/currency variants of this product+ filter = "product_id = '#{product.prefixed_id}'"+ client.index(index_name).delete_documents(filter: filter)rescue ::Meilisearch::ApiError => eraise unless e.http_code == 404end@@ -73,9 +76,10 @@ module Spreeclient.index(index_name).add_documents(documents, 'prefixed_id')end- # Remove document by prefixed_id (e.g. 'prod_abc')+ # Remove all documents for a product by its prefixed_id (e.g. 'prod_abc')def remove_by_id(prefixed_id)- client.index(index_name).delete_document(prefixed_id)+ filter = "product_id = '#{sanitize_prefixed_id(prefixed_id)}'"+ client.index(index_name).delete_documents(filter: filter)rescue ::Meilisearch::ApiError => eraise unless e.http_code == 404end@@ -116,29 +120,15 @@ module Spreeenddef searchable_attributes- ['name', 'name_*', 'description', 'description_*', 'sku', 'option_values', 'category_names', 'tags']+ %w[name description sku option_values category_names tags]enddef filterable_attributes- attrs = %w[status in_stock store_ids currency_codes discontinue_on category_ids tags]-- # Price fields per currency: price_USD, price_EUR, etc.- currencies = store.supported_currencies_list.map(&:iso_code)- currencies.each { |c| attrs << "price_#{c}" }-- attrs << 'option_value_ids'-- attrs+ %w[product_id status in_stock store_ids locale currency discontinue_on price category_ids tags option_value_ids]enddef sortable_attributes- attrs = %w[created_at available_on units_sold_count]-- # Price fields per currency- currencies = store.supported_currencies_list.map(&:iso_code)- currencies.each { |c| attrs << "price_#{c}" }-- attrs+ %w[name price created_at available_on units_sold_count]enddef facet_attributes@@ -156,11 +146,12 @@ module Spreedef build_filters(filters)conditions = []- # Always scope to current store and active products with prices in currency.- # This mirrors the AR scope: store.products.active(currency)+ # Always scope to current store, locale, currency, active, not discontinued.+ # This mirrors the AR scope: store.products.active(currency) with localeconditions << "store_ids = '#{store.id}'"conditions << "status = 'active'"- conditions << "currency_codes = '#{currency}'"+ conditions << "locale = '#{locale}'"+ conditions << "currency = '#{currency}'"conditions << "(discontinue_on = 0 OR discontinue_on > #{Time.current.to_i})"filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)@@ -172,9 +163,9 @@ module Spreekey = key.to_scase keywhen 'price_gte'- conditions << "price_#{currency} >= #{value.to_f}"+ conditions << "price >= #{value.to_f}"when 'price_lte'- conditions << "price_#{currency} <= #{value.to_f}"+ conditions << "price <= #{value.to_f}"when 'in_stock'conditions << 'in_stock = true' if value.to_s != '0'when 'out_of_stock'@@ -198,13 +189,13 @@ module Spreecase sortwhen 'price'- ["price_#{currency}:asc"]+ ['price:asc']when '-price'- ["price_#{currency}:desc"]+ ['price:desc']when 'name'- ["name_#{I18n.locale}:asc"]+ ['name:asc']when '-name'- ["name_#{I18n.locale}:desc"]+ ['name:desc']when '-available_on'['available_on:desc']when 'available_on'@@ -219,9 +210,8 @@ module Spreefilters = []# Price range- price_key = "price_#{currency}"- if facet_distribution[price_key].present?- amounts = facet_distribution[price_key].keys.map(&:to_f)+ if facet_distribution['price'].present?+ amounts = facet_distribution['price'].keys.map(&:to_f)filters << {id: 'price',type: 'price_range',@@ -307,6 +297,10 @@ module SpreePagy::MeilisearchPaginator.paginate(fake_result, {})end+ def locale+ Spree::Current.locale || store.default_locale || I18n.locale.to_s+ end+def currencySpree::Current.currency || store.default_currencyenddiff --git a/spree/core/app/presenters/spree/search_provider/product_presenter.rb b/spree/core/app/presenters/spree/search_provider/product_presenter.rbindex dd21676086..8c86474093 100644--- a/spree/core/app/presenters/spree/search_provider/product_presenter.rb+++ b/spree/core/app/presenters/spree/search_provider/product_presenter.rb@@ -8,17 +8,49 @@ module Spree@store = storeend+ # Returns an array of documents — one per market × locale combination.+ # Each document has flat name, description, price fields (no dynamic suffixes).def call+ documents = []++ market_locale_pairs.each do |market, locale|+ Mobility.with_locale(locale) do+ documents << build_document(locale, market.currency)+ end+ end++ # Fallback: if no markets, index with store defaults+ if documents.empty?+ documents << build_document(store.default_locale || I18n.default_locale.to_s, store.default_currency)+ end++ documents+ end++ private++ def build_document(locale, currency)+ price = lowest_price(currency)+{- prefixed_id: product.prefixed_id,+ # Composite ID: product + locale + currency+ prefixed_id: "#{product.prefixed_id}_#{locale}_#{currency}",+ product_id: product.prefixed_id,+ locale: locale.to_s,+ currency: currency,+ # Translated fields (Mobility resolves via current locale)name: product.name,description: product.description,slug: product.slug,+ # Price in this currency+ price: price&.to_f,+ compare_at_price: compare_at_price(currency)&.to_f,+ # Non-locale/currency fieldsstatus: product.status,sku: product.sku,in_stock: product.in_stock?,store_ids: product.store_ids.map(&:to_s),- currency_codes: product.prices_including_master.base_prices.where.not(amount: nil).pluck(:currency).uniq,+ discontinue_on: product.discontinue_on&.to_i || 0,category_ids: product.taxons.map(&:prefixed_id),category_names: product.taxons.pluck(:name),option_type_ids: product.option_types.map(&:prefixed_id),@@ -28,73 +60,37 @@ module Spreetags: product.tag_list || [],thumbnail_url: product.primary_media&.url(:large),units_sold_count: product.store_products.find_by(store: store)&.units_sold_count || 0,- discontinue_on: product.discontinue_on&.to_i || 0,available_on: product.available_on&.iso8601,created_at: product.created_at&.iso8601,- updated_at: product.updated_at&.iso8601,- # Multi-currency prices- **prices_by_currency,- # Multi-language translations- **translations_by_locale+ updated_at: product.updated_at&.iso8601}end- private-- # Indexes base prices (no price list overrides) for all currencies:- # { price_USD: 19.99, price_EUR: 17.50, compare_at_price_USD: 24.99 }- def prices_by_currency- result = {}-- product.prices_including_master.base_prices.where.not(amount: nil).each do |price|- currency = price.currency- amount = price.amount.to_f-- # Keep the lowest price per currency across all variants- key = "price_#{currency}"- if result[key].nil? || amount < result[key]- result[key] = amount- end-- if price.compare_at_amount.present?- result["compare_at_price_#{currency}"] = price.compare_at_amount.to_f- end+ # Returns all market × locale pairs for this store+ def market_locale_pairs+ @market_locale_pairs ||= store.markets.flat_map do |market|+ market.supported_locales_list.map { |locale| [market, locale] }end-- resultend- # Indexes translations for all locales: { name_en: "Shirt", name_fr: "Chemise", description_en: "...", ... }- def translations_by_locale- result = {}-- if product.respond_to?(:translations)- product.translations.each do |translation|- locale = translation.locale- result["name_#{locale}"] = translation.name if translation.name.present?- result["description_#{locale}"] = translation.description if translation.description.present?- result["slug_#{locale}"] = translation.slug if translation.slug.present?- end- end-- # Ensure current locale is always indexed- result["name_#{I18n.locale}"] ||= product.name- result["description_#{I18n.locale}"] ||= product.description- result["slug_#{I18n.locale}"] ||= product.slug+ def lowest_price(currency)+ product.price_in(currency)&.amount+ end- result+ def compare_at_price(currency)+ product.compare_at_amount_in(currency)enddef variant_option_value_ids- variant_option_values_data.map { |ov| ov.prefixed_id }.uniq+ variant_option_values_data.map(&:prefixed_id).uniqenddef variant_option_value_presentations- variant_option_values_data.map { |ov| ov.presentation }.uniq+ variant_option_values_data.map(&:presentation).uniqenddef variant_option_values_data- @variant_option_values_data ||= product.variants.includes(:option_values).flat_map { |v| v.option_values }+ @variant_option_values_data ||= product.variants.includes(:option_values).flat_map(&:option_values)endendenddiff --git a/spree/core/lib/tasks/search.rake b/spree/core/lib/tasks/search.rakeindex 12167fe26a..42ae5e6995 100644--- a/spree/core/lib/tasks/search.rake+++ b/spree/core/lib/tasks/search.rake@@ -16,7 +16,7 @@ namespace :spree do.preload(:taxons, :option_types, :primary_media,variants_including_master: [:prices, :option_values]).find_in_batches(batch_size: 500) do |batch|- documents = batch.map { |product| Spree::SearchProvider::ProductPresenter.new(product, store).call }+ documents = batch.flat_map { |product| Spree::SearchProvider::ProductPresenter.new(product, store).call }provider.index_batch(documents) if provider.respond_to?(:index_batch)indexed += batch.sizeprint "\r #{indexed}/#{total} products indexed"diff --git a/spree/core/spec/models/concerns/spree/search_indexable_spec.rb b/spree/core/spec/models/concerns/spree/search_indexable_spec.rbindex d6a0943dc6..89a5fe6424 100644--- a/spree/core/spec/models/concerns/spree/search_indexable_spec.rb+++ b/spree/core/spec/models/concerns/spree/search_indexable_spec.rb@@ -7,11 +7,11 @@ RSpec.describe Spree::SearchIndexable, type: :concern dolet(:product) { create(:product, stores: [store]) }describe '#search_presentation' do- it 'returns the presenter hash' do+ it 'returns an array of document hashes (one per market × locale)' doresult = product.search_presentation(store)- expect(result).to be_a(Hash)- expect(result[:prefixed_id]).to eq(product.prefixed_id)- expect(result[:name]).to eq(product.name)+ expect(result).to be_an(Array)+ expect(result.first[:product_id]).to eq(product.prefixed_id)+ expect(result.first[:name]).to eq(product.name)endenddiff --git a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rbindex 3eb78307ee..bdbfa702f0 100644--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb@@ -20,13 +20,13 @@ module Spreelet(:ms_response) do{'hits' => [- { 'prefixed_id' => product_1.prefixed_id },- { 'prefixed_id' => product_2.prefixed_id }+ { 'product_id' => product_1.prefixed_id },+ { 'product_id' => product_2.prefixed_id }],'estimatedTotalHits' => 2,'facetDistribution' => {'in_stock' => { 'true' => 2 },- "price_#{store.default_currency}" => { '19.99' => 1, '29.99' => 1 }+ 'price' => { '19.99' => 1, '29.99' => 1 }}}end@@ -84,7 +84,7 @@ module Spreecontext 'with price filter' doit 'passes price filter to Meilisearch' doexpect(mock_index).to receive(:search).with(anything, hash_including(- filter: include("price_#{store.default_currency} >= 10.0")+ filter: include('price >= 10.0'))).and_return(ms_response)provider.search_and_filter(scope: store.products, filters: { 'price_gte' => '10' })@@ -104,7 +104,7 @@ module Spreecontext 'with sort' doit 'passes price sort to Meilisearch' doexpect(mock_index).to receive(:search).with(anything, hash_including(- sort: ["price_#{store.default_currency}:asc"]+ sort: ['price:asc'])).and_return(ms_response)provider.search_and_filter(scope: store.products, sort: 'price')@@ -112,7 +112,7 @@ module Spreeit 'passes descending price sort' doexpect(mock_index).to receive(:search).with(anything, hash_including(- sort: ["price_#{store.default_currency}:desc"]+ sort: ['price:desc'])).and_return(ms_response)provider.search_and_filter(scope: store.products, sort: '-price')@@ -140,38 +140,39 @@ module Spreeenddescribe '#index' do- it 'adds document to Meilisearch index with prefixed_id as primary key' do+ it 'adds documents to Meilisearch index with prefixed_id as primary key' doexpect(mock_index).to receive(:add_documents).with(- [hash_including(prefixed_id: product_1.prefixed_id, name: 'Blue Shirt')],+ array_including(hash_including(product_id: product_1.prefixed_id, name: 'Blue Shirt')),'prefixed_id')provider.index(product_1)endit 'uses ProductPresenter to serialize' do- presenter = instance_double(SearchProvider::ProductPresenter, call: { prefixed_id: 'prod_abc' })+ docs = [{ prefixed_id: 'prod_abc_en_USD', product_id: 'prod_abc' }]+ presenter = instance_double(SearchProvider::ProductPresenter, call: docs)allow(SearchProvider::ProductPresenter).to receive(:new).with(product_1, store).and_return(presenter)- expect(mock_index).to receive(:add_documents).with([{ prefixed_id: 'prod_abc' }], 'prefixed_id')+ expect(mock_index).to receive(:add_documents).with(docs, 'prefixed_id')provider.index(product_1)endenddescribe '#remove' do- it 'deletes document by prefixed_id from Meilisearch index' do- expect(mock_index).to receive(:delete_document).with(product_1.prefixed_id)+ it 'deletes all locale/currency variants by product_id filter' do+ expect(mock_index).to receive(:delete_documents).with(filter: "product_id = '#{product_1.prefixed_id}'")provider.remove(product_1)endenddescribe '#remove_by_id' do- it 'deletes document by prefixed_id' do- expect(mock_index).to receive(:delete_document).with('prod_abc')+ it 'deletes all documents by product_id filter' do+ expect(mock_index).to receive(:delete_documents).with(filter: "product_id = 'prod_abc'")provider.remove_by_id('prod_abc')endit 'ignores 404 errors' doerror = ::Meilisearch::ApiError.new(404, 'not found', 'document_not_found')- allow(mock_index).to receive(:delete_document).and_raise(error)+ allow(mock_index).to receive(:delete_documents).and_raise(error)expect { provider.remove_by_id('prod_abc') }.not_to raise_errorendenddiff --git a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rbindex b1f4910ff1..bb2cb6de1b 100644--- a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb+++ b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb@@ -7,32 +7,52 @@ module Spreelet(:presenter) { described_class.new(product, store) }describe '#call' do- subject(:result) { presenter.call }+ subject(:documents) { presenter.call }- it 'returns a hash' do- expect(result).to be_a(Hash)+ it 'returns an array of documents' do+ expect(documents).to be_an(Array)+ expect(documents).not_to be_emptyend- it 'includes core product attributes' do- expect(result[:prefixed_id]).to eq(product.prefixed_id)- expect(result[:name]).to eq('Test Shirt')- expect(result[:slug]).to eq(product.slug)- expect(result[:status]).to eq(product.status)- expect(result[:sku]).to eq(product.sku)- end+ context 'document structure' do+ subject(:doc) { documents.first }- it 'includes timestamps' do- expect(result[:created_at]).to be_present- expect(result[:updated_at]).to be_present- end+ it 'includes composite prefixed_id and product_id' do+ expect(doc[:prefixed_id]).to start_with(product.prefixed_id)+ expect(doc[:product_id]).to eq(product.prefixed_id)+ end- it 'includes stock status' do- expect(result).to have_key(:in_stock)- end+ it 'includes locale and currency' do+ expect(doc[:locale]).to be_present+ expect(doc[:currency]).to be_present+ end++ it 'includes flat name and description' do+ expect(doc[:name]).to eq('Test Shirt')+ expect(doc[:slug]).to eq(product.slug)+ end++ it 'includes flat price' do+ expect(doc).to have_key(:price)+ end++ it 'includes stock status' do+ expect(doc).to have_key(:in_stock)+ end++ it 'includes timestamps' do+ expect(doc[:created_at]).to be_present+ expect(doc[:updated_at]).to be_present+ end++ it 'includes category data as prefixed IDs' do+ expect(doc[:category_ids]).to be_an(Array)+ end- it 'includes category data' do- expect(result[:category_ids]).to be_an(Array)- expect(result[:category_names]).to be_an(Array)+ it 'includes option data as prefixed IDs' do+ expect(doc[:option_type_ids]).to be_an(Array)+ expect(doc[:option_value_ids]).to be_an(Array)+ endendcontext 'with categories' do@@ -41,41 +61,29 @@ module Spreelet(:product) { create(:product, name: 'Test Shirt', stores: [store], taxons: [taxon]) }it 'indexes category_ids as prefixed IDs' do- expect(result[:category_ids]).to eq([taxon.prefixed_id])- expect(result[:category_ids].first).to start_with('ctg_')+ doc = documents.first+ expect(doc[:category_ids]).to eq([taxon.prefixed_id])+ expect(doc[:category_ids].first).to start_with('ctg_')endend- it 'includes option data as prefixed IDs' do- expect(result[:option_type_ids]).to be_an(Array)- expect(result[:option_value_ids]).to be_an(Array)- end-- context 'with prices in multiple currencies' do- before do- product.master.prices.find_by(currency: 'USD')&.update!(amount: 19.99)- create(:price, variant: product.master, amount: 17.50, currency: 'EUR')- end-- it 'indexes prices for each currency' do- expect(result['price_USD']).to eq(19.99)- expect(result['price_EUR']).to eq(17.50)- end- end+ context 'with multiple markets' do+ let!(:us_market) { create(:market, store: store, name: 'US', currency: 'USD', default_locale: 'en') }+ let!(:eu_market) { create(:market, store: store, name: 'EU', currency: 'EUR', default_locale: 'de', supported_locales: 'de,fr') }- context 'with compare_at_price' do- before do- product.master.prices.find_by(currency: 'USD')&.update!(amount: 19.99, compare_at_amount: 29.99)- end+ # Reload store to pick up new markets+ let(:presenter) { described_class.new(product, store.reload) }- it 'indexes compare_at_price' do- expect(result['compare_at_price_USD']).to eq(29.99)+ it 'creates documents for each market × locale combination' do+ locales_currencies = documents.map { |d| [d[:locale], d[:currency]] }+ expect(locales_currencies).to include(['en', 'USD'])+ expect(locales_currencies).to include(['de', 'EUR'])+ expect(locales_currencies).to include(['fr', 'EUR'])end- end- context 'with translations' do- it 'indexes translations for the current locale' do- expect(result["name_#{I18n.locale}"]).to eq('Test Shirt')+ it 'each document has a unique composite prefixed_id' do+ ids = documents.map { |d| d[:prefixed_id] }+ expect(ids.uniq.size).to eq(ids.size)endend@@ -83,8 +91,8 @@ module Spreelet(:product) { create(:product_with_option_types, stores: [store]) }it 'includes option value prefixed IDs' do- expect(result[:option_value_ids]).to be_an(Array)- result[:option_value_ids].each do |ov_id|+ doc = documents.first+ doc[:option_value_ids].each do |ov_id|expect(ov_id).to start_with('optval_')endend