# How to get existing index object, if index exists in php elastica

**URL:** <https://discuss.elastic.co/t/how-to-get-existing-index-object-if-index-exists-in-php-elastica/22670>\
**Category:** Elasticsearch\
**Created:** [March 15, 2015, 10:00am UTC](https://discuss.elastic.co/t/how-to-get-existing-index-object-if-index-exists-in-php-elastica/22670 "2015-03-15T10:00:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitin\_birdi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nitin_birdi/32/835_2.png) [@nitin\_birdi](https://discuss.elastic.co/u/nitin_birdi)\
**Post date:** [March 15, 2015, 10:00am UTC](https://discuss.elastic.co/t/how-to-get-existing-index-object-if-index-exists-in-php-elastica/22670/1 "2015-03-15T10:00:01Z")

</div>

Hi,

I am new to elastic search. I am using php elastica client and facing a  
problem:  
If an index exists, I want to get the object of this existing index and not  
recreate it. How can this be done?

client = new \Elastica\Client($arrServerConf, $callback);

if ( $client-\>getIndex($name)-\>exists() ) {  
//do something here to get this existing object -- what to do  
here???  
} else {  
// create a new one  
$index = $client-\>getIndex($name);  
$index-\>create(array('index' =\> array('number\_of\_shards' =\>  
$shards, 'number\_of\_replicas' =\> 0)), $delete);  
}  
$type = $index-\>getType($typeName);

Or is there some other way of doing this?  
I need this because, I'll be adding documents in this index and searching  
among them.

I think its a very trivial task, should have easily found a fix, but am  
unable to fix this. Hope you guys will be kind enough to help me out.

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/da2f6e7a-5f5b-4cda-b602-6e5ea362d480%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/da2f6e7a-5f5b-4cda-b602-6e5ea362d480%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nitin\_birdi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nitin_birdi/32/835_2.png) [@nitin\_birdi](https://discuss.elastic.co/u/nitin_birdi)\
**Post date:** [March 15, 2015, 12:22pm UTC](https://discuss.elastic.co/t/how-to-get-existing-index-object-if-index-exists-in-php-elastica/22670/2 "2015-03-15T12:22:18Z")

</div>

My bad. It was so easy: $index = $client-\>getIndex($name); Had some problem  
in my code itself.  
Thanks anyways.

On Sunday, March 15, 2015 at 3:30:01 PM UTC+5:30, nitin birdi wrote:

> Hi,
> 
> I am new to Elasticsearch. I am using php elastica client and facing a  
> problem:  
> If an index exists, I want to get the object of this existing index and  
> not recreate it. How can this be done?
> 
> client = new \Elastica\Client($arrServerConf, $callback);
> 
> if ( $client-\>getIndex($name)-\>exists() ) {  
> //do something here to get this existing object -- what to do  
> here???  
> } else {  
> // create a new one  
> $index = $client-\>getIndex($name);  
> $index-\>create(array('index' =\> array('number\_of\_shards' =\>  
> $shards, 'number\_of\_replicas' =\> 0)), $delete);  
> }  
> $type = $index-\>getType($typeName);
> 
> Or is there some other way of doing this?  
> I need this because, I'll be adding documents in this index and searching  
> among them.
> 
> I think its a very trivial task, should have easily found a fix, but am  
> unable to fix this. Hope you guys will be kind enough to help me out.
> 
> Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2849a69e-f289-42e1-8e3b-4ac4fd1f817f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2849a69e-f289-42e1-8e3b-4ac4fd1f817f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:26am UTC](https://discuss.elastic.co/t/how-to-get-existing-index-object-if-index-exists-in-php-elastica/22670/3 "2017-07-06T00:26:42Z")

</div>


