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

Additional Scout Indexers #1252

Merged
merged 7 commits into from
Sep 22, 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
9 changes: 6 additions & 3 deletions packages/core/config/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
'models' => [
// These models are required by the system, do not change them.
\Lunar\Models\Brand::class,
\Lunar\Models\Collection::class,
\Lunar\Models\Customer::class,
\Lunar\Models\Order::class,
\Lunar\Models\Product::class,
\Lunar\Models\ProductOption::class,
\Lunar\Models\Order::class,
\Lunar\Models\Customer::class,
\Lunar\Models\Brand::class,
// Below you can add your own models for indexing
],
/*
Expand All @@ -38,7 +38,10 @@

'indexers' => [
\Lunar\Models\Brand::class => \Lunar\Search\BrandIndexer::class,
\Lunar\Models\Collection::class => \Lunar\Search\CollectionIndexer::class,
\Lunar\Models\Customer::class => \Lunar\Search\CustomerIndexer::class,
\Lunar\Models\Order::class => \Lunar\Search\OrderIndexer::class,
\Lunar\Models\Product::class => \Lunar\Search\ProductIndexer::class,
\Lunar\Models\ProductOption::class => \Lunar\Search\ProductOptionIndexer::class,
],
];
16 changes: 0 additions & 16 deletions packages/core/src/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ class Collection extends BaseModel implements SpatieHasMedia
NodeTrait::usesSoftDelete insteadof Searchable;
}

/**
* Define our base filterable attributes.
*
* @var array
*/
protected $filterable = [];

/**
* Define our base sortable attributes.
*
* @var array
*/
protected $sortable = [
'name',
];

/**
* Define which attributes should be cast.
*
Expand Down
91 changes: 0 additions & 91 deletions packages/core/src/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,6 @@ class Order extends BaseModel
LogsActivity,
Searchable;

/**
* Define our base filterable attributes.
*
* @var array
*/
protected $filterable = [
'__soft_deleted',
'status',
'created_at',
'placed_at',
'tags',
];

/**
* Define our base sortable attributes.
*
* @var array
*/
protected $sortable = [
'created_at',
'placed_at',
'total',
];

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -102,16 +78,6 @@ protected static function newFactory(): OrderFactory
return OrderFactory::new();
}

/**
* Get the name of the index associated with the model.
*
* @return string
*/
public function searchableAs()
{
return config('scout.prefix').'orders';
}

/**
* Getter for status label.
*
Expand Down Expand Up @@ -296,63 +262,6 @@ public function user()
);
}

/**
* {@inheritDoc}
*/
protected function getSearchableAttributes()
{
$data = [
'id' => $this->id,
'channel' => $this->channel->name,
'reference' => $this->reference,
'customer_reference' => $this->customer_reference,
'status' => $this->status,
'placed_at' => optional($this->placed_at)->timestamp,
'created_at' => $this->created_at->timestamp,
'sub_total' => $this->sub_total->value,
'total' => $this->total->value,
'currency_code' => $this->currency_code,
'charges' => $this->transactions->map(function ($transaction) {
return [
'reference' => $transaction->reference,
];
}),
'currency' => $this->currency_code,
'lines' => $this->productLines->map(function ($line) {
return [
'description' => $line->description,
'identifier' => $line->identifier,
];
})->toArray(),
];

foreach ($this->addresses as $address) {
$fields = [
'first_name',
'last_name',
'company_name',
'line_one',
'line_two',
'line_three',
'city',
'state',
'postcode',
'contact_email',
'contact_phone',
];

foreach ($fields as $field) {
$data["{$address->type}_{$field}"] = $address->getAttribute($field);
}

$data["{$address->type}_country"] = optional($address->country)->name;
}

$data['tags'] = $this->tags->pluck('value')->toArray();

return $data;
}

/**
* Determines if this is a draft order.
*/
Expand Down
55 changes: 0 additions & 55 deletions packages/core/src/Models/ProductOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ class ProductOption extends BaseModel implements SpatieHasMedia
use HasTranslations;
use Searchable;

/**
* Define our base filterable attributes.
*
* @var array
*/
protected $filterable = [];

/**
* Define our base sortable attributes.
*
* @var array
*/
protected $sortable = [
'name',
'label',
];

/**
* Define which attributes should be cast.
*
Expand All @@ -57,16 +40,6 @@ class ProductOption extends BaseModel implements SpatieHasMedia
'label' => AsCollection::class,
];

/**
* Get the name of the index associated with the model.
*
* @return string
*/
public function searchableAs()
{
return config('scout.prefix').'product_options';
}

/**
* Return a new factory instance for the model.
*/
Expand Down Expand Up @@ -110,32 +83,4 @@ public function values()
{
return $this->hasMany(ProductOptionValue::class)->orderBy('position');
}

/**
* {@inheritDoc}
*/
public function getSearchableAttributes()
{
$data['id'] = $this->id;

// Loop for add option name
foreach ($this->name as $locale => $name) {
$data['name_'.$locale] = $name;
}

// Loop for add option label
foreach ($this->name as $locale => $name) {
$data['label_'.$locale] = $name;
}

// Loop for add options
foreach ($this->values as $option) {
foreach ($option->name as $locale => $name) {
$key = 'option_'.$option->id.'_'.$locale;
$data[$key] = $name;
}
}

return $data;
}
}
30 changes: 30 additions & 0 deletions packages/core/src/Search/CollectionIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Lunar\Search;

use Illuminate\Database\Eloquent\Builder;

class CollectionIndexer extends ScoutIndexer
{
public function getSortableFields(): array
{
return [
'created_at',
'updated_at',
'name',
];
}

public function getFilterableFields(): array
{
return [
'__soft_deleted',
'name',
];
}

public function makeAllSearchableUsing(Builder $query): Builder
{
return $query;
}
}
99 changes: 99 additions & 0 deletions packages/core/src/Search/OrderIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Lunar\Search;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class OrderIndexer extends ScoutIndexer
{
public function getSortableFields(): array
{
return [
'customer_id',
'user_id',
'channel_id',
'created_at',
'updated_at',
'total',
];
}

public function getFilterableFields(): array
{
return [
'customer_id',
'user_id',
'status',
'placed_at',
'channel_id',
'tags',
'__soft_deleted',
];
}

public function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with([
'channel',
'transactions',
'productLines',
'addresses',
'tags',
]);
}

public function toSearchableArray(Model $model): array
{
$data = [
'id' => $model->id,
'channel' => $model->channel->name,
'reference' => $model->reference,
'customer_reference' => $model->customer_reference,
'status' => $model->status,
'placed_at' => optional($model->placed_at)->timestamp,
'created_at' => $model->created_at->timestamp,
'sub_total' => $model->sub_total->value,
'total' => $model->total->value,
'currency_code' => $model->currency_code,
'charges' => $model->transactions->map(function ($transaction) {
return [
'reference' => $transaction->reference,
];
}),
'currency' => $model->currency_code,
'lines' => $model->productLines->map(function ($line) {
return [
'description' => $line->description,
'identifier' => $line->identifier,
];
})->toArray(),
];

foreach ($model->addresses as $address) {
$fields = [
'first_name',
'last_name',
'company_name',
'line_one',
'line_two',
'line_three',
'city',
'state',
'postcode',
'contact_email',
'contact_phone',
];

foreach ($fields as $field) {
$data["{$address->type}_{$field}"] = $address->getAttribute($field);
}

$data["{$address->type}_country"] = optional($address->country)->name;
}

$data['tags'] = $model->tags->pluck('value')->toArray();

return $data;
}
}
Loading
Loading