diff --git a/src/Manager/Indices.php b/src/Manager/Indices.php index 099524e..2c474b0 100644 --- a/src/Manager/Indices.php +++ b/src/Manager/Indices.php @@ -34,6 +34,9 @@ class Indices { /** @var string|null */ private $stopwordFilterSlug = null; + /** @var string[]|null */ + private static $clusterIndices = null; + /** * @param Elasticsearch $elasticsearch * @param Indexables $indexables @@ -90,13 +93,46 @@ public function clearCurrentIndexLanguage() { $this->currentIndexLanguage = ''; } + /** + * @return string[] + */ + private function getClusterIndicesCache() { + if ( null === self::$clusterIndices ) { + $clusterIndices = $this->elasticsearch->get_cluster_indices(); + self::$clusterIndices = wp_list_pluck( $clusterIndices, 'index' ); + } + + return self::$clusterIndices; + } + + /** + * @param string $indexName + */ + private function saveIndexInClusterCache( $indexName ) { + if ( null === self::$clusterIndices ) { + self::$clusterIndices = []; + } + + if ( in_array( $indexName, self::$clusterIndices, true ) ) { + return; + } + + self::$clusterIndices[] = $indexName; + } + + private function clearClusterIndicesCache() { + self::$clusterIndices = null; + } + /** * @param string $indexName * * @return bool */ public function indexExists( $indexName ) { - return $this->elasticsearch->index_exists( $indexName ); + $clusterIndices = $this->getClusterIndicesCache(); + + return in_array( $indexName, $clusterIndices, true ); } /** @@ -155,6 +191,7 @@ public function generateIndexByIndexable( $indexable ) { ]; $this->elasticsearch->put_mapping( $indexName, $mapping ); + $this->saveIndexInClusterCache( $indexName ); } /** @@ -168,6 +205,7 @@ public function generateIndexableIndexes( $indexables ) { public function clearAllIndices() { $this->elasticsearch->delete_all_indices(); + $this->clearClusterIndicesCache(); } public function generateMissingIndices() {