-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathIndex.php
More file actions
130 lines (103 loc) · 3.66 KB
/
Index.php
File metadata and controls
130 lines (103 loc) · 3.66 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
<?php
namespace Statamic\Search\Algolia;
use Algolia\AlgoliaSearch\Api\SearchClient;
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Support\Arr;
use Statamic\Search\Documents;
use Statamic\Search\Index as BaseIndex;
use Statamic\Search\IndexNotFoundException;
use Statamic\Search\Result;
use Statamic\Support\Str;
class Index extends BaseIndex
{
private SearchClient $client;
private bool $settingsInitialized = false;
public function __construct(SearchClient $client, $name, $config, $locale)
{
$this->client = $client;
parent::__construct($name, $config, $locale);
}
protected function client(): SearchClient
{
if (! $this->settingsInitialized && isset($this->config['settings']) && ! $this->exists()) {
$this->client->setSettings($this->name, $this->config['settings']);
$this->settingsInitialized = true;
}
return $this->client;
}
public function search($query)
{
return (new Query($this))->query($query);
}
public function insertDocuments(Documents $documents)
{
$documents = $documents->map(function ($item, $id) {
$item['objectID'] = $id;
return $item;
})->values();
try {
$this->client()->saveObjects($this->name, $documents->all());
} catch (ConnectException $e) {
throw new \Exception('Error connecting to Algolia. Check your API credentials.', 0, $e);
}
}
public function delete($document)
{
$this->client()->deleteObject($this->name, $document->getSearchReference());
}
public function deleteIndex()
{
$this->client()->deleteIndex($this->name);
}
public function update()
{
$this->client()->clearObjects($this->name);
if (isset($this->config['settings'])) {
$this->client()->setSettings($this->name, $this->config['settings']);
}
$this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables));
return $this;
}
public function searchUsingApi($query, $fields = null)
{
$arguments = ['query' => $query];
if ($fields) {
$arguments['restrictSearchableAttributes'] = implode(',', Arr::wrap($fields));
}
try {
$response = $this->client()->searchSingleIndex($this->name, $arguments);
} catch (AlgoliaException $e) {
$this->handleAlgoliaException($e);
}
return collect($response['hits'])->map(function ($hit) {
$hit['reference'] = $hit['objectID'];
return $hit;
});
}
public function exists()
{
return collect($this->client->listIndices()['items'])->first(function ($index) {
return $index['name'] == $this->name;
}) !== null;
}
private function handleAlgoliaException($e)
{
if (Str::contains($e->getMessage(), "Index {$this->name} does not exist")) {
throw new IndexNotFoundException("Index [{$this->name}] does not exist.");
}
if (preg_match('/attribute (.*) is not in searchableAttributes/', $e->getMessage(), $matches)) {
throw new \Exception(
"Field [{$matches[1]}] does not exist in this index's searchableAttributes list."
);
}
throw $e;
}
public function extraAugmentedResultData(Result $result)
{
return [
'search_highlights' => $result->getRawResult()['_highlightResult'] ?? null,
'search_snippets' => $result->getRawResult()['_snippetResult'] ?? null,
];
}
}