From 067532b6fc2de188e573a6dcdea9225370900c1e Mon Sep 17 00:00:00 2001 From: ludo Date: Wed, 31 Jan 2024 23:45:58 +0100 Subject: [PATCH] add new search engine --- app/Models/Shop/Article.php | 29 ++++++++++++++++++++++++++- app/Repositories/Shop/Searches.php | 6 ++++++ composer.json | 2 ++ config/scout.php | 8 ++++++++ resources/views/Shop/matomo.blade.php | 15 ++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 resources/views/Shop/matomo.blade.php diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index 72d28f64..7d628265 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -4,6 +4,7 @@ namespace App\Models\Shop; use App\Models\Botanic\Specie; use App\Models\Botanic\Variety; +use App\Repositories\Shop\Articles; use App\Traits\Model\HasComments; use App\Traits\Model\Imageable; use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; @@ -15,6 +16,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\SoftDeletes; use Kirschbaum\PowerJoins\PowerJoins; +use Laravel\Scout\Searchable; use Rinvex\Categories\Traits\Categorizable; use Rinvex\Tags\Traits\Taggable; use Spatie\MediaLibrary\HasMedia; @@ -31,6 +33,7 @@ class Article extends Model implements HasMedia use Imageable; use Powerjoins; use RevisionableTrait; + use Searchable; use SoftDeletes; use Taggable; use UserStamps; @@ -192,7 +195,31 @@ class Article extends Model implements HasMedia public function scopeWithAvailableOffers($query, $saleChannelId = false) { return $query->whereHas('offers', function ($query) use ($saleChannelId) { - $query->active()->byStockAvailable()->bySaleChannel($saleChannelId); + $query->active()->byStockAvailable(); + + if ($saleChannelId) { + $query->bySaleChannel($saleChannelId); + } }); } + + public function toSearchableArray(): array + { + $description = $this->description; + $product = $this->product ?? false; + + if (is_object($product)) { + $description .= ' ' . $product ? $product->description : ''; + $description .= ' ' . $product ? $product->plus ?? '' : ''; + + $specie = $product ? $product->specie : false; + $description .= ' ' . $specie ? $specie->description ?? '' : ''; + } + + return [ + 'id' => (int) $this->id, + 'name' => $this->name, + 'description' => html_entity_decode(strip_tags($description)), + ]; + } } diff --git a/app/Repositories/Shop/Searches.php b/app/Repositories/Shop/Searches.php index 6ce6c7b6..6dc2e7b1 100644 --- a/app/Repositories/Shop/Searches.php +++ b/app/Repositories/Shop/Searches.php @@ -2,8 +2,14 @@ namespace App\Repositories\Shop; +use App\Models\Shop\Article; + class Searches { + public static function search($query) + { + return Article::withAvailableOffers()->search($query)->get(); + } public static function getResults($options) { return Articles::getArticlesToSell($options); diff --git a/composer.json b/composer.json index 11a0df61..4bb2b873 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "gzero/eloquent-tree": "^3.1", "hassankhan/config": "^3.1", "htmlmin/htmlmin": "^9.0", + "http-interop/http-factory-guzzle": "^1.2", "intervention/image": "^2.7", "jasonlewis/expressive-date": "^1.0", "jenssegers/date": "^4.0", @@ -59,6 +60,7 @@ "league/period": "^5.3", "livewire/livewire": "^2.12", "maatwebsite/excel": "^3.1", + "meilisearch/meilisearch-php": "^1.6", "moneyphp/money": "^4.3", "mpdf/mpdf": "^8.2", "mpociot/teamwork": "^8.1", diff --git a/config/scout.php b/config/scout.php index eac37c5e..dff17852 100644 --- a/config/scout.php +++ b/config/scout.php @@ -1,5 +1,7 @@ [ 'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'), 'key' => env('MEILISEARCH_KEY', null), + 'index-settings' => [ + Article::class => [ + 'filterableAttributes'=> ['id', 'name', 'description'], + 'sortableAttributes' => ['created_at'], + ], + ], ], ]; diff --git a/resources/views/Shop/matomo.blade.php b/resources/views/Shop/matomo.blade.php new file mode 100644 index 00000000..62e00bd6 --- /dev/null +++ b/resources/views/Shop/matomo.blade.php @@ -0,0 +1,15 @@ + + + \ No newline at end of file