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..31803e170 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 */