/** * Execute the console command. * * @return mixed */ public function handle(TranslateContract $translator) { $path = $this->argument('path'); if (!isset($path) || !file_exists($path)) { $path = env('XSLT_OUT_PATH'); } $this->info(PHP_EOL . 'Using directory path for ingest: ' . $path . PHP_EOL); // create index $index = env('ES_STUDY_UNIT_INDEX'); $type = env('ES_STUDY_INDEX_TYPE'); $this->indexIsCreated = ElasticSearch::isIndexCreated($index); if (!$this->indexIsCreated) { ElasticSearch::createIndexFromJsonFile(); } $errors = array(); $files = array_diff(scandir($path), array('..', '.')); foreach ($files as $file) { $text = file_get_contents($path . $file); $body = json_decode($text, true); if (!isset($body)) { // not conform json! array_push($errors, $path . $file . ' - JSON error'); continue; } $id = $body['id']; if (array_key_exists('startdate', $body)) { $body['startdate'] = Utils::fixDate($body['startdate']); } if (array_key_exists('enddate', $body)) { $body['enddate'] = Utils::fixDate($body['enddate']); } $body = HarminizationHelper::harmonizeDocument($body); if (env('TRANSLATE_ON_INGEST') == 1) { $translatorHelper = new TranslateHelper(); $body = $translatorHelper->translate($body, $translator); } $this->comment('Importing: ' . $path . $file); $result = ElasticSearch::indexDocument($id, $index, $type, $body); if (!isset($result)) { // not conform json! array_push($errors, $path . $file . ' - ES ingest error'); continue; } \Log::debug(json_encode($result)); if ($result['created']) { $this->comment(' - result: Created'); } if (intval($result['_version']) > 1) { $this->comment(' - result: Updated to version: ' . $result['_version']); } } $this->info(PHP_EOL . 'Result: '); $this->info('Files to ingest: ' . sizeof($files)); $this->info('Failed: ' . sizeof($errors)); $this->comment(PHP_EOL . 'Error files:'); foreach ($errors as $error) { $this->comment('File: ' . $error); } }
/** * @test */ function createIndexFromJsonFile() { // clean up $exitCode = Artisan::call('es:delete-index', ['index' => 'study']); ElasticSearch::createIndexFromJsonFile(); $result = ElasticSearch::isIndexCreated('study'); $this->assertTrue($result); }