Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Ability to use where() method #8

Merged
merged 2 commits into from
Apr 20, 2020
Merged
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
51 changes: 33 additions & 18 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Meilisearch $meilisearch, bool $softDelete = false)
/**
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @param \Illuminate\Database\Eloquent\Collection $models
*
* @return void
*/
Expand All @@ -55,15 +55,15 @@ public function update($models)
return array_merge($searchableData, $model->scoutMetadata());
})->filter()->values()->all();

if (! empty($objects)) {
if (!empty($objects)) {
$index->addDocuments($objects, $models->first()->getKeyName());
}
}

/**
* Remove the given model from the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @param \Illuminate\Database\Eloquent\Collection $models
*
* @return void
*/
Expand All @@ -81,38 +81,40 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param Builder $builder
*
* @return mixed
*/
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'filters' => $this->filters($builder),
'limit' => $builder->limit,
]));
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param int $perPage
* @param int $page
* @param Builder $builder
* @param int $perPage
* @param int $page
*
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
{
return $this->performSearch($builder, array_filter([
'filters' => $this->filters($builder),
'limit' => $perPage,
]));
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param array $options
* @param Builder $builder
* @param array $options
*
* @return mixed
*/
Expand All @@ -132,10 +134,23 @@ protected function performSearch(Builder $builder, array $options = [])
return $meilisearch->search($builder->query, $options);
}

/**
* Get the filter array for the query.
*
* @param \Laravel\Scout\Builder $builder
* @return array
*/
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
return $key . '=' . '"'.$value.'"';
})->values()->implode(' AND ');
}

/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @param mixed $results
*
* @return \Illuminate\Support\Collection
*/
Expand All @@ -150,9 +165,9 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return \Illuminate\Database\Eloquent\Collection
*/
Expand All @@ -177,7 +192,7 @@ public function map(Builder $builder, $results, $model)
/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @param mixed $results
*
* @return int
*/
Expand All @@ -189,7 +204,7 @@ public function getTotalCount($results)
/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
Expand All @@ -203,7 +218,7 @@ public function flush($model)
/**
* Determine if the given model uses soft deletes.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return bool
*/
Expand All @@ -215,8 +230,8 @@ protected function usesSoftDelete($model)
/**
* Dynamically call the MeiliSearch client instance.
*
* @param string $method
* @param array $parameters
* @param string $method
* @param array $parameters
*
* @return mixed
*/
Expand Down