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

Commit 20b615c

Browse files
committed
Fixed migration command
1 parent a386b4b commit 20b615c

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/Console/ElasticMigrateCommand.php

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,25 @@ protected function isAliasExists($name)
177177
}
178178

179179
/**
180-
* @param string $name
180+
* @param $name
181+
* @return array
181182
*/
182-
protected function deleteAlias($name)
183+
protected function getAlias($name)
183184
{
184-
$indices = ElasticClient::indices();
185-
186185
$getPayload = (new RawPayload())
187186
->set('name', $name)
188187
->get();
189188

190-
$aliases = $indices->getAlias($getPayload);
189+
return ElasticClient::indices()
190+
->getAlias($getPayload);
191+
}
192+
193+
/**
194+
* @param string $name
195+
*/
196+
protected function deleteAlias($name)
197+
{
198+
$aliases = $this->getAlias($name);
191199

192200
if (empty($aliases)) {
193201
return;
@@ -199,7 +207,8 @@ protected function deleteAlias($name)
199207
->set('name', $name)
200208
->get();
201209

202-
$indices->deleteAlias($deletePayload);
210+
ElasticClient::indices()
211+
->deleteAlias($deletePayload);
203212

204213
$this->info(sprintf(
205214
'The %s alias for the %s index was deleted.',
@@ -247,19 +256,38 @@ protected function importDocumentsToTargetIndex()
247256

248257
protected function deleteSourceIndex()
249258
{
250-
$sourceIndexConfigurator = $this->getModel()
259+
$sourceIndexConfigurator = $this
260+
->getModel()
251261
->getIndexConfigurator();
252262

253-
$payload = (new IndexPayload($sourceIndexConfigurator))
254-
->get();
263+
if ($this->isAliasExists($sourceIndexConfigurator->getName())) {
264+
$aliases = $this->getAlias($sourceIndexConfigurator->getName());
255265

256-
ElasticClient::indices()
257-
->delete($payload);
266+
foreach ($aliases as $index => $alias) {
267+
$payload = (new RawPayload())
268+
->set('index', $index)
269+
->get();
258270

259-
$this->info(sprintf(
260-
'The %s index was removed.',
261-
$sourceIndexConfigurator->getName()
262-
));
271+
ElasticClient::indices()
272+
->delete($payload);
273+
274+
$this->info(sprintf(
275+
'The %s index was removed.',
276+
$index
277+
));
278+
}
279+
} else {
280+
$payload = (new IndexPayload($sourceIndexConfigurator))
281+
->get();
282+
283+
ElasticClient::indices()
284+
->delete($payload);
285+
286+
$this->info(sprintf(
287+
'The %s index was removed.',
288+
$sourceIndexConfigurator->getName()
289+
));
290+
}
263291
}
264292

265293
public function handle()

src/Console/ElasticUpdateMappingCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use ScoutElastic\Console\Features\RequiresModelArgument;
88
use ScoutElastic\Facades\ElasticClient;
9+
use ScoutElastic\Migratable;
910
use ScoutElastic\Payloads\TypePayload;
1011

1112
class ElasticUpdateMappingCommand extends Command
@@ -39,12 +40,16 @@ public function handle()
3940
throw new LogicException('Nothing to update: the mapping is not specified.');
4041
}
4142

42-
$payload = (new TypePayload($model))
43-
->set('body.' . $model->searchableAs(), $mapping)
44-
->get();
43+
$payload = new TypePayload($model);
44+
45+
if (in_array(Migratable::class, class_uses_recursive($configurator))) {
46+
$payload->useAlias('write');
47+
}
48+
49+
$payload->set('body.' . $model->searchableAs(), $mapping);
4550

4651
ElasticClient::indices()
47-
->putMapping($payload);
52+
->putMapping($payload->get());
4853

4954
$this->info(sprintf(
5055
'The %s mapping was updated!',

0 commit comments

Comments
 (0)