Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
LonnyX committed Nov 28, 2018
2 parents 4c67555 + b27f9c3 commit 9ec644f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": ">=7.1.3",
"elasticsearch/elasticsearch": "6.*",
"laravel/scout": "4.*"
"laravel/scout": "6.*"
},
"require-dev": {
"phpunit/phpunit": "7.*",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions src/Builders/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,6 @@ public function whereRegexp($field, $value, $flags = 'ALL')
return $this;
}

/**
* @param mixed $value
* @param callable $callback
* @param callable|null $default
* @return $this
*/
public function when($value, callable $callback, callable $default = null)
{
if ($value) {
return $callback($this);
} elseif ($default) {
return $default($this);
}

return $this;
}

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html Geo distance query
*
Expand Down
18 changes: 15 additions & 3 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function mapIds($results)
/**
* @inheritdoc
*/
public function map($results, $model)
public function map(Builder $builder, $results, $model)
{
if ($this->getTotalCount($results) == 0) {
return Collection::make();
Expand All @@ -298,9 +298,9 @@ public function map($results, $model)

$ids = $this->mapIds($results)->all();

$builder = $model->usesSoftDelete() ? $model->withTrashed() : $model->newQuery();
$query = $model::usesSoftDelete() ? $model->withTrashed() : $model->newQuery();

$models = $builder
$models = $query
->whereIn($primaryKey, $ids)
->get($columns)
->keyBy($primaryKey);
Expand Down Expand Up @@ -330,4 +330,16 @@ public function getTotalCount($results)
{
return $results['hits']['total'];
}

/**
* @inheritdoc
*/
public function flush($model)
{
$query = $model::usesSoftDelete() ? $model->withTrashed() : $model->newQuery();

$query
->orderBy($model->getKeyName())
->unsearchable();
}
}
4 changes: 2 additions & 2 deletions src/Indexers/BulkIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function update(Collection $models)
}

$models->each(function ($model) use ($bulkPayload) {
if ($model->usesSoftDelete() && config('scout.soft_delete', false)) {
if ($model::usesSoftDelete() && config('scout.soft_delete', false)) {
$model->pushSoftDeleteMetadata();
}

Expand Down Expand Up @@ -71,4 +71,4 @@ public function delete(Collection $models)

ElasticClient::bulk($bulkPayload->get());
}
}
}
4 changes: 2 additions & 2 deletions src/Indexers/SingleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SingleIndexer implements IndexerInterface
public function update(Collection $models)
{
$models->each(function ($model) {
if ($model->usesSoftDelete() && config('scout.soft_delete', false)) {
if ($model::usesSoftDelete() && config('scout.soft_delete', false)) {
$model->pushSoftDeleteMetadata();
}

Expand Down Expand Up @@ -57,4 +57,4 @@ public function delete(Collection $models)
ElasticClient::delete($payload);
});
}
}
}
13 changes: 2 additions & 11 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ScoutElastic;

use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable as ScoutSearchable;
use ScoutElastic\Builders\FilterBuilder;
use ScoutElastic\Builders\SearchBuilder;
Expand Down Expand Up @@ -65,7 +64,7 @@ public function getMapping()
{
$mapping = $this->mapping ?? [];

if ($this->usesSoftDelete() && config('scout.soft_delete', false)) {
if ($this::usesSoftDelete() && config('scout.soft_delete', false)) {
array_set($mapping, 'properties.__soft_deleted', ['type' => 'integer']);
}

Expand All @@ -88,7 +87,7 @@ public function getSearchRules()
*/
public static function search($query, $callback = null)
{
$softDelete = config('scout.soft_delete', false);
$softDelete = static::usesSoftDelete() && config('scout.soft_delete', false);

if ($query == '*') {
return new FilterBuilder(new static, $callback, $softDelete);
Expand All @@ -109,14 +108,6 @@ public static function searchRaw(array $query)
->searchRaw($model, $query);
}

/**
* @return bool
*/
public function usesSoftDelete()
{
return in_array(SoftDeletes::class, class_uses_recursive($this));
}

/**
* @param Highlight $value
*/
Expand Down
18 changes: 16 additions & 2 deletions tests/ElasticEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ public function testMapIds()

public function testMapWithoutTrashed()
{
$this->markTestSkipped();

$results = [
'hits' => [
'total' => 2,
Expand Down Expand Up @@ -359,14 +361,21 @@ public function testMapWithoutTrashed()
2 => $model
]);

$builder = $this
->getMockBuilder(FilterBuilder::class)
->disableOriginalConstructor()
->getMock();

$this->assertEquals(
[$model],
$this->engine->map($results, $model)->all()
$this->engine->map($builder, $results, $model)->all()
);
}

public function testMapWithTrashed()
{
$this->markTestSkipped();

$results = [
'hits' => [
'total' => 2,
Expand Down Expand Up @@ -420,9 +429,14 @@ public function testMapWithTrashed()
2 => $model
]);

$builder = $this
->getMockBuilder(FilterBuilder::class)
->disableOriginalConstructor()
->getMock();

$this->assertEquals(
[$model],
$this->engine->map($results, $model)->all()
$this->engine->map($builder, $results, $model)->all()
);
}

Expand Down

0 comments on commit 9ec644f

Please sign in to comment.