visuddhinanda 1 day ago
parent
commit
715315ac47

+ 41 - 0
api-v12/app/Console/Commands/IndexOpenSearch.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Services\OpenSearchService;
+use Illuminate\Support\Facades\App;
+
+
+class IndexOpenSearch extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'app:index-open-search';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '如果是新的索引 全量更新openSearch';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        //
+        $service = app(OpenSearchService::class);
+        if ($service->count() === 0) {
+            $this->call('opensearch:index-pali');
+        } else {
+            if (App::environment('local')) {
+                $this->info('data exist');
+            }
+        }
+    }
+}

+ 33 - 0
api-v12/app/Console/Commands/PostInstall.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class DeplyInit extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'app:post-install';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        //
+        $this->info('deploy init start');
+        $this->call('create:opensearch.index');
+        $this->info('deploy init done');
+    }
+}

+ 71 - 0
api-v12/app/Console/Commands/UpdateOpenSearchIndex.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Services\OpenSearchService;
+use Illuminate\Console\Command;
+
+class UpdateOpenSearchIndex extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan create:opensearch.index
+     * @var string
+     */
+    protected $signature = 'update:opensearch.index';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $openSearch = app(OpenSearchService::class);
+
+        // Test OpenSearch connection
+        $open = $openSearch->testConnection();
+        if ($open[0]) {
+            $this->info($open[1]);
+        } else {
+            $this->error($open[1]);
+            return 1; // Exit with error code
+        }
+
+        // Attempt to create or update index
+        $this->warn('Index already exists, attempting to update...');
+        try {
+            $update = $openSearch->updateIndex();
+            if (!empty($update['settings']) && $update['settings']['acknowledged']) {
+                $this->info('Index settings updated successfully');
+            }
+            if (!empty($update['mappings']) && $update['mappings']['acknowledged']) {
+                $this->info('Index mappings updated successfully');
+            }
+            if (empty($update['settings']) && empty($update['mappings'])) {
+                $this->warn('No settings or mappings provided for update');
+            }
+        } catch (\Exception $updateException) {
+            $this->error('Failed to update index: ' . $updateException->getMessage());
+            return 1;
+        }
+        return 0;
+    }
+}