/**
  * @param $study
  * @return the seven most similar studies based on subjects
  */
 public static function thematicallySimilarQuery($resource)
 {
     $json = '{
     "query": {
       "bool": {
         "must_not": {
           "match": {
             "_id": "' . $resource['_id'] . '"
           }
         },
         "should": [';
     $firstMatch = true;
     if (array_key_exists('nativeSubject', $resource['_source'])) {
         foreach ($resource['_source']['keyword']['en'] as $subject) {
             if ($firstMatch) {
                 $firstMatch = false;
             } else {
                 $json .= ',';
             }
             $json .= '{
           "match": {
             "keyword.en": "' . $subject . '"
           }
         }';
         }
     }
     $json .= '],
           "minimum_should_match" : 1
         }
       }
     }';
     $params = ['index' => 'study', 'type' => 'studytype', 'size' => 7, 'body' => $json];
     $result = ElasticSearch::getClient()->search($params);
     return $result['hits']['hits'];
 }
 /**
  * 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);
 }
 /**
  * Performs an auto completion request
  * @return mixed
  */
 public function suggest()
 {
     $text = '';
     if (Request::has('text')) {
         $text = Request::get('text');
     }
     $result = ElasticSearch::suggest($text);
     return $result;
 }
 /**
  * Deletes the index specified
  * @return mixed
  */
 public function handle()
 {
     $index = $this->argument('index');
     $this->info(PHP_EOL . 'Using index: ' . $index . PHP_EOL);
     if (isset($index)) {
         $result = ElasticSearch::deleteIndex($index);
         $this->comment($result);
     } else {
         $this->comment('No index given');
     }
 }