From 1c01e7e014174069b9c73f441008b21de7dd8f3c Mon Sep 17 00:00:00 2001 From: Markus Machatschek Date: Wed, 10 Nov 2021 21:41:44 +0100 Subject: [PATCH] Replicate Algolia data when building the searchable data for documents --- src/Engines/MeiliSearchEngine.php | 8 ++++++-- tests/Unit/MeiliSearchEngineTest.php | 12 ++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Engines/MeiliSearchEngine.php b/src/Engines/MeiliSearchEngine.php index a6ec2c04..26ee7baf 100644 --- a/src/Engines/MeiliSearchEngine.php +++ b/src/Engines/MeiliSearchEngine.php @@ -62,7 +62,11 @@ public function update($models) return; } - return array_merge($searchableData, $model->scoutMetadata()); + return array_merge( + [$model->getScoutKeyName() => $model->getScoutKey()], + $searchableData, + $model->scoutMetadata() + ); })->filter()->values()->all(); if (! empty($objects)) { @@ -203,7 +207,7 @@ public function mapIds($results) } /** - * Pluck and the given results with the given primary key name. + * Pluck the given results with the given primary key name. * * @param mixed $results * @param string $key diff --git a/tests/Unit/MeiliSearchEngineTest.php b/tests/Unit/MeiliSearchEngineTest.php index 3522c997..efb0006d 100644 --- a/tests/Unit/MeiliSearchEngineTest.php +++ b/tests/Unit/MeiliSearchEngineTest.php @@ -275,10 +275,13 @@ public function test_a_model_is_indexed_with_a_custom_meilisearch_key() { $client = m::mock(Client::class); $client->shouldReceive('index')->with('table')->andReturn($index = m::mock(Indexes::class)); - $index->shouldReceive('addDocuments')->with([['id' => 'my-meilisearch-key.1']], 'id'); + $index->shouldReceive('addDocuments')->once()->with([[ + 'meilisearch-key' => 'my-meilisearch-key.5', + 'id' => 5, + ]], 'meilisearch-key'); $engine = new MeiliSearchEngine($client); - $engine->update(Collection::make([new MeiliSearchCustomKeySearchableModel()])); + $engine->update(Collection::make([new MeiliSearchCustomKeySearchableModel(['id' => 5])])); } public function test_flush_a_model_with_a_custom_meilisearch_key() @@ -426,4 +429,9 @@ public function getScoutKey() { return 'my-meilisearch-key.'.$this->getKey(); } + + public function getScoutKeyName() + { + return 'meilisearch-key'; + } }