Skip to content

Commit

Permalink
Merge pull request #100 from matchish/routing
Browse files Browse the repository at this point in the history
Follow up: #91 Need to add Routing meta-field to be able to use join datatype
  • Loading branch information
matchish authored Mar 14, 2020
2 parents 6683500 + 7789ad6 commit 623716c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [3.0.2] - 2020-03-14
### Added
- Populate routing meta-field [#90](https://github.com/matchish/laravel-scout-elasticsearch/issues/90)

## [3.0.1] - 2020-03-02
### Fixed
- Respect the model uses soft delete
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "Search among multiple models with ElasticSearch and Laravel Scout",
"version": "3.0.1",
"version": "3.0.2",
"keywords": [
"laravel",
"scout",
Expand Down
12 changes: 10 additions & 2 deletions src/ElasticSearch/Params/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function delete($docs): void
}

/**
* TODO: Add ability to extend payload without modifying the class.
*
* @return array
*/
public function toArray(): array
Expand All @@ -42,11 +44,14 @@ function ($payload, $model) {
if ($model::usesSoftDelete() && config('scout.soft_delete', false)) {
$model->pushSoftDeleteMetadata();
}
$routing = $model->routing;
$scoutKey = $model->getScoutKey();
$payload['body'][] = [
'index' => [
'_index' => $model->searchableAs(),
'_id' => $model->getScoutKey(),
'_id' => $scoutKey,
'_type' => '_doc',
'routing' => false === empty($routing) ? $routing : $scoutKey,
],
];

Expand All @@ -62,11 +67,14 @@ function ($payload, $model) {
}, $payload);
$payload = collect($this->deleteDocs)->reduce(
function ($payload, $model) {
$routing = $model->routing;
$scoutKey = $model->getScoutKey();
$payload['body'][] = [
'delete' => [
'_index' => $model->searchableAs(),
'_id' => $model->getScoutKey(),
'_id' => $scoutKey,
'_type' => '_doc',
'routing' => false === empty($routing) ? $routing : $scoutKey,
],
];

Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/ElasticSearch/Params/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test_delete()
$params = $bulk->toArray();

$this->assertEquals([
'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 2]]],
'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]]],
], $params);
}

Expand All @@ -31,7 +31,7 @@ public function test_delete_with_custom_key_name()
$params = $bulk->toArray();

$this->assertEquals([
'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout']]],
'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']]],
], $params);
}

Expand All @@ -45,7 +45,7 @@ public function test_index()

$this->assertEquals([
'body' => [
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2]],
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]],
['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'],
],
], $params);
Expand All @@ -62,7 +62,7 @@ public function test_index_with_custom_key_name()

$this->assertEquals([
'body' => [
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout']],
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']],
['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'],
],
], $params);
Expand All @@ -78,7 +78,7 @@ public function test_push_soft_delete_meta_data()
$params = $bulk->toArray();
$this->assertEquals([
'body' => [
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2]],
['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]],
['title' => 'Scout', '__soft_deleted' => 0, 'id' => 2, '__class_name' => 'App\Product'],
],
], $params);
Expand Down

0 comments on commit 623716c

Please sign in to comment.