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

[Instantsearch] Scout indexing products & categories #734

Merged
merged 10 commits into from
Feb 18, 2025
6 changes: 6 additions & 0 deletions config/rapidez/indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
// VISIBILITY_IN_SEARCH = 3
// VISIBILITY_BOTH = 4
'visibility' => [2, 3, 4],

// The Rapidez indexer will automatically start indexing any models with the Searchable trait that exist within `models.php`.
// If you want to add extra models you can add them here. Only Eloquent models are supported.
'extra_models' => [
// 'book' => \App\Models\Book::class,
],
];
6 changes: 4 additions & 2 deletions resources/views/components/listing.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
label: window.config.translations.newest,
field: 'created_at',
order: 'desc',
value: config.index+'_created_at_desc',
{{-- TODO: Extract this somewhere? --}}
value: config.index_prefix + '_products_' + config.store + '_created_at_desc',
key: '_created_at_desc'
}]"
v-cloak
Expand All @@ -23,7 +24,8 @@
<ais-instant-search
v-if="loaded"
:search-client="listingSlotProps.searchClient"
:index-name="config.index"
{{-- TODO: Extract this somewhere? --}}
:index-name="config.index_prefix + '_products_' + config.store"
:routing="listingSlotProps.routing"
>
<ais-configure :filters="{!! $query !!}"/>
Expand Down
62 changes: 0 additions & 62 deletions src/Commands/ElasticsearchIndexCommand.php

This file was deleted.

151 changes: 0 additions & 151 deletions src/Commands/ElasticsearchIndexer.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/Commands/IndexCategoriesCommand.php

This file was deleted.

51 changes: 51 additions & 0 deletions src/Commands/IndexCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rapidez\Core\Commands;

use Illuminate\Console\Command;
use Rapidez\Core\Events\IndexAfterEvent;
use Rapidez\Core\Events\IndexBeforeEvent;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Core\Models\Traits\Searchable;

class IndexCommand extends Command
{
protected $signature = 'rapidez:index {--t|types= : To specify types of models to index, separated by commas} {--s|store= : To specify store IDs from Magento, separated by commas}';

protected $description = 'Index all searchable models into ElasticSearch';

public function handle()
{
$baseSearchableModels = collect(config('rapidez.models'))
->filter(fn ($class) => in_array(Searchable::class, class_uses_recursive($class)))
->merge(config('rapidez.indexer.extra_models'));

$types = $this->option('types')
? $baseSearchableModels->only(explode(',', $this->option('types')))
: $baseSearchableModels;

$stores = $this->option('store')
? Rapidez::getStores(explode(',', $this->option('store')))
: Rapidez::getStores();

$this->call('cache:clear');

IndexBeforeEvent::dispatch($this);

foreach ($stores as $store) {
Rapidez::setStore($store);

$this->line('Store: ' . $store['name']);

foreach ($types as $type => $model) {
config()->set('rapidez.index', $type);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to infer this from the model itself? It would save us from setting this value which thanks to your changes doesn't seem needed anymore.

Perhaps a function that reads from an empty model variable/constant, which if it's not set Str::snake() on the class name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's also an option; currently we do this differently on multiple places, for example:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to infer this from the model itself? It would save us from setting this value which thanks to your changes doesn't seem needed anymore.

Perhaps a function that reads from an empty model variable/constant, which if it's not set Str::snake() on the class name

I had done this originally, but figured we might want to keep flexibility up for now, and it might not work properly if you overwrite a model with a different class name (e.g. if your code standards require you to call your extended version of a product model ProductExt or whatever).

There's also the thing where it checks if you tried to use Scout directly. Although we'd probably be better off using a different solution for that eventually.


$this->call('scout:import', [
'searchable' => $model,
]);
}
}

IndexAfterEvent::dispatch($this);
}
}
Loading
Loading