jikan-rest/app/JikanApiSearchableModel.php

105 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?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
{
$titleAttributeName = $this->getTitleAttributeName();
return [
'name' => $this->searchableAs(),
'fields' => [
[
'name' => '.*',
'type' => 'auto',
],
[
'name' => $titleAttributeName,
'type' => 'string',
'sort' => true,
'optional' => false
]
]
];
}
/**
* Get the name of the index associated with the model.
*
* @return string
*/
public function searchableAs(): string
{
return strtolower($this->table) . '_index' . (env("APP_ENV") === "testing" ? "_testing" : "");
}
/**
* Get the value used to index the model.
*
* @return mixed
*/
public function getScoutKey(): mixed
{
return $this->mal_id;
}
/**
* Get the key name used to index the model.
*
* @return mixed
*/
public function getScoutKeyName(): mixed
{
return 'mal_id';
}
/**
* 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
{
if (!$val) {
return "";
}
2022-10-03 20:04:39 +01:00
return preg_replace("/[^[:alnum:][:space:]]/u", ' ', $val) ?? "";
}
public function getTitleAttributeName(): string
{
return 'title';
}
}