diff --git a/Elastica/Client.php b/Elastica/Client.php index 0db15b3..e921965 100644 --- a/Elastica/Client.php +++ b/Elastica/Client.php @@ -14,6 +14,13 @@ use FOS\ElasticaBundle\Logger\ElasticaLogger; */ class Client extends BaseClient { + /** + * Stores created indexes to avoid recreation. + * + * @var array + */ + private $indexCache = array(); + /** * {@inheritdoc} */ @@ -42,6 +49,10 @@ class Client extends BaseClient public function getIndex($name) { - return new Index($this, $name); + if (isset($this->indexCache[$name])) { + return $this->indexCache[$name]; + } + + return $this->indexCache[$name] = new Index($this, $name); } } diff --git a/Elastica/Index.php b/Elastica/Index.php index ec32c62..bf37c51 100644 --- a/Elastica/Index.php +++ b/Elastica/Index.php @@ -3,6 +3,7 @@ namespace FOS\ElasticaBundle\Elastica; use Elastica\Index as BaseIndex; +use Elastica\Type; /** * Overridden Elastica Index class that provides dynamic index name changes. @@ -13,6 +14,13 @@ class Index extends BaseIndex { private $originalName; + /** + * Stores created types to avoid recreation. + * + * @var array + */ + private $typeCache = array(); + /** * Returns the original name of the index if the index has been renamed for reindexing * or realiasing purposes. @@ -24,6 +32,15 @@ class Index extends BaseIndex return $this->originalName ?: $this->_name; } + public function getType($type) + { + if (isset($this->typeCache[$type])) { + return $this->typeCache[$type]; + } + + return $this->typeCache[$type] = parent::getType($type); + } + /** * Reassign index name *