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

Commit 71298a0

Browse files
committed
Fixed bulk indexing url and payload size.
1 parent 2b3232d commit 71298a0

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/Indexers/BulkIndexer.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
use ScoutElastic\Facades\ElasticClient;
77
use ScoutElastic\Migratable;
88
use ScoutElastic\Payloads\RawPayload;
9+
use ScoutElastic\Payloads\TypePayload;
910

1011
class BulkIndexer implements IndexerInterface
1112
{
1213
public function update(Collection $models)
1314
{
14-
$bulkPayload = new RawPayload();
15+
$model = $models->first();
16+
$indexConfigurator = $model->getIndexConfigurator();
17+
18+
$bulkPayload = new TypePayload($model);
19+
20+
if (in_array(Migratable::class, class_uses_recursive($indexConfigurator))) {
21+
$bulkPayload->useAlias('write');
22+
}
1523

1624
$models->each(function ($model) use ($bulkPayload) {
1725
$modelData = $model->toSearchableArray();
@@ -20,19 +28,11 @@ public function update(Collection $models)
2028
return true;
2129
}
2230

23-
$indexConfigurator = $model->getIndexConfigurator();
24-
2531
$actionPayload = (new RawPayload())
26-
->set('index._type', $model->searchableAs())
2732
->set('index._id', $model->getKey());
2833

29-
if (in_array(Migratable::class, class_uses_recursive($indexConfigurator))) {
30-
$actionPayload->set('index._index', $indexConfigurator->getWriteAlias());
31-
} else {
32-
$actionPayload->set('index._index', $indexConfigurator->getName());
33-
}
34-
35-
$bulkPayload->add('body', $actionPayload->get())
34+
$bulkPayload
35+
->add('body', $actionPayload->get())
3636
->add('body', $modelData);
3737
});
3838

@@ -41,14 +41,12 @@ public function update(Collection $models)
4141

4242
public function delete(Collection $models)
4343
{
44-
$bulkPayload = new RawPayload();
44+
$model = $models->first();
4545

46-
$models->each(function ($model) use ($bulkPayload) {
47-
$indexConfigurator = $model->getIndexConfigurator();
46+
$bulkPayload = new TypePayload($model);
4847

48+
$models->each(function ($model) use ($bulkPayload) {
4949
$actionPayload = (new RawPayload())
50-
->set('delete._index', $indexConfigurator->getName())
51-
->set('delete._type', $model->searchableAs())
5250
->set('delete._id', $model->getKey());
5351

5452
$bulkPayload->add('body', $actionPayload->get());

src/Payloads/RawPayload.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public function setIfNotEmpty($key, $value)
2424
return $this->set($key, $value);
2525
}
2626

27+
public function has($key)
28+
{
29+
return array_has($this->payload, $key);
30+
}
31+
2732
public function add($key, $value)
2833
{
2934
if (!is_null($key)) {

tests/ElasticEngineTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function test_if_the_update_method_with_bulk_indexer_builds_correct_paylo
7878
$this->mockClient()
7979
->shouldReceive('bulk')
8080
->with([
81+
'index' => 'test_index_write',
82+
'type' => 'test_table',
8183
'body' => [
8284
[
8385
'index' => [
84-
'_index' => 'test_index_write',
85-
'_type' => 'test_table',
8686
'_id' => 1
8787
]
8888
],
@@ -109,11 +109,11 @@ public function test_if_the_delete_method_with_bulk_indexer_builds_correct_paylo
109109
$this->mockClient()
110110
->shouldReceive('bulk')
111111
->with([
112+
'index' => 'test_index',
113+
'type' => 'test_table',
112114
'body' => [
113115
[
114116
'delete' => [
115-
'_index' => 'test_index',
116-
'_type' => 'test_table',
117117
'_id' => 1
118118
]
119119
]

0 commit comments

Comments
 (0)