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: preserving meilisearch sort order when rendering product list … (#13856)

Damian Legawiec7f37c2c5315c
diff --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.rb
index bd03e46bd9..c53422a984 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
@@ -544,6 +544,20 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
filter: include("status = 'active'")
))
end
+
+ it 'preserves Meilisearch sort order in the response' do
+ allow(mock_index).to receive(:search).and_return({
+ 'hits' => [{ 'product_id' => product2.prefixed_id }, { 'product_id' => product.prefixed_id }],
+ 'estimatedTotalHits' => 2,
+ 'facetDistribution' => {}
+ })
+
+ get :index, params: { sort: '-price' }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to eq([product2.prefixed_id, product.prefixed_id])
+ end
end
end
end
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index cc37326bbb..b1f607ce02 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -103,9 +103,10 @@ module Spree
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.
+ # Intersect with AR scope for security/visibility, preserving Meilisearch sort order.
products = if raw_ids.any?
- scope.where(id: raw_ids).reorder(nil)
+ records = scope.where(id: raw_ids).reorder(nil).index_by(&:id)
+ raw_ids.filter_map { |id| records[id] }
else
scope.none
end
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 f76472f57c..ff74197695 100644
--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
@@ -129,6 +129,23 @@ module Spree
end
end
+ context 'with sort order preservation' do
+ it 'returns products in the order Meilisearch returned them' do
+ reversed_response = {
+ 'hits' => [
+ { 'product_id' => product_2.prefixed_id },
+ { 'product_id' => product_1.prefixed_id }
+ ],
+ 'estimatedTotalHits' => 2,
+ 'facetDistribution' => {}
+ }
+ allow(mock_index).to receive(:search).and_return(reversed_response)
+
+ result = provider.search_and_filter(scope: store.products, query: 'shirt')
+ expect(result.products).to eq([product_2, product_1])
+ end
+ end
+
context 'with visibility scope' do
it 'intersects Meilisearch results with AR scope' do
restricted_scope = store.products.where(id: product_1.id)