Пример #1
0
 public function savePreferences($context)
 {
     $settings = array_map('trim', $context['settings']['elasticsearch']);
     $index_to_delete = NULL;
     $index_to_create = NULL;
     // index name has changed, so delete the original and create new
     if ($settings['index-name'] !== $settings['index_name_original']) {
         $index_to_delete = $settings['index_name_original'];
         $index_to_create = $settings['index-name'];
     }
     // only try to delete existing index if it exists (i.e. don't run this
     // the first time the extension is configured and there's no existing index)
     if (!empty($index_to_delete)) {
         // instantiate extension's ES helper class
         ElasticSearch::init($settings['host'], $index_to_delete, $settings['username'], $settings['password']);
         // delete original index
         $index = ElasticSearch::getClient()->getIndex($index_to_delete);
         if ($index->exists()) {
             $index->delete();
         }
         // reset
         ElasticSearch::flush();
     }
     if (!empty($index_to_create)) {
         // instantiate extension's ES helper class
         ElasticSearch::init($settings['host'], $index_to_create, $settings['username'], $settings['password']);
         // create new index
         $index = ElasticSearch::getClient()->getIndex($index_to_create);
         $index_settings_file = WORKSPACE . '/elasticsearch/index.json';
         $index_settings = array();
         if (file_exists($index_settings_file)) {
             $index_settings = json_decode(file_get_contents($index_settings_file), TRUE);
         } else {
             $index_settings = array();
         }
         if (is_null($index_settings)) {
             throw new Exception('ElasticSearch: workspace/elasticsearch/index.json is not valid JSON');
         }
         $index->create($index_settings, TRUE);
     }
     unset($context['settings']['elasticsearch']['index_name_original']);
 }