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
Fix: Kept admin-only custom fields out of the Meilisearch index
Damian Legawiece7c603ecda1e
diff --git a/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb b/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rbindex 273c7104b6..5d8ab19610 100644--- a/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb+++ b/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb@@ -101,7 +101,7 @@ module SpreeMeilisearchdef reindex(scope = nil)full_reindex = scope.nil?scope ||= store.products- @custom_field_schema = Spree::SearchProvider::CustomFieldSchema.new(store, audience: audience)+ @custom_field_schema = storefront_custom_field_schemaensure_index_settings!# On a full reindex, clear first so stale docs (e.g. membership docs for@@ -285,6 +285,18 @@ module SpreeMeilisearch"#{store.code}_products"end+ # The index is the storefront's: shoppers search it and the back office+ # never does, so its vocabulary is the storefront schema whatever audience+ # this provider was built for. A back-office reindex must not teach the+ # shared index about admin-only fields.+ def custom_field_schema+ @custom_field_schema ||= storefront_custom_field_schema+ end++ def storefront_custom_field_schema+ Spree::SearchProvider::CustomFieldSchema.new(store, audience: :store)+ end+def searchable_attributes%w[name description sku option_values category_names tags] + custom_field_schema.searchable_attribute_keysenddiff --git a/spree/providers/meilisearch/app/presenters/spree_meilisearch/product_presenter.rb b/spree/providers/meilisearch/app/presenters/spree_meilisearch/product_presenter.rbindex d5cb93333b..e8d9ed4862 100644--- a/spree/providers/meilisearch/app/presenters/spree_meilisearch/product_presenter.rb+++ b/spree/providers/meilisearch/app/presenters/spree_meilisearch/product_presenter.rb@@ -93,10 +93,12 @@ module SpreeMeilisearchend# Flat cf_* hash for searchable ∪ sortable custom_fields (Meilisearch docs).+ # Storefront-visible ones only: the index is the storefront's, and a value+ # the serializer withholds has no business in a document it can search.def custom_field_document_attributes@custom_field_document_attributes ||= product.custom_fields.filter_map do |custom_field|definition = custom_field.custom_field_definition- next unless definition&.searchable? || definition&.sortable?+ next unless definition&.storefront_visible? && (definition.searchable? || definition.sortable?)[definition.filter_key, custom_field_index_value(custom_field, definition.field_type)]end.to_hdiff --git a/spree/providers/meilisearch/spec/models/spree_meilisearch/search_provider_spec.rb b/spree/providers/meilisearch/spec/models/spree_meilisearch/search_provider_spec.rbindex 3204902e6c..bbcf106576 100644--- a/spree/providers/meilisearch/spec/models/spree_meilisearch/search_provider_spec.rb+++ b/spree/providers/meilisearch/spec/models/spree_meilisearch/search_provider_spec.rb@@ -518,6 +518,16 @@ RSpec.describe SpreeMeilisearch::SearchProvider doexpect(provider.send(:facet_attributes)).not_to include('cf_custom_label')end+ it 'keeps admin-only definitions out of the index settings, whatever audience built the provider' do+ create(:custom_field_definition, :number_field, :admin_only, :searchable, :sortable,+ namespace: 'internal', key: 'margin')+ back_office = described_class.new(store, audience: nil)++ expect(back_office.send(:searchable_attributes)).not_to include('cf_internal_margin')+ expect(back_office.send(:sortable_attributes)).not_to include('cf_internal_margin')+ expect(back_office.send(:filterable_attributes)).not_to include('cf_internal_margin')+ end+it 'maps cf_* sort params without namespace resolution' doexpect(provider.send(:sort_mapping, 'cf_custom_label')).to eq(['cf_custom_label:asc'])expect(provider.send(:sort_mapping, '-cf_custom_label')).to eq(['cf_custom_label:desc'])diff --git a/spree/providers/meilisearch/spec/presenters/spree_meilisearch/product_presenter_spec.rb b/spree/providers/meilisearch/spec/presenters/spree_meilisearch/product_presenter_spec.rbindex 786027e527..76b3291383 100644--- a/spree/providers/meilisearch/spec/presenters/spree_meilisearch/product_presenter_spec.rb+++ b/spree/providers/meilisearch/spec/presenters/spree_meilisearch/product_presenter_spec.rb@@ -54,6 +54,23 @@ RSpec.describe SpreeMeilisearch::ProductPresenter doendend+ context 'with custom fields' do+ let(:visible) { create(:custom_field_definition, :short_text_field, :searchable, namespace: 'custom', key: 'label') }+ let(:hidden) { create(:custom_field_definition, :number_field, :admin_only, :sortable, namespace: 'internal', key: 'margin') }++ before do+ product.set_custom_field(visible, 'wool')+ product.set_custom_field(hidden, '40')+ end++ it 'indexes storefront-visible values and leaves admin-only ones out' do+ doc = documents.first++ expect(doc['cf_custom_label']).to eq('wool')+ expect(doc).not_to have_key('cf_internal_margin')+ end+ end+context 'with categories' dolet(:root_category) { create(:category, store: store, name: 'Apparel') }let(:category) { create(:category, store: store, parent: root_category) }