-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathIndex.php
More file actions
166 lines (137 loc) · 4.38 KB
/
Index.php
File metadata and controls
166 lines (137 loc) · 4.38 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace StatamicRadPack\Meilisearch\Meilisearch;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Meilisearch\Client;
use Meilisearch\Exceptions\ApiException;
use Statamic\Contracts\Search\Searchable;
use Statamic\Search\Documents;
use Statamic\Search\Index as BaseIndex;
class Index extends BaseIndex
{
protected $client;
public function __construct(Client $client, $name, array $config, ?string $locale = null)
{
$this->client = $client;
parent::__construct($name, $config, $locale);
}
public function search($query)
{
return (new Query($this))->query($query);
}
public function insert($document)
{
return $this->insertMultiple(collect([$document]));
}
public function fields(Searchable $searchable)
{
return array_merge(
$this->searchables()->fields($searchable),
$this->getDefaultFields($searchable)
);
}
public function delete($document)
{
$this->getIndex()->deleteDocument($this->getSafeDocumentID($document->getSearchReference()));
}
public function exists()
{
try {
$this->getIndex()->fetchRawInfo();
return true;
} catch (ApiException $e) {
return false;
}
}
public function insertDocuments(Documents $documents)
{
$this->getIndex()->updateDocuments($documents->values()->all());
}
protected function deleteIndex()
{
try {
$this->getIndex()->delete();
} catch (ApiException $e) {
$this->handlemeilisearchException($e, 'deleteIndex');
}
}
protected function createIndex()
{
try {
$this->client->createIndex($this->name, ['primaryKey' => 'id']);
if (! isset($this->config['settings'])) {
return;
}
$this->getIndex()->updateSettings($this->config['settings']);
$this->getIndex()->updatePagination($this->config['pagination'] ?? ['maxTotalHits' => 1000000]);
} catch (ApiException $e) {
$this->handlemeilisearchException($e, 'createIndex');
}
}
public function update()
{
$this->deleteIndex();
$this->createIndex();
$this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables));
return $this;
}
public function searchUsingApi($query, array $options = ['hitsPerPage' => 1000000, 'showRankingScore' => true]): Collection
{
try {
$searchResults = $this->getIndex()->search($query, $options);
} catch (\Exception $e) {
$this->handleMeilisearchException($e, 'searchUsingApi');
}
return collect($searchResults->getHits());
}
private function getIndex()
{
return $this->client->index($this->name);
}
private function getDefaultFields(Searchable $entry): array
{
return [
'id' => $this->getSafeDocumentID($entry->getSearchReference()),
'reference' => $entry->getSearchReference(),
];
}
/**
* Custom error parsing for Meilisearch exceptions.
*/
private function handleMeilisearchException($e, $method)
{
// Ignore if already created.
if ($e->errorCode === 'index_already_exists' && $method === 'createIndex') {
return true;
}
// Ignore if not found.
if ($e->errorCode === 'index_not_found' && $method === 'deleteIndex') {
return true;
}
throw $e;
}
/**
* Get the document ID for the given entry.
* As a document id is only allowed to be an integer or string composed only of alphanumeric characters (a-z A-Z 0-9), hyphens (-), and underscores (_) we need to make sure that the ID is safe to use.
* More under https://docs.meilisearch.com/reference/api/error_codes.html#invalid-document-id
*
* @return string
*/
private function getSafeDocumentID(string $entryReference)
{
return Str::of($entryReference)
->explode('::')
->map(function ($part) {
return Str::slug($part);
})
->implode('---');
}
public function getCount()
{
return $this->getIndex()->stats()['numberOfDocuments'] ?? 0;
}
public function client()
{
return $this->client;
}
}