44
55use Illuminate \Console \Command ;
66use ScoutElastic \Facades \ElasticClient ;
7- use ScoutElastic \Payloads \IndexPayload ;
7+ use ScoutElastic \IndexConfigurator ;
8+ use ScoutElastic \Migratable ;
89use ScoutElastic \Console \Features \RequiresIndexConfiguratorArgument ;
10+ use ScoutElastic \Payloads \RawPayload ;
911
1012class ElasticIndexDropCommand extends Command
1113{
@@ -29,16 +31,38 @@ class ElasticIndexDropCommand extends Command
2931 public function handle ()
3032 {
3133 $ configurator = $ this ->getIndexConfigurator ();
34+ $ indexName = $ this ->resolveIndexName ($ configurator );
3235
33- $ payload = (new IndexPayload ($ configurator ))
36+ $ payload = (new RawPayload ())
37+ ->set ('index ' , $ indexName )
3438 ->get ();
3539
3640 ElasticClient::indices ()
3741 ->delete ($ payload );
3842
3943 $ this ->info (sprintf (
4044 'The index %s was deleted! ' ,
41- $ configurator -> getName ()
45+ $ indexName
4246 ));
4347 }
48+
49+ /**
50+ * @param IndexConfigurator $configurator
51+ * @return string
52+ */
53+ protected function resolveIndexName ($ configurator )
54+ {
55+ if (in_array (Migratable::class, class_uses_recursive ($ configurator ))) {
56+ $ payload = (new RawPayload ())
57+ ->set ('name ' , $ configurator ->getWriteAlias ())
58+ ->get ();
59+
60+ $ aliases = ElasticClient::indices ()
61+ ->getAlias ($ payload );
62+
63+ return key ($ aliases );
64+ } else {
65+ return $ configurator ->getName ();
66+ }
67+ }
4468}
0 commit comments