Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Allow specification of elements other than body
Browse files Browse the repository at this point in the history
  • Loading branch information
soranoba committed Apr 19, 2020
1 parent 6844679 commit 7a8acf0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Indexers/SingleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions tests/Dependencies/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 8 additions & 0 deletions tests/Indexers/AbstractIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ protected function setUp()
'trashed' => false,
'searchable_array' => [],
]),
$this->mockModel([
'key' => 4,
'trashed' => false,
'scoutMetadata' => [
'name' => 'bar',
'_routing' => 'woo',
],
]),
]);
}
}
55 changes: 55 additions & 0 deletions tests/Indexers/SingleIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 7a8acf0

Please sign in to comment.