/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $this->output->progressStart();
     $offset = 0;
     $media = Media::with('mediaType', 'scan', 'scan.specimen', 'scan.museum', 'scan.author', 'scan.animalGroup')->limit(self::BATCH_LIMIT)->offset($offset)->get()->toArray();
     while (!empty($media)) {
         foreach ($media as $key => $mediaArray) {
             $media[$key] = $this->flattenNested($mediaArray, true);
         }
         $this->output->progressAdvance(self::BATCH_LIMIT);
         $this->search->insertDocuments($media);
         $offset += self::BATCH_LIMIT;
         $media = Media::with('mediaType', 'scan', 'scan.specimen', 'scan.museum', 'scan.author', 'scan.animalGroup')->limit(self::BATCH_LIMIT)->offset($offset)->get()->toArray();
     }
     $offset = 0;
     $images = Image::with('scan', 'scan.specimen', 'scan.museum', 'scan.author', 'scan.animalGroup')->limit(self::BATCH_LIMIT)->offset($offset)->get()->toArray();
     while (!empty($images)) {
         foreach ($images as $key => $image) {
             $images[$key] = $this->flattenNested($image, true);
         }
         $this->output->progressAdvance(self::BATCH_LIMIT);
         $this->search->insertDocuments($images);
         $offset += self::BATCH_LIMIT;
         $images = Image::with('scan', 'scan.specimen', 'scan.museum', 'scan.author', 'scan.animalGroup')->limit(self::BATCH_LIMIT)->offset($offset)->get()->toArray();
     }
     $this->output->progressFinish();
 }