Browse Source

add strlen

visuddhinanda 3 years ago
parent
commit
cb5d1c71cb

+ 9 - 4
app/Console/Commands/UpgradeDictVocabulary.php

@@ -42,15 +42,20 @@ class UpgradeDictVocabulary extends Command
     {
         $words = UserDict::where('source','_PAPER_')->selectRaw('word,count(*)')->groupBy('word')->cursor();
 
-		$bar = $this->output->createProgressBar(200000);
+		$bar = $this->output->createProgressBar(230000);
 		foreach ($words as $word) {
-			$update = Vocabulary::where('word',$word->word)->updateOrCreate(
-                ['count' => $word->count, 'flag' => 1],
-                ['word' => $word->word, 'word_en'=>Tools::getWordEn($word->word)]
+			$update = Vocabulary::firstOrNew(
+                ['word' => $word->word],
+                ['word_en'=>Tools::getWordEn($word->word)]
             );
+            $update->count = $word->count;
+            $update->flag = 1;
+            $update->strlen = mb_strlen($word->word,"UTF-8");
+            $update->save();
             $bar->advance();
 		}
         $bar->finish();
+        Vocabulary::where('flag',0)->delete();
         return 0;
     }
 }

+ 5 - 2
app/Http/Controllers/VocabularyController.php

@@ -6,6 +6,7 @@ use App\Models\Vocabulary;
 use Illuminate\Http\Request;
 use App\Http\Resources\VocabularyResource;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
 
 class VocabularyController extends Controller
 {
@@ -20,9 +21,11 @@ class VocabularyController extends Controller
         switch ($request->get("view")) {
             case 'key':
                 $key = $request->get("key");
-                $result = Cache::remember("/dict_vocabulary/{$key}",60,function() use($key){
-                    return Vocabulary::where('word','like',$key."%")
+                $result = Cache::remember("/dict_vocabulary/{$key}",10,function() use($key){
+                        return Vocabulary::whereRaw('word like ? or word_en like ?',[$key."%",$key."%"])
                                     ->whereOr('word_en','like',$key."%")
+                                    ->orderBy('strlen')
+                                    ->orderBy('word')
                                     ->take(10)->get();
                 });
                 return $this->ok(['rows'=>VocabularyResource::collection($result),'count'=>count($result)]);

+ 36 - 0
database/migrations/2023_04_05_132106_add_strlen_in_vocabularies.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddStrlenInVocabularies extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('vocabularies', function (Blueprint $table) {
+            //
+            $table->integer('strlen')->index()->default(0);
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('vocabularies', function (Blueprint $table) {
+            //
+            $table->dropColumn('strlen');
+
+        });
+    }
+}