UpgradeSimIndex.php 1.4 KB

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