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.

Fixed `in_category` filter in Meilisearch provider (#13842)

Damian Legawiec33dcbb81a075
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index 4b43a325cd..8e691acb7f 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -201,7 +201,8 @@ module Spree
when 'in_category'
"category_ids = '#{sanitize_prefixed_id(value)}'" if valid_prefixed_id?(value)
when 'in_categories'
- Array(value).filter_map { |id| "category_ids = '#{sanitize_prefixed_id(id)}'" if valid_prefixed_id?(id) }
+ parts = Array(value).filter_map { |id| "category_ids = '#{sanitize_prefixed_id(id)}'" if valid_prefixed_id?(id) }
+ parts.length > 1 ? "(#{parts.join(' OR ')})" : parts.first
when 'with_option_value_ids'
Array(value).filter_map { |ov| "option_value_ids = '#{sanitize_prefixed_id(ov)}'" if valid_prefixed_id?(ov) }
end
diff --git a/spree/core/app/presenters/spree/search_provider/product_presenter.rb b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
index ffbe2be8c9..8349fe74d0 100644
--- a/spree/core/app/presenters/spree/search_provider/product_presenter.rb
+++ b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
@@ -55,7 +55,7 @@ module Spree
in_stock: product.in_stock?,
store_ids: cached_store_ids,
discontinue_on: product.discontinue_on&.to_i || 0,
- category_ids: product.taxons.map(&:prefixed_id),
+ category_ids: category_ids_with_ancestors,
category_names: product.taxons.map { |t| translated(t, :name, fallback_locale) },
option_type_ids: product.option_types.map(&:prefixed_id),
option_type_names: product.option_types.map { |ot| translated(ot, :presentation, fallback_locale) },
@@ -103,6 +103,14 @@ module Spree
@compare_at_cache[currency]
end
+ # Include ancestor category IDs so filtering by a parent category
+ # matches products classified under its descendants.
+ def category_ids_with_ancestors
+ @category_ids_with_ancestors ||= product.taxons.flat_map { |t|
+ t.self_and_ancestors.map(&:prefixed_id)
+ }.uniq
+ end
+
# Memoized — avoids N+1 when called per document
def cached_store_ids
@cached_store_ids ||= product.store_ids.map(&:to_s)
diff --git a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
index 9a7dff3b72..2a57bda0c7 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
@@ -60,10 +60,25 @@ module Spree
let(:taxon) { create(:taxon, taxonomy: taxonomy) }
let(:product) { create(:product, name: 'Test Shirt', stores: [store], taxons: [taxon]) }
- it 'indexes category_ids as prefixed IDs' do
+ it 'indexes category_ids as prefixed IDs including ancestors' do
doc = documents.first
- expect(doc[:category_ids]).to eq([taxon.prefixed_id])
- expect(doc[:category_ids].first).to start_with('ctg_')
+ expect(doc[:category_ids]).to include(taxon.prefixed_id)
+ expect(doc[:category_ids]).to include(taxonomy.root.prefixed_id)
+ doc[:category_ids].each { |id| expect(id).to start_with('ctg_') }
+ end
+ end
+
+ context 'with nested categories' do
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let(:parent) { create(:taxon, taxonomy: taxonomy, name: 'Men') }
+ let(:child) { create(:taxon, taxonomy: taxonomy, parent: parent, name: 'Jackets') }
+ let(:product) { create(:product, name: 'Test Jacket', stores: [store], taxons: [child]) }
+
+ it 'indexes all ancestor category IDs so parent-level filtering works' do
+ doc = documents.first
+ expect(doc[:category_ids]).to include(child.prefixed_id)
+ expect(doc[:category_ids]).to include(parent.prefixed_id)
+ expect(doc[:category_ids]).to include(taxonomy.root.prefixed_id)
end
end