Exemple #1
0
 public function performPostInstallTasks()
 {
     $o_config = Configuration::load();
     if ($o_config->get('search_engine_plugin') == 'ElasticSearch') {
         require_once __CA_LIB_DIR__ . '/core/Plugins/SearchEngine/ElasticSearch.php';
         $o_es = new WLPlugSearchEngineElasticSearch();
         $o_es->refreshMapping(true);
     }
 }
Exemple #2
0
 public function flushContentBuffer()
 {
     foreach (WLPlugSearchEngineElasticSearch::$s_doc_content_buffer as $vs_key => $va_doc_content_buffer) {
         $va_post_json = array();
         $va_key = explode('/', $vs_key);
         foreach ($va_doc_content_buffer as $vs_field_name => $va_field_content) {
             foreach ($va_field_content as $vs_field_content) {
                 $va_post_json[$vs_field_name][] = $vs_field_content;
             }
         }
         if (!isset($va_doc_content_buffer[$va_key[0] . "." . $va_key[1]])) {
             /* add pk */
             $va_post_json[$va_key[1]] = $va_key[2];
         }
         // Output created on and modified on timestamps
         $qr_res = $this->opo_db->query("\n\t\t\t\tSELECT ccl.log_id, ccl.log_datetime, ccl.changetype, ccl.user_id\n\t\t\t\tFROM ca_change_log ccl\n\t\t\t\tWHERE\n\t\t\t\t\t(ccl.logged_table_num = ?) AND (ccl.logged_row_id = ?)\n\t\t\t\t\tAND\n\t\t\t\t\t(ccl.changetype <> 'D')\n\t\t\t", $this->opo_datamodel->getTableNum($va_key[0]), (int) $va_key[2]);
         while ($qr_res->nextRow()) {
             // We "fake" the <table>.<primary key> value here to be the log_id of the change log entry to ensure that the log entry
             // document has a different unique key than the entry for the actual record. If we didn't do this then we'd overwrite
             // the indexing for the record itself with indexing for successful log entries. Since the SearchEngine is looking for
             // just the primary key, sans table name, it's ok to do this hack.
             $va_post_json[$va_key[0] . "." . $va_key[1]] = $qr_res->get('log_id');
             $va_post_json[$va_key[1]] = $va_key[2];
             if ($qr_res->get('changetype') == 'I') {
                 $va_post_json["created"] = date("c", $qr_res->get('log_datetime'));
                 $va_post_json["created_user_id"] = $qr_res->get('user_id');
             } else {
                 $va_post_json["modified"] = date("c", $qr_res->get('log_datetime'));
                 $va_post_json["modified_user_id"] = $qr_res->get('user_id');
             }
         }
         $vo_http_client = new Zend_Http_Client();
         $vo_http_client->setUri($this->ops_elasticsearch_base_url . "/" . $this->ops_elasticsearch_index_name . "/" . $va_key[0] . "/" . $va_key[2]);
         try {
             $vo_http_client->setRawData(json_encode($va_post_json))->setEncType('text/json')->request('POST');
             $vo_http_response = $vo_http_client->request();
             if ($vo_http_response->getStatus() != 200) {
                 caLogEvent('ERR', _t('Indexing commit failed for %1; response was %2; request was %3', $vs_key, $vo_http_response->getBody(), json_encode($va_post_json)), 'ElasticSearch->flushContentBuffer()');
             }
         } catch (Exception $e) {
             caLogEvent('ERR', _t('Indexing commit failed for %1 with Exception: %2', $vs_key, $e->getMessage()), 'ElasticSearch->flushContentBuffer()');
         }
     }
     $this->opa_doc_content_buffer = array();
     WLPlugSearchEngineElasticSearch::$s_doc_content_buffer = array();
 }
 /**
  * Flush content buffer and write to index
  * @throws Elasticsearch\Common\Exceptions\NoNodesAvailableException
  */
 public function flushContentBuffer()
 {
     $this->refreshMapping();
     $va_bulk_params = array();
     // @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
     // @see https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_indexing_documents.html#_bulk_indexing
     // delete docs
     foreach (self::$s_delete_buffer as $vs_table_name => $va_rows) {
         foreach (array_unique($va_rows) as $vn_row_id) {
             $va_bulk_params['body'][] = array('delete' => array('_index' => $this->getIndexName(), '_type' => $vs_table_name, '_id' => $vn_row_id));
             // also make sure we don't do unessecary indexing for this record below
             unset(self::$s_update_content_buffer[$vs_table_name][$vn_row_id]);
         }
     }
     // newly indexed docs
     foreach (self::$s_doc_content_buffer as $vs_key => $va_doc_content_buffer) {
         $va_tmp = explode('/', $vs_key);
         $vs_table_name = $va_tmp[0];
         $vn_primary_key = intval($va_tmp[1]);
         $va_bulk_params['body'][] = array('index' => array('_index' => $this->getIndexName(), '_type' => $vs_table_name, '_id' => $vn_primary_key));
         // add changelog to index
         $va_doc_content_buffer = array_merge($va_doc_content_buffer, caGetChangeLogForElasticSearch($this->opo_db, $this->opo_datamodel->getTableNum($vs_table_name), $vn_primary_key));
         $va_bulk_params['body'][] = $va_doc_content_buffer;
     }
     // update existing docs
     foreach (self::$s_update_content_buffer as $vs_table_name => $va_rows) {
         foreach ($va_rows as $vn_row_id => $va_fragment) {
             $va_bulk_params['body'][] = array('update' => array('_index' => $this->getIndexName(), '_type' => $vs_table_name, '_id' => (int) $vn_row_id));
             // add changelog to fragment
             $va_fragment = array_merge($va_fragment, caGetChangeLogForElasticSearch($this->opo_db, $this->opo_datamodel->getTableNum($vs_table_name), $vn_row_id));
             $va_bulk_params['body'][] = array('doc' => $va_fragment);
         }
     }
     if (sizeof($va_bulk_params['body'])) {
         $this->getClient()->bulk($va_bulk_params);
         // we usually don't need indexing to be available *immediately* unless we're running automated tests of course :-)
         if (caIsRunFromCLI() && $this->getIndexName() && (!defined('__CollectiveAccess_IS_REINDEXING__') || !__CollectiveAccess_IS_REINDEXING__)) {
             $this->getClient()->indices()->refresh(array('index' => $this->getIndexName()));
         }
     }
     $this->opa_index_content_buffer = array();
     self::$s_doc_content_buffer = array();
     self::$s_update_content_buffer = array();
     self::$s_delete_buffer = array();
 }