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

Commit 91a1e1b

Browse files
committed
Added refresh option for document updates
1 parent 14cf9cd commit 91a1e1b

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Option | Description
7272
client | A setting hash to build Elasticsearch client. More information you can find [here](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_configuration.html#_building_the_client_from_a_configuration_hash). By default the host is set to `localhost:9200`.
7373
update_mapping | The option that specifies whether to update a mapping automatically or not. By default it is set to `true`.
7474
indexer | Set to `single` for the single document indexing and to `bulk` for the bulk document indexing. By default is set to `single`.
75+
document_refresh | This option controls when updated documents appear in the search results. Can be set to `'true'`, `'false'`, `'wait_for'` or `null`. More details about this option you can find [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html). By default set to `null`.
7576

7677
Note, that if you use the bulk document indexing you'll probably want to change the chunk size, you can do that in the `config/scout.php` file.
7778

config/scout_elastic.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
]
88
],
99
'update_mapping' => env('SCOUT_ELASTIC_UPDATE_MAPPING', true),
10-
'indexer' => env('SCOUT_ELASTIC_INDEXER', 'single')
10+
'indexer' => env('SCOUT_ELASTIC_INDEXER', 'single'),
11+
'document_refresh' => env('SCOUT_ELASTIC_DOCUMENT_REFRESH')
1112
];

src/Indexers/BulkIndexer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function update(Collection $models)
2121
$bulkPayload->useAlias('write');
2222
}
2323

24+
if ($documentRefresh = config('scout_elastic.document_refresh')) {
25+
$bulkPayload->set('refresh', $documentRefresh);
26+
}
27+
2428
$models->each(function ($model) use ($bulkPayload) {
2529
if ($model->usesSoftDelete() && config('scout.soft_delete', false)) {
2630
$model->pushSoftDeleteMetadata();

src/Indexers/SingleIndexer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function update(Collection $models)
3434
$payload->useAlias('write');
3535
}
3636

37+
if ($documentRefresh = config('scout_elastic.document_refresh')) {
38+
$payload->set('refresh', $documentRefresh);
39+
}
40+
3741
ElasticClient::index($payload->get());
3842
});
3943
}

tests/Indexers/BulkIndexerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ public function testUpdateWithEnabledSoftDelete()
5858
$this->addToAssertionCount(1);
5959
}
6060

61+
public function testUpdateWithSpecifiedDocumentRefreshOption()
62+
{
63+
Config::set('scout_elastic.document_refresh', 'true');
64+
65+
ElasticClient
66+
::shouldReceive('bulk')
67+
->once()
68+
->with([
69+
'index' => 'test',
70+
'type' => 'test',
71+
'refresh' => 'true',
72+
'body' => [
73+
['index' => ['_id' => 1]],
74+
['name' => 'foo'],
75+
['index' => ['_id' => 2]],
76+
['name' => 'bar']
77+
]
78+
]);
79+
80+
(new BulkIndexer())
81+
->update($this->models);
82+
83+
$this->addToAssertionCount(1);
84+
}
85+
6186
public function testDelete()
6287
{
6388
ElasticClient

tests/Indexers/SingleIndexerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,40 @@ public function testUpdateWithEnabledSoftDelete()
8484
$this->addToAssertionCount(1);
8585
}
8686

87+
public function testUpdateWithSpecifiedDocumentRefreshOption()
88+
{
89+
Config::set('scout_elastic.document_refresh', 'true');
90+
91+
ElasticClient
92+
::shouldReceive('index')
93+
->once()
94+
->with([
95+
'index' => 'test',
96+
'type' => 'test',
97+
'refresh' => 'true',
98+
'id' => 1,
99+
'body' => [
100+
'name' => 'foo'
101+
]
102+
])
103+
->shouldReceive('index')
104+
->once()
105+
->with([
106+
'index' => 'test',
107+
'type' => 'test',
108+
'refresh' => 'true',
109+
'id' => 2,
110+
'body' => [
111+
'name' => 'bar'
112+
]
113+
]);
114+
115+
(new SingleIndexer())
116+
->update($this->models);
117+
118+
$this->addToAssertionCount(1);
119+
}
120+
87121
public function testDelete()
88122
{
89123
ElasticClient

0 commit comments

Comments
 (0)