From f9ffd91620e596b2cdbb299cc615d71897749674 Mon Sep 17 00:00:00 2001 From: Abdullah Almsaeed Date: Fri, 20 Apr 2018 10:05:00 -0400 Subject: [PATCH] Add exception to remove blast database nodes --- docker-compose.yml | 2 +- includes/Elasticsearch/ESInstance.php | 9 ++++- includes/Jobs/NodesIndexJob.php | 3 +- tripal_elasticsearch.install | 50 +++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 3dc87324..1be78548 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,4 +11,4 @@ services: elasticsearch: image: 'elasticsearch:5' ports: - - "9200:9200" + - "9201:9200" diff --git a/includes/Elasticsearch/ESInstance.php b/includes/Elasticsearch/ESInstance.php index 3fe892e0..5112a11a 100644 --- a/includes/Elasticsearch/ESInstance.php +++ b/includes/Elasticsearch/ESInstance.php @@ -293,18 +293,25 @@ public function setIndexParams($index_name, $shards = 5, $replicas = 0, * Perform the actual search. * Use this function after setting the search params. * + * @param bool $return_source whether to format the results or not. + * * @see \ESInstance::setTableSearchParams() * @see \ESInstance::setWebsiteSearchParams() * * @return array * @throws \Exception */ - public function search() { + public function search($return_source = false) { if (empty($this->searchParams)) { throw new Exception('Please build search parameters before attempting to search.'); } $hits = $this->client->search($this->searchParams); + + if($return_source) { + return $hits; + } + $results = []; foreach ($hits['hits']['hits'] as $hit) { if (isset($hit['highlight'])) { diff --git a/includes/Jobs/NodesIndexJob.php b/includes/Jobs/NodesIndexJob.php index 3f51820b..72efe673 100644 --- a/includes/Jobs/NodesIndexJob.php +++ b/includes/Jobs/NodesIndexJob.php @@ -194,6 +194,7 @@ public function total() { * @return int */ public function count() { - return db_query('SELECT COUNT(nid) FROM {node} WHERE status=1')->fetchField(); + return db_query('SELECT COUNT(nid) FROM {node} N + WHERE status=1 AND type != :type', [':type' => 'blastdb'])->fetchField(); } } diff --git a/tripal_elasticsearch.install b/tripal_elasticsearch.install index 2f839bb5..7ee0d998 100644 --- a/tripal_elasticsearch.install +++ b/tripal_elasticsearch.install @@ -134,13 +134,13 @@ function tripal_elasticsearch_uninstall() { // Auto drop anything in the schema foreach ($schema as $table => $fields) { - if(db_table_exists($table)) { + if (db_table_exists($table)) { db_drop_table($table); } } // Drop tables that are created in the update functions - if(db_table_exists('tripal_elasticsearch_indices')) { + if (db_table_exists('tripal_elasticsearch_indices')) { db_drop_table('tripal_elasticsearch_indices'); } } @@ -380,3 +380,49 @@ function tripal_elasticsearch_update_7209() { throw $exception; } } + +/** + * Remove blast database content type from website index. + * + * @throws \Exception + */ +function tripal_elasticsearch_update_7210() { + try { + $es = new ESInstance(); + $count = $es->setWebsiteSearchParams('*', 'Blast Database', 'website') + ->count(); + $client = $es->client; + + if ($count === 0) { + print "No blast database content was found\n"; + return; + } + + for ($i = 0; $i < $count; $i += 100) { + $results = $es->setWebsiteSearchParams('*', 'Blast Database', 'website', 'website', [ + $i, + 100, + ])->search(FALSE); + foreach ($results as $result) { + $client->deleteByQuery([ + 'index' => 'website', + 'type' => 'website', + 'body' => [ + 'query' => [ + 'match' => [ + 'nid' => $result['nid'], + ], + ], + ], + ]); + } + } + + print "Successfully removed all blast database nodes from website index.\n"; + } catch (Exception $exception) { + print "ERROR: failed to remove blast database nodes from website index due to the following exception\n"; + print $exception->getMessage(); + + throw $exception; + } +}