2022-06-08 17:05:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Jenssegers\Mongodb\Eloquent\Model;
|
|
|
|
use Typesense\LaravelTypesense\Interfaces\TypesenseDocument;
|
|
|
|
|
|
|
|
abstract class JikanApiSearchableModel extends JikanApiModel implements TypesenseDocument
|
|
|
|
{
|
|
|
|
use JikanSearchable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public abstract function typesenseQueryBy(): array;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Typesense schema to be created.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCollectionSchema(): array
|
|
|
|
{
|
2024-01-31 18:33:32 +00:00
|
|
|
$titleAttributeName = $this->getTitleAttributeName();
|
|
|
|
|
2022-06-08 17:05:12 +01:00
|
|
|
return [
|
|
|
|
'name' => $this->searchableAs(),
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'name' => '.*',
|
|
|
|
'type' => 'auto',
|
2024-01-31 18:33:32 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => $titleAttributeName,
|
|
|
|
'type' => 'string',
|
|
|
|
'sort' => true,
|
|
|
|
'optional' => false
|
2022-06-08 17:05:12 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the index associated with the model.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function searchableAs(): string
|
|
|
|
{
|
2022-12-04 19:53:27 +00:00
|
|
|
return strtolower($this->table) . '_index' . (env("APP_ENV") === "testing" ? "_testing" : "");
|
2022-06-08 17:05:12 +01:00
|
|
|
}
|
2022-06-08 17:17:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value used to index the model.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getScoutKey(): mixed
|
|
|
|
{
|
|
|
|
return $this->mal_id;
|
|
|
|
}
|
2022-06-17 16:23:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the key name used to index the model.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getScoutKeyName(): mixed
|
|
|
|
{
|
|
|
|
return 'mal_id';
|
|
|
|
}
|
2022-06-18 15:03:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns what weights to use on query_by fields.
|
|
|
|
* https://typesense.org/docs/0.23.0/api/documents.html#search-parameters
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getTypeSenseQueryByWeights(): string|null
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns which fields the search index should sort on when searching
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function getSearchIndexSortBy(): array|null
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-10-03 20:04:39 +01:00
|
|
|
|
|
|
|
protected function simplifyStringForSearch($val): string
|
|
|
|
{
|
2023-05-01 16:52:52 +01:00
|
|
|
if (!$val) {
|
|
|
|
return "";
|
|
|
|
}
|
2022-10-03 20:04:39 +01:00
|
|
|
return preg_replace("/[^[:alnum:][:space:]]/u", ' ', $val) ?? "";
|
|
|
|
}
|
2023-09-17 20:28:45 +00:00
|
|
|
|
|
|
|
public function getTitleAttributeName(): string
|
|
|
|
{
|
|
|
|
return 'title';
|
|
|
|
}
|
2022-06-08 17:05:12 +01:00
|
|
|
}
|