Exemplo n.º 1
0
 /**
  * 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();
 }