Skip to content

Commit 2002361

Browse files
authored
Move to lazy indexing/updating (#31)
1 parent 2616097 commit 2002361

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ window.meilisearch = new meilisearch({
6565
</script>
6666
```
6767

68+
You can optionally publish the config file for this package using:
69+
70+
```
71+
php artisan vendor:publish --tag=statamic-meilisearch-config
72+
```
73+
6874
### Few words about Document IDs in meilisearch
6975

7076
When you index your Statamic Entries, the driver will always transform the ID. This is required because meilisearch only allows `id` to be a string containing alphanumeric characters (a-Z, 0-9), hyphens (-) and underscores (_). You can read more about this in the [meilisearch documentation](https://www.meilisearch.com/docs/learn/core_concepts/primary_key#invalid_document_id)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"guzzlehttp/guzzle": "^7.3",
2929
"http-interop/http-factory-guzzle": "^1.0",
3030
"illuminate/support": "^9.0|^10.0",
31-
"statamic/cms": "^4.0"
31+
"statamic/cms": "^4.41"
3232
},
3333
"require-dev": {
3434
"orchestra/testbench": "^7.0 || ^8.0",

config/statamic-meilisearch.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
5+
// if you make this higher it will use more memory
6+
// but be quicker to update large numbers of documents
7+
'insert_chunk_size' => 100,
8+
9+
];

src/Meilisearch/Index.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Index extends BaseIndex
1414
{
1515
protected $client;
1616

17-
public function __construct(Client $client, $name, array $config, string $locale = null)
17+
public function __construct(Client $client, $name, array $config, ?string $locale = null)
1818
{
1919
$this->client = $client;
2020

@@ -28,11 +28,26 @@ public function search($query)
2828

2929
public function insert($document)
3030
{
31-
$fields = array_merge(
32-
$this->searchables()->fields($document),
33-
$this->getDefaultFields($document),
34-
);
35-
$this->getIndex()->updateDocuments([$fields]);
31+
return $this->insertMultiple(collect($document));
32+
}
33+
34+
public function insertMultiple($documents)
35+
{
36+
$documents
37+
->chunk(config('statamic-meilisearch.insert_chunk_size', 100))
38+
->each(function ($documents, $index) {
39+
$documents = $documents
40+
->map(fn ($document) => array_merge(
41+
$this->searchables()->fields($document),
42+
$this->getDefaultFields($document),
43+
))
44+
->values()
45+
->toArray();
46+
47+
$this->insertDocuments(new Documents($documents));
48+
});
49+
50+
return $this;
3651
}
3752

3853
public function delete($document)
@@ -53,15 +68,7 @@ public function exists()
5368

5469
protected function insertDocuments(Documents $documents)
5570
{
56-
try {
57-
if ($documents->isEmpty()) {
58-
return true;
59-
}
60-
61-
return $this->getIndex()->updateDocuments($documents->all());
62-
} catch (\Exception $e) {
63-
throw new \Exception($e->getMessage());
64-
}
71+
$this->getIndex()->updateDocuments($documents->all());
6572
}
6673

6774
protected function deleteIndex()
@@ -94,17 +101,7 @@ public function update()
94101
$this->deleteIndex();
95102
$this->createIndex();
96103

97-
// Prepare documents for update
98-
$searchables = $this->searchables()->all()->map(function ($entry) {
99-
return array_merge(
100-
$this->searchables()->fields($entry),
101-
$this->getDefaultFields($entry),
102-
);
103-
});
104-
105-
// Update documents
106-
$documents = new Documents($searchables);
107-
$this->insertDocuments($documents);
104+
$this->searchables()->lazy()->each(fn ($searchables) => $this->insertMultiple($searchables));
108105

109106
return $this;
110107
}
@@ -167,4 +164,9 @@ private function getSafeDocumentID(string $entryReference)
167164
})
168165
->implode('---');
169166
}
167+
168+
public function getCount()
169+
{
170+
return $this->getIndex()->stats()['numberOfDocuments'] ?? 0;
171+
}
170172
}

src/ServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ class ServiceProvider extends AddonServiceProvider
1111
{
1212
public function bootAddon()
1313
{
14+
$this->mergeConfigFrom(__DIR__.'/../config/statamic-meilisearch.php', 'statamic-meilisearch');
15+
16+
if ($this->app->runningInConsole()) {
17+
18+
$this->publishes([
19+
__DIR__.'/../config/statamic-meilisearch.php' => config_path('statamic-meilisearch.php'),
20+
], 'statamic-meilisearch-config');
21+
22+
}
23+
1424
Search::extend('meilisearch', function (Application $app, array $config, $name, $locale = null) {
1525
$client = $app->makeWith(Client::class, [
1626
'url' => $config['credentials']['url'],

0 commit comments

Comments
 (0)