From 7a8acf06531a20d5db3e3ed7c69947e69feff5be Mon Sep 17 00:00:00 2001 From: soranoba Date: Sun, 19 Apr 2020 16:50:39 +0900 Subject: [PATCH] Allow specification of elements other than body --- src/Indexers/SingleIndexer.php | 15 ++++++- tests/Dependencies/Model.php | 4 ++ tests/Indexers/AbstractIndexerTest.php | 8 ++++ tests/Indexers/SingleIndexerTest.php | 55 ++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Indexers/SingleIndexer.php b/src/Indexers/SingleIndexer.php index 285f510..69df15f 100644 --- a/src/Indexers/SingleIndexer.php +++ b/src/Indexers/SingleIndexer.php @@ -19,9 +19,19 @@ public function update(Collection $models) $model->pushSoftDeleteMetadata(); } + $scoutMetaBody = []; + $scoutMetaOther = []; + foreach ($model->scoutMetadata() as $k => $v) { + if (is_string($k) && substr($k, 0, 1) === '_') { + $scoutMetaOther[substr($k, 1)] = $v; + } else { + $scoutMetaBody[$k] = $v; + } + } + $modelData = array_merge( $model->toSearchableArray(), - $model->scoutMetadata() + $scoutMetaBody ); if (empty($modelData)) { @@ -32,6 +42,9 @@ public function update(Collection $models) $payload = (new DocumentPayload($model)) ->set('body', $modelData); + foreach ($scoutMetaOther as $k => $v) { + $payload->set($k, $v); + } if (in_array(Migratable::class, class_uses_recursive($indexConfigurator))) { $payload->useAlias('write'); diff --git a/tests/Dependencies/Model.php b/tests/Dependencies/Model.php index 5f5c630..f124678 100644 --- a/tests/Dependencies/Model.php +++ b/tests/Dependencies/Model.php @@ -52,6 +52,10 @@ public function mockModel(array $params = []) ->method('toSearchableArray') ->willReturn($params['searchable_array'] ?? []); + $mock + ->method('scoutMetadata') + ->willReturn($params['scoutMetadata'] ?? []); + $mock ->method('getIndexConfigurator') ->willReturn($params['index_configurator'] ?? $this->mockIndexConfigurator()); diff --git a/tests/Indexers/AbstractIndexerTest.php b/tests/Indexers/AbstractIndexerTest.php index 671bfd4..cec47d2 100644 --- a/tests/Indexers/AbstractIndexerTest.php +++ b/tests/Indexers/AbstractIndexerTest.php @@ -37,6 +37,14 @@ protected function setUp() 'trashed' => false, 'searchable_array' => [], ]), + $this->mockModel([ + 'key' => 4, + 'trashed' => false, + 'scoutMetadata' => [ + 'name' => 'bar', + '_routing' => 'woo', + ], + ]), ]); } } diff --git a/tests/Indexers/SingleIndexerTest.php b/tests/Indexers/SingleIndexerTest.php index 968243e..e94acf2 100644 --- a/tests/Indexers/SingleIndexerTest.php +++ b/tests/Indexers/SingleIndexerTest.php @@ -32,6 +32,17 @@ public function testUpdateWithDisabledSoftDelete() 'body' => [ 'name' => 'bar', ], + ]) + ->shouldReceive('index') + ->once() + ->with([ + 'index' => 'test', + 'type' => 'test', + 'id' => 4, + 'body' => [ + 'name' => 'bar', + ], + 'routing' => 'woo', ]); (new SingleIndexer) @@ -76,6 +87,18 @@ public function testUpdateWithEnabledSoftDelete() 'body' => [ '__soft_deleted' => 0, ], + ]) + ->shouldReceive('index') + ->once() + ->with([ + 'index' => 'test', + 'type' => 'test', + 'id' => 4, + 'body' => [ + 'name' => 'bar', + '__soft_deleted' => 0, + ], + 'routing' => 'woo', ]); (new SingleIndexer) @@ -110,6 +133,17 @@ public function testUpdateWithSpecifiedDocumentRefreshOption() 'body' => [ 'name' => 'bar', ], + ]) + ->shouldReceive('index') + ->once() + ->with([ + 'index' => 'test', + 'type' => 'test', + 'id' => 4, + 'body' => [ + 'name' => 'bar', + ], + 'routing' => 'woo', ]); (new SingleIndexer) @@ -150,6 +184,16 @@ public function testDelete() 'client' => [ 'ignore' => 404, ], + ]) + ->shouldReceive('delete') + ->once() + ->with([ + 'index' => 'test', + 'type' => 'test', + 'id' => 4, + 'client' => [ + 'ignore' => 404, + ], ]); (new SingleIndexer) @@ -195,6 +239,17 @@ public function testDeleteWithSpecifiedDocumentRefreshOption() 'client' => [ 'ignore' => 404, ], + ]) + ->shouldReceive('delete') + ->once() + ->with([ + 'index' => 'test', + 'type' => 'test', + 'id' => 2, + 'refresh' => true, + 'client' => [ + 'ignore' => 404, + ], ]); (new SingleIndexer())