This repository has been archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
/
BulkIndexer.php
80 lines (61 loc) · 2.15 KB
/
BulkIndexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace ScoutElastic\Indexers;
use Illuminate\Database\Eloquent\Collection;
use ScoutElastic\Facades\ElasticClient;
use ScoutElastic\Migratable;
use ScoutElastic\Payloads\RawPayload;
use ScoutElastic\Payloads\TypePayload;
class BulkIndexer implements IndexerInterface
{
/**
* {@inheritdoc}
*/
public function update(Collection $models)
{
$model = $models->first();
$indexConfigurator = $model->getIndexConfigurator();
$bulkPayload = new TypePayload($model);
if (in_array(Migratable::class, class_uses_recursive($indexConfigurator))) {
$bulkPayload->useAlias('write');
}
if ($documentRefresh = config('scout_elastic.document_refresh')) {
$bulkPayload->set('refresh', $documentRefresh);
}
$models->each(function ($model) use ($bulkPayload) {
if ($model::usesSoftDelete() && config('scout.soft_delete', false)) {
$model->pushSoftDeleteMetadata();
}
$modelData = array_merge(
$model->toSearchableArray(),
$model->scoutMetadata()
);
if (empty($modelData)) {
return true;
}
$actionPayload = (new RawPayload)
->set('index._id', $model->getScoutKey());
$bulkPayload
->add('body', $actionPayload->get())
->add('body', $modelData);
});
ElasticClient::bulk($bulkPayload->get());
}
/**
* {@inheritdoc}
*/
public function delete(Collection $models)
{
$model = $models->first();
$bulkPayload = new TypePayload($model);
$models->each(function ($model) use ($bulkPayload) {
$actionPayload = (new RawPayload)
->set('delete._id', $model->getScoutKey());
$bulkPayload->add('body', $actionPayload->get());
});
if ($documentRefresh = config('scout_elastic.document_refresh')) {
$bulkPayload->set('refresh', $documentRefresh);
}
$bulkPayload->set('client.ignore', 404);
ElasticClient::bulk($bulkPayload->get());
}
}