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.

Ensure index exists before indexing the first product (#13862)

Damian Legawiec5b6ab8aa59f4
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index ead65a396e..be5c4a2d92 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -124,6 +124,7 @@ module Spree
end
def index(product)
+ ensure_index_settings_once!
documents = presenter_class.new(product, store).call
client.index(index_name).add_documents(documents, 'id')
end
@@ -178,6 +179,16 @@ module Spree
tasks << index.update_sortable_attributes(sortable_attributes)
tasks << index.update_searchable_attributes(searchable_attributes)
tasks.each { |task| task&.await }
+ @index_settings_configured = true
+ end
+
+ # Lightweight guard — configures index settings once per provider instance.
+ # Meilisearch settings updates are idempotent, so repeated calls are safe
+ # but we avoid the overhead by memoizing.
+ def ensure_index_settings_once!
+ return if @index_settings_configured
+
+ ensure_index_settings!
end
private
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 ff74197695..73567a60be 100644
--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
@@ -14,6 +14,9 @@ module Spree
before do
allow(::Meilisearch::Client).to receive(:new).and_return(mock_client)
allow(mock_client).to receive(:index).and_return(mock_index)
+ allow(mock_index).to receive(:update_filterable_attributes)
+ allow(mock_index).to receive(:update_sortable_attributes)
+ allow(mock_index).to receive(:update_searchable_attributes)
end
describe '#search_and_filter' do
@@ -195,12 +198,6 @@ module Spree
end
describe '#reindex' do
- before do
- allow(mock_index).to receive(:update_filterable_attributes)
- allow(mock_index).to receive(:update_sortable_attributes)
- allow(mock_index).to receive(:update_searchable_attributes)
- end
-
it 'configures index settings' do
expect(mock_index).to receive(:update_filterable_attributes)
expect(mock_index).to receive(:update_sortable_attributes)