UpgradeSimIndex.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\SentSim;
  5. use App\Models\SentSimIndex;
  6. use Illuminate\Support\Facades\DB;
  7. class UpgradeSimIndex extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:sim.index';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '刷新相似句索引';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. SentSimIndex::where('id','>',0)->delete();
  38. $result = DB::select('select count(*) from (select sent1 from sent_sims where sim>0.7 group by sent1) T');
  39. $bar = $this->output->createProgressBar($result[0]->count);
  40. foreach (SentSim::selectRaw('sent1,count(*)')->where('sim','>',0.7)->groupBy('sent1')->cursor() as $sent) {
  41. SentSimIndex::insert(
  42. ['sent_id'=>$sent->sent1,
  43. 'count'=>$sent->count,]);
  44. $bar->advance();
  45. }
  46. $bar->finish();
  47. return 0;
  48. }
  49. }