Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 14cf9cd

Browse files
committed
Soft delete support
1 parent b57a9fc commit 14cf9cd

23 files changed

+469
-136
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="tests/bootstrap.php"
55
colors="true"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"

src/Builders/FilterBuilder.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ScoutElastic\Builders;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Laravel\Scout\Builder;
67

78
class FilterBuilder extends Builder
@@ -25,10 +26,19 @@ class FilterBuilder extends Builder
2526
*/
2627
public $select = [];
2728

28-
public function __construct($model, $callback = null)
29+
/**
30+
* @param Model $model
31+
* @param callable|null $callback
32+
* @param bool $softDelete
33+
*/
34+
public function __construct($model, $callback = null, $softDelete = false)
2935
{
3036
$this->model = $model;
3137
$this->callback = $callback;
38+
39+
if ($softDelete) {
40+
$this->wheres['must'][] = ['term' => ['__soft_deleted' => 0]];
41+
}
3242
}
3343

3444
/**
@@ -280,4 +290,29 @@ public function count()
280290
->engine()
281291
->count($this);
282292
}
293+
294+
/**
295+
* @inheritdoc
296+
*/
297+
public function withTrashed()
298+
{
299+
$this->wheres['must'] = collect($this->wheres['must'])
300+
->filter(function ($item) {
301+
return array_get($item, 'term.__soft_deleted') !== 0;
302+
})
303+
->values()
304+
->all();
305+
306+
return $this;
307+
}
308+
309+
/**
310+
* @inheritdoc
311+
*/
312+
public function onlyTrashed()
313+
{
314+
return tap($this->withTrashed(), function () {
315+
$this->wheres['must'][] = ['term' => ['__soft_deleted' => 1]];
316+
});
317+
}
283318
}

src/Builders/SearchBuilder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
namespace ScoutElastic\Builders;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
57
class SearchBuilder extends FilterBuilder
68
{
79
public $rules = [];
810

9-
public function __construct($model, $query, $callback = null)
11+
/**
12+
* @param Model $model
13+
* @param string $query
14+
* @param callable|null $callback
15+
* @param bool $softDelete
16+
*/
17+
public function __construct($model, $query, $callback = null, $softDelete = false)
1018
{
11-
$this->model = $model;
19+
parent::__construct($model, $callback, $softDelete);
20+
1221
$this->query = $query;
13-
$this->callback = $callback;
1422
}
1523

1624
public function rule($rule)

src/ElasticEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ public function map($results, $model)
242242

243243
$ids = $this->mapIds($results);
244244

245-
$models = $model
245+
$builder = $model->usesSoftDelete() ? $model->withTrashed() : $model->newQuery();
246+
247+
$models = $builder
246248
->whereIn($primaryKey, $ids)
247249
->get($columns)
248250
->keyBy($primaryKey);

src/Indexers/BulkIndexer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ public function update(Collection $models)
2222
}
2323

2424
$models->each(function ($model) use ($bulkPayload) {
25-
$modelData = $model->toSearchableArray();
25+
if ($model->usesSoftDelete() && config('scout.soft_delete', false)) {
26+
$model->pushSoftDeleteMetadata();
27+
}
28+
29+
$modelData = array_merge(
30+
$model->toSearchableArray(),
31+
$model->scoutMetadata()
32+
);
2633

2734
if (empty($modelData)) {
2835
return true;

src/Indexers/SingleIndexer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class SingleIndexer implements IndexerInterface
1212
public function update(Collection $models)
1313
{
1414
$models->each(function ($model) {
15-
$modelData = $model->toSearchableArray();
15+
if ($model->usesSoftDelete() && config('scout.soft_delete', false)) {
16+
$model->pushSoftDeleteMetadata();
17+
}
18+
19+
$modelData = array_merge(
20+
$model->toSearchableArray(),
21+
$model->scoutMetadata()
22+
);
1623

1724
if (empty($modelData)) {
1825
return true;

src/Searchable.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ScoutElastic;
44

5+
use Illuminate\Database\Eloquent\SoftDeletes;
56
use Laravel\Scout\Searchable as ScoutSearchable;
67
use ScoutElastic\Builders\FilterBuilder;
78
use ScoutElastic\Builders\SearchBuilder;
@@ -47,7 +48,13 @@ public function getIndexConfigurator()
4748

4849
public function getMapping()
4950
{
50-
return isset($this->mapping) ? $this->mapping : [];
51+
$mapping = $this->mapping ?? [];
52+
53+
if ($this->usesSoftDelete() && config('scout.soft_delete', false)) {
54+
array_set($mapping, 'properties.__soft_deleted', ['type' => 'integer']);
55+
}
56+
57+
return $mapping;
5158
}
5259

5360
public function getSearchRules()
@@ -57,10 +64,12 @@ public function getSearchRules()
5764

5865
public static function search($query, $callback = null)
5966
{
67+
$softDelete = config('scout.soft_delete', false);
68+
6069
if ($query == '*') {
61-
return new FilterBuilder(new static, $callback);
70+
return new FilterBuilder(new static, $callback, $softDelete);
6271
} else {
63-
return new SearchBuilder(new static, $query, $callback);
72+
return new SearchBuilder(new static, $query, $callback, $softDelete);
6473
}
6574
}
6675

@@ -71,4 +80,12 @@ public static function searchRaw($query)
7180
return $model->searchableUsing()
7281
->searchRaw($model, $query);
7382
}
83+
84+
/**
85+
* @return bool
86+
*/
87+
public function usesSoftDelete()
88+
{
89+
return in_array(SoftDeletes::class, class_uses_recursive($this));
90+
}
7491
}

tests/AbstractTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ScoutElastic\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class AbstractTestCase extends TestCase
8+
{
9+
protected function tearDown()
10+
{
11+
parent::tearDown();
12+
13+
Config::reset();
14+
}
15+
}

tests/Builders/AbstractBuilderTest.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)