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
Small batch of meilisearch/search provider improvements (#13858)
Damian Legawiecca9631e89196
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rbindex b1f607ce02..ead65a396e 100644--- a/spree/core/app/models/spree/search_provider/meilisearch.rb+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb@@ -133,6 +133,8 @@ module Spreeenddef index_batch(documents)+ return if documents.empty?+client.index(index_name).add_documents(documents, 'id')end@@ -148,21 +150,34 @@ module Spreescope ||= store.productsensure_index_settings!+ indexed = 0scope.reorder(id: :asc).preload_associations_lazily.find_in_batches(batch_size: 500) do |batch|documents = batch.flat_map { |product| presenter_class.new(product, store).call }+ next if documents.empty?+index_batch(documents)+ indexed += documents.size++ Rails.logger.info { "[Meilisearch] Enqueued #{documents.size} documents (#{indexed} total) for #{index_name}" }end++ Rails.logger.info { "[Meilisearch] Reindex complete: #{indexed} documents enqueued for #{index_name}" }+ indexedend# Configure index settings for filtering, sorting, and faceting.# Called automatically by reindex, but can be called separately.+ # Waits for all settings tasks to complete before returning so that+ # subsequent add_documents calls use the correct filterable/sortable attributes.def ensure_index_settings!index = client.index(index_name)- index.update_filterable_attributes(filterable_attributes)- index.update_sortable_attributes(sortable_attributes)- index.update_searchable_attributes(searchable_attributes)+ tasks = []+ tasks << index.update_filterable_attributes(filterable_attributes)+ tasks << index.update_sortable_attributes(sortable_attributes)+ tasks << index.update_searchable_attributes(searchable_attributes)+ tasks.each { |task| task&.await }endprivatediff --git a/spree/core/app/presenters/spree/search_provider/product_presenter.rb b/spree/core/app/presenters/spree/search_provider/product_presenter.rbindex ae9f8e3e07..7b9b52bd63 100644--- a/spree/core/app/presenters/spree/search_provider/product_presenter.rb+++ b/spree/core/app/presenters/spree/search_provider/product_presenter.rb@@ -35,9 +35,14 @@ module Spreeprivate+ # Build a document for a given locale and currency+ # @param locale [String] the locale to build the document for+ # @param currency [String] the currency to build the document for+ # @param fallback_locale [String] the fallback locale to use if the product has no translation for the given locale+ # @return [Hash] the documentdef build_document(locale, currency, fallback_locale){- # Composite ID: product + locale + currency (Meilisearch primary key)+ # Composite ID: product + locale + currencyid: "#{product.prefixed_id}_#{locale}_#{currency}",product_id: product.prefixed_id,locale: locale.to_s,@@ -53,7 +58,7 @@ module Spreestatus: product.status,sku: product.sku,in_stock: product.in_stock?,- store_ids: cached_store_ids,+ store_ids: product.store_ids.map(&:to_s),discontinue_on: product.discontinue_on&.to_i || 0,category_ids: category_ids_with_ancestors,category_names: product.taxons.map { |t| translated(t, :name, fallback_locale) },@@ -62,8 +67,7 @@ module Spreeoption_value_ids: variant_option_value_ids,option_values: variant_option_values_data.map { |ov| translated(ov, :presentation, fallback_locale) }.uniq,tags: product.tag_list || [],- thumbnail_url: product.primary_media&.url(:large),- units_sold_count: cached_units_sold_count,+ units_sold_count: product.store_products.find { |sp| sp.store_id == store.id }&.units_sold_count || 0,available_on: product.available_on&.iso8601,created_at: product.created_at&.iso8601,updated_at: product.updated_at&.iso8601@@ -111,15 +115,6 @@ module Spree}.uniqend- # Memoized — avoids N+1 when called per document- def cached_store_ids- @cached_store_ids ||= product.store_ids.map(&:to_s)- end-- def cached_units_sold_count- @cached_units_sold_count ||= product.store_products.detect { |sp| sp.store_id == store.id }&.units_sold_count || 0- end-def variant_option_value_idsvariant_option_values_data.map(&:prefixed_id).uniqenddiff --git a/spree/core/lib/tasks/search.rake b/spree/core/lib/tasks/search.rakeindex e666e91223..58595a637f 100644--- a/spree/core/lib/tasks/search.rake+++ b/spree/core/lib/tasks/search.rake@@ -6,9 +6,9 @@ namespace :spree doprovider = Spree.search_provider.constantize.new(store)total = store.products.count- puts "Reindexing #{store.name} (#{total} products)..."- provider.reindex(store.products)- puts "Done."+ puts "Reindexing #{store.name} (#{total} products) using #{Spree.search_provider}..."+ indexed = provider.reindex(store.products.preload_associations_lazily)+ puts "Done. #{indexed || total} documents enqueued."endendend