From 7331ad6a96390e99f38e36b6e9d6e93ae56e1149 Mon Sep 17 00:00:00 2001 From: Jade Date: Fri, 23 Jan 2026 15:15:21 +0100 Subject: [PATCH 1/2] Get popular products from product views instead of reviews --- .../partials/popular-products.blade.php | 7 +++---- src/Models/Traits/Product/Searchable.php | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/resources/views/listing/partials/popular-products.blade.php b/resources/views/listing/partials/popular-products.blade.php index 3f10251ed..46bd79df3 100644 --- a/resources/views/listing/partials/popular-products.blade.php +++ b/resources/views/listing/partials/popular-products.blade.php @@ -3,10 +3,9 @@ v-if="intersected" v-bind:query="() => [{ function_score: { - script_score: { - script: { - source: `(doc['reviews_score'].empty ? 0 : doc['reviews_score'].value) * (1 - 1 / Math.max(1, doc['reviews_count'].empty ? 0 : doc['reviews_count'].value))`, - }, + field_value_factor: { + field: 'popularity', + missing: 0, }, }, }]" diff --git a/src/Models/Traits/Product/Searchable.php b/src/Models/Traits/Product/Searchable.php index f218a7fdd..e01cc7df3 100644 --- a/src/Models/Traits/Product/Searchable.php +++ b/src/Models/Traits/Product/Searchable.php @@ -77,9 +77,30 @@ public function toSearchableArray(): array // Turn all positions positive ->mapWithKeys(fn ($position, $category_id) => [$category_id => $maxPositions[$category_id] - $position]); + $data['popularity'] = $this->getPopularity(); + return Eventy::filter('index.' . static::getModelName() . '.data', $data, $this); } + public function getPopularity(): int + { + $popularityList = Cache::driver('array')->rememberForever('product-popularity-' . config('rapidez.store'), function() { + $views = config('rapidez.models.product_view')::query() + ->groupBy('product_id') + ->selectRaw('COUNT(*) as views') + ->addSelect('product_id') + ->pluck('views', 'product_id'); + + // Create sorted list of products ordered by popularity + $popularityOrder = $views->sort()->keys(); + + // Swap keys and values to get a list of popularity keyed by product + return $popularityOrder->flip(); + }); + + return $popularityList[$this->entity_id] ?? 0; + } + /** * Add the category paths */ From e4cc1bec9c8df4529070bbc8e6224e9b5b378161 Mon Sep 17 00:00:00 2001 From: Jade-GG Date: Fri, 23 Jan 2026 14:23:22 +0000 Subject: [PATCH 2/2] Apply fixes from Duster --- src/Models/Traits/Product/Searchable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Traits/Product/Searchable.php b/src/Models/Traits/Product/Searchable.php index e01cc7df3..31803e170 100644 --- a/src/Models/Traits/Product/Searchable.php +++ b/src/Models/Traits/Product/Searchable.php @@ -84,7 +84,7 @@ public function toSearchableArray(): array public function getPopularity(): int { - $popularityList = Cache::driver('array')->rememberForever('product-popularity-' . config('rapidez.store'), function() { + $popularityList = Cache::driver('array')->rememberForever('product-popularity-' . config('rapidez.store'), function () { $views = config('rapidez.models.product_view')::query() ->groupBy('product_id') ->selectRaw('COUNT(*) as views')