Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect the position per category #382

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading