Skip to content

Commit

Permalink
Position per category support (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Nov 17, 2023
1 parent d896bcb commit fc3f26c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
22 changes: 21 additions & 1 deletion resources/views/components/listing.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="min-h-screen">
<listing
:additional-filters="{!! isset($query) ? "['query-filter', 'category']" : "['category']" !!}"
:additional-filters="{!! isset($query) ? "['query-filter', 'category', 'score-position']" : "['category', 'score-position']" !!}"
:additional-sorting="[{
label: window.config.translations.newest,
dataField: 'created_at',
Expand All @@ -27,6 +27,26 @@
:show-filter="false"
></reactive-component>
@endisset
<reactive-component
component-id="score-position"
:custom-query="function () {
if (!window.config.category?.entity_id) {
return;
}
return {
query: {
function_score: {
field_value_factor: {
field: 'positions.'+window.config.category.entity_id,
missing: 0
}
}
}
}
}"
:show-filter="false"
></reactive-component>

{{ $before ?? '' }}
@if ($slot->isEmpty())
Expand Down
18 changes: 15 additions & 3 deletions src/Commands/IndexProductsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rapidez\Core\Events\IndexBeforeEvent;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Core\Models\Category;
use Rapidez\Core\Models\CategoryProduct;
use TorMorten\Eventy\Facades\Eventy;

class IndexProductsCommand extends ElasticsearchIndexCommand
Expand Down Expand Up @@ -42,9 +43,15 @@ public function handle()
],
],
]), Eventy::filter('index.product.settings', []));

try {
$maxPositions = CategoryProduct::query()
->selectRaw('GREATEST(MAX(position), 0) as position')
->addSelect('category_id')
->groupBy('category_id')
->pluck('position', 'category_id');

$productQuery = $productModel::selectOnlyIndexable()
->with('categoryProducts')
->withEventyGlobalScopes('index.product.scopes')
->withExists('options AS has_options');

Expand All @@ -58,8 +65,8 @@ public function handle()
$showOutOfStock = (bool) Rapidez::config('cataloginventory/options/show_out_of_stock', 0);
$indexVisibility = config('rapidez.indexer.visibility');

$productQuery->chunk($this->chunkSize, function ($products) use ($store, $bar, $categories, $showOutOfStock, $indexVisibility) {
$this->indexer->index($products, function ($product) use ($store, $categories, $showOutOfStock, $indexVisibility) {
$productQuery->chunk($this->chunkSize, function ($products) use ($store, $bar, $categories, $showOutOfStock, $indexVisibility, $maxPositions) {
$this->indexer->index($products, function ($product) use ($store, $categories, $showOutOfStock, $indexVisibility, $maxPositions) {
if (! in_array($product->visibility, $indexVisibility)) {
return;
}
Expand All @@ -78,6 +85,11 @@ public function handle()

$data = $this->withCategories($data, $categories);

$data['positions'] = $product->categoryProducts
->pluck('position', 'category_id')
// Turn all positions positive
->mapWithKeys(fn ($position, $category_id) => [$category_id => $maxPositions[$category_id] - $position]);

return Eventy::filter('index.product.data', $data, $product);
});

Expand Down
9 changes: 9 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function options(): HasMany
);
}

public function categoryProducts(): HasMany
{
return $this
->hasMany(
config('rapidez.models.category_product'),
'product_id',
);
}

public function rewrites(): HasMany
{
return $this
Expand Down

0 comments on commit fc3f26c

Please sign in to comment.