Skip to content

Commit

Permalink
修复分页使用 where 条件时返回总数为未过滤之前的总数的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vanry committed Nov 27, 2020
1 parent 3459db4 commit 37761d7
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/Engines/TNTSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Vanry\Scout\Engines;

use Illuminate\Database\Eloquent\Builder as Query;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Builder;
use TeamTNT\TNTSearch\TNTSearch;
use Laravel\Scout\Engines\Engine;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder as Query;
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
use TeamTNT\TNTSearch\TNTSearch;

class TNTSearchEngine extends Engine
{
Expand Down Expand Up @@ -115,23 +115,34 @@ public function paginate(Builder $builder, $perPage, $page)
{
$results = $this->search($builder);

$results['hits'] = $this->getFilteredTotalCount($builder, $results);

if ($builder->limit) {
$results['hits'] = $builder->limit;
$results['hits'] = min($results['hits'], $builder->limit);
}

$chunks = array_chunk($results['ids'], $perPage);

if (! empty($chunks)) {
if (array_key_exists($page - 1, $chunks)) {
$results['ids'] = $chunks[$page - 1];
} else {
$results['ids'] = end($chunks);
}
}
$results['ids'] = $chunks[$page - 1] ?? [];

return $results;
}

protected function getFilteredTotalCount(Builder $builder, $results)
{
$this->builder = $builder;

$model = $builder->model;

$query = $model->whereIn($model->getQualifiedKeyName(), $results['ids']);

if ($this->usesSoftDelete($model) && config('scout.soft_delete')) {
$query = $this->handleSoftDelete($query);
}

return $this->applyWheres($query)->count();
}

/**
* Perform the given search on the engine.
*
Expand All @@ -154,24 +165,22 @@ protected function performSearch(Builder $builder, array $options = [])
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map(Builder $builder, $results, $model)
{
return (is_null($results['ids']) || count($results['ids']) === 0)
? $model->newCollection()
: $this->mapModels($builder, $results, $model);
return (empty($results['ids'])) ? $model->newCollection() : $this->mapModels($builder, $results, $model);
}

/**
* Map eloquent models with search results.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
protected function mapModels(Builder $builder, $results, $model)
Expand Down Expand Up @@ -277,7 +286,7 @@ protected function indexPath($model)
protected function createIndex($model)
{
if (! file_exists($this->tnt->config['storage'])) {
mkdir($this->tnt->config['storage'], 0777, true);
mkdir($this->tnt->config['storage'], 0755, true);
}

$this->tnt->createIndex($this->indexName($model));
Expand Down

0 comments on commit 37761d7

Please sign in to comment.