78
common/Search/Drivers/Mysql/MysqlFullTextIndexer.php
Executable file
78
common/Search/Drivers/Mysql/MysqlFullTextIndexer.php
Executable file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Search\Drivers\Mysql;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MysqlFullTextIndexer
|
||||
{
|
||||
private string $tableName;
|
||||
|
||||
private string $indexName;
|
||||
|
||||
private array $searchableFields;
|
||||
|
||||
private bool $indexAlreadyExists;
|
||||
|
||||
public function createOrUpdateIndex(string $model): void
|
||||
{
|
||||
$model = new $model();
|
||||
$this->tableName =
|
||||
config('database.connections.mysql.prefix') . $model->getTable();
|
||||
$this->indexName = $model->searchableAs();
|
||||
|
||||
$this->searchableFields = $model->getSearchableKeys(true);
|
||||
|
||||
$this->indexAlreadyExists = $this->indexExists();
|
||||
|
||||
if (!$this->indexAlreadyExists || $this->indexNeedsUpdate()) {
|
||||
$this->dropIndex();
|
||||
$fields = implode(',', $this->searchableFields);
|
||||
DB::statement(
|
||||
"CREATE FULLTEXT INDEX $this->indexName ON $this->tableName ($fields)",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function indexExists(): bool
|
||||
{
|
||||
return !empty(
|
||||
DB::select("SHOW INDEX FROM $this->tableName WHERE Key_name = ?", [
|
||||
$this->indexName,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
private function indexNeedsUpdate(): bool
|
||||
{
|
||||
$currentIndexFields = $this->searchableFields;
|
||||
$expectedIndexFields = $this->getIndexFields();
|
||||
|
||||
return $currentIndexFields != $expectedIndexFields;
|
||||
}
|
||||
|
||||
private function getIndexFields(): array
|
||||
{
|
||||
$index = DB::select(
|
||||
"SHOW INDEX FROM $this->tableName WHERE Key_name = ?",
|
||||
[$this->indexName],
|
||||
);
|
||||
|
||||
$indexFields = [];
|
||||
|
||||
foreach ($index as $idx) {
|
||||
$indexFields[] = $idx->Column_name;
|
||||
}
|
||||
|
||||
return $indexFields;
|
||||
}
|
||||
|
||||
private function dropIndex()
|
||||
{
|
||||
if ($this->indexAlreadyExists) {
|
||||
DB::statement(
|
||||
"ALTER TABLE $this->tableName DROP INDEX $this->indexName",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
136
common/Search/Drivers/Mysql/MysqlSearchEngine.php
Executable file
136
common/Search/Drivers/Mysql/MysqlSearchEngine.php
Executable file
@@ -0,0 +1,136 @@
|
||||
<?php namespace Common\Search\Drivers\Mysql;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use Laravel\Scout\Builder;
|
||||
use Laravel\Scout\Engines\Engine;
|
||||
|
||||
class MysqlSearchEngine extends Engine
|
||||
{
|
||||
/**
|
||||
* Update the given model in the index.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Collection $models
|
||||
*/
|
||||
public function update($models): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given model from the index.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Collection $models
|
||||
*/
|
||||
public function delete($models): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function search(Builder $builder)
|
||||
{
|
||||
return $this->performSearch($builder, ['limit' => $builder->limit]);
|
||||
}
|
||||
|
||||
public function paginate(Builder $builder, $perPage, $page): Collection
|
||||
{
|
||||
return $this->performSearch($builder, [
|
||||
'limit' => $perPage,
|
||||
'offset' => $perPage * $page - $perPage,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function performSearch(
|
||||
Builder $builder,
|
||||
array $options = []
|
||||
): Collection {
|
||||
if ($builder->callback) {
|
||||
return call_user_func(
|
||||
$builder->callback,
|
||||
null,
|
||||
$builder->query,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
|
||||
$query = $builder->model->mysqlSearch($builder->query);
|
||||
|
||||
if (!empty($builder->orders)) {
|
||||
foreach ($builder->orders as $order) {
|
||||
$query->orderBy(
|
||||
Arr::get($order, 'column'),
|
||||
Arr::get($order, 'direction'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($options['limit'])) {
|
||||
$query = $query->take($options['limit']);
|
||||
}
|
||||
if (isset($options['offset'])) {
|
||||
$query = $query->skip($options['offset']);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pluck and return the primary keys of the given results.
|
||||
*
|
||||
* @param mixed $results
|
||||
* @return Collection
|
||||
*/
|
||||
public function mapIds($results)
|
||||
{
|
||||
return $results->pluck('id')->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the given results to instances of the given model.
|
||||
*
|
||||
* @param Builder $builder
|
||||
* @param mixed $results
|
||||
* @param Model $model
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function map(Builder $builder, $results, $model)
|
||||
{
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total count from a raw result returned by the engine.
|
||||
*
|
||||
* @param mixed $results
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalCount($results)
|
||||
{
|
||||
return count($results);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function flush($model)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function lazyMap(Builder $builder, $results, $model)
|
||||
{
|
||||
return LazyCollection::make($results);
|
||||
}
|
||||
|
||||
public function createIndex($name, array $options = [])
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function deleteIndex($name)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user