-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathAlgoliaEngine.php
148 lines (116 loc) · 3.84 KB
/
AlgoliaEngine.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace rias\scout\engines;
use Algolia\AlgoliaSearch\SearchClient as Algolia;
use craft\base\Element;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use rias\scout\IndexSettings;
use rias\scout\ScoutIndex;
class AlgoliaEngine extends Engine
{
/** @var \Algolia\AlgoliaSearch\SearchClient */
protected $algolia;
/** @var \rias\scout\ScoutIndex */
public $scoutIndex;
public function __construct(ScoutIndex $scoutIndex, Algolia $algolia)
{
$this->scoutIndex = $scoutIndex;
$this->algolia = $algolia;
}
/**
* Update the given model in the index.
*
* @param array|Element $elements
*
* @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException
*/
public function update($elements)
{
if ($this->scoutIndex->replicaIndex) {
return;
}
$elements = new Collection(Arr::wrap($elements));
$elements = $elements->filter(function(Element $element) {
return get_class($element) === $this->scoutIndex->elementType;
});
if ($elements->isEmpty()) {
return;
}
$objects = $this->transformElements($elements);
if (!empty($objects)) {
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$index->saveObjects($objects);
}
}
public function delete($elements)
{
if ($this->scoutIndex->replicaIndex) {
return;
}
$elements = new Collection(Arr::wrap($elements));
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$objectIds = $elements->map(function($object) {
if ($object instanceof Element) {
return $object->id;
}
return $object['distinctID'] ?? $object['objectID'];
})->unique()->values()->all();
if (empty($objectIds)) {
return;
}
if (empty($this->scoutIndex->splitElementsOn)) {
return $index->deleteObjects($objectIds);
}
return $index->deleteBy([
'filters' => 'distinctID:' . implode(' OR distinctID:', $objectIds),
]);
}
public function flush()
{
if ($this->scoutIndex->replicaIndex) {
return;
}
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$index->clearObjects();
}
public function updateSettings(IndexSettings $indexSettings)
{
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$index->setSettings($indexSettings->settings, [
'forwardToReplicas' => $indexSettings->forwardToReplicas,
]);
}
public function getSettings(): array
{
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
return $index->getSettings();
}
public function getTotalRecords(): int
{
$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$response = $index->search('', [
'attributesToRetrieve' => null,
]);
return (int) $response['nbHits'];
}
private function transformElements(Collection $elements): array
{
$objects = $elements->map(function(Element $element) {
/** @var \rias\scout\behaviors\SearchableBehavior $element */
if (empty($searchableData = $element->toSearchableArray($this->scoutIndex))) {
return;
}
return array_merge(
['objectID' => $element->id],
$searchableData
);
})->filter()->values()->all();
if (empty($this->scoutIndex->splitElementsOn)) {
return $objects;
}
$result = $this->splitObjects($objects);
$this->delete($result['delete']);
$objects = $result['save'];
return $objects;
}
}