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

Allow specification of elements other than body #359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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