# Create index with multiple different types / add new type to existing index via JavaAPI

**URL:** <https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110>\
**Category:** Elasticsearch\
**Created:** [October 25, 2013, 9:43am UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110 "2013-10-25T09:43:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Kley](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Alexander\_Kley](https://discuss.elastic.co/u/Alexander_Kley)\
**Post date:** [October 25, 2013, 9:43am UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/1 "2013-10-25T09:43:28Z")

</div>

Hi guys,

1. 

is it possible to create an index with more than one type? Things like this  
don't work and thrown a 'Index allready exists':

// create index with more than one type with default mapping  
public void createIndex(final String index, final Map\<String, Object\>  
indexSetting, final String... types) throws IndexCreateException  
{  
final Map\<String, Object\> indexSettingTmp = indexSetting == null ? new  
HashMap\<String, Object\>() : indexSetting;  
final Settings settings = getImmutableSettings(indexSettingTmp);

for (final String type : types)  
{

esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,  
null).execute().actionGet();  
}

}

1. 

is it possible to to add new type(s) to an existing index via Java API. I  
cant find any examples for Java API

Thanks in advance  
Alex

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Josh\_Canfield](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/josh_canfield/32/1705_2.png) [@Josh\_Canfield](https://discuss.elastic.co/u/Josh_Canfield)\
**Post date:** [October 25, 2013, 5:20pm UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/2 "2013-10-25T17:20:05Z")

</div>

I don't know the java api, I'm strictly using the REST, but you are calling  
create index for every type which of course is giving you "index already  
exists"

Have you considered not calling execute() until you've added all the  
mappings?

On Fri, Oct 25, 2013 at 2:43 AM, Alex [alexander.kley@gmail.com](mailto:alexander.kley@gmail.com) wrote:

> Hi guys,
> 
> 1. 
> 
> is it possible to create an index with more than one type? Things like  
> this don't work and thrown a 'Index allready exists':
> 
> // create index with more than one type with default mapping  
> public void createIndex(final String index, final Map\<String, Object\>  
> indexSetting, final String... types) throws IndexCreateException  
> {  
> final Map\<String, Object\> indexSettingTmp = indexSetting == null ? new  
> HashMap\<String, Object\>() : indexSetting;  
> final Settings settings = getImmutableSettings(indexSettingTmp);
> 
> for (final String type : types)  
> {
> 
> esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,  
> null).execute().actionGet();  
> }
> 
> }
> 
> 1. 
> 
> is it possible to to add new type(s) to an existing index via Java API. I  
> cant find any examples for Java API
> 
> Thanks in advance  
> Alex
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Alexander\_Kley](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Alexander\_Kley](https://discuss.elastic.co/u/Alexander_Kley)\
**Post date:** [October 25, 2013, 5:41pm UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/3 "2013-10-25T17:41:07Z")

</div>

Hi Josh,

thats a good hint! I'll try to add .setType() in a for-loop and after this  
the execute() will be append. Good idea to create multiple types in one  
request. But how can i add further types on existing indexes? Via REST it  
seems to be quite simple. But how can I do this via Java API?  
Have anybody ideas?

regards,  
Alex

Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:

> I don't know the java api, I'm strictly using the REST, but you are  
> calling create index for every type which of course is giving you "index  
> already exists"
> 
> Have you considered not calling execute() until you've added all the  
> mappings?
> 
> On Fri, Oct 25, 2013 at 2:43 AM, Alex \<[alexand...@gmail.com](mailto:alexand...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi guys,
> > 
> > 1. 
> > 
> > is it possible to create an index with more than one type? Things like  
> > this don't work and thrown a 'Index allready exists':
> > 
> > // create index with more than one type with default mapping  
> > public void createIndex(final String index, final Map\<String, Object\>  
> > indexSetting, final String... types) throws IndexCreateException  
> > {  
> > final Map\<String, Object\> indexSettingTmp = indexSetting == null ? new  
> > HashMap\<String, Object\>() : indexSetting;  
> > final Settings settings = getImmutableSettings(indexSettingTmp);
> > 
> > for (final String type : types)  
> > {
> > 
> > esClient.admin().indices().prepareCreate(index).setSettings(settings).addMapping(type,  
> > null).execute().actionGet();  
> > }
> > 
> > }
> > 
> > 1. 
> > 
> > is it possible to to add new type(s) to an existing index via Java API. I  
> > cant find any examples for Java API
> > 
> > Thanks in advance  
> > Alex
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 25, 2013, 10:22pm UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/4 "2013-10-25T22:22:36Z")

</div>

I have never created a new type via the Java API, so I can't easily answer  
your question, but if you ever need to find the Java equivalent of a REST  
call, here are two suggestions:

1. Look for the Java endpoint of the REST call. It will be in  
the org.elasticsearch.rest.action package and will usually have a  
recognizable name that begins with Rest, such as RestCreateIndexAction. An  
IDE with class name completion helps.
2. Look at the tests. The majority of elasticsearch tasks are tested via  
numerous unit and integration tests. Good place to steal, I mean borrow,  
code from. 🙂

Give a man a fish, and you feed him for a day; show him how to catch fish,  
and you feed him for a lifetime. 🙂

Cheers,

Ivan

On Fri, Oct 25, 2013 at 10:41 AM, Alexander Kley  
[alexander.kley@gmail.com](mailto:alexander.kley@gmail.com)wrote:

> Hi Josh,
> 
> thats a good hint! I'll try to add .setType() in a for-loop and after this  
> the execute() will be append. Good idea to create multiple types in one  
> request. But how can i add further types on existing indexes? Via REST it  
> seems to be quite simple. But how can I do this via Java API?  
> Have anybody ideas?
> 
> regards,  
> Alex
> 
> Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:
> 
> > I don't know the java api, I'm strictly using the REST, but you are  
> > calling create index for every type which of course is giving you "index  
> > already exists"
> > 
> > Have you considered not calling execute() until you've added all the  
> > mappings?
> > 
> > On Fri, Oct 25, 2013 at 2:43 AM, Alex [alexand...@gmail.com](mailto:alexand...@gmail.com) wrote:
> > 
> > > Hi guys,
> > > 
> > > 1. 
> > > 
> > > is it possible to create an index with more than one type? Things like  
> > > this don't work and thrown a 'Index allready exists':
> > > 
> > > // create index with more than one type with default mapping  
> > > public void createIndex(final String index, final Map\<String, Object\>  
> > > indexSetting, final String... types) throws IndexCreateException  
> > > {  
> > > final Map\<String, Object\> indexSettingTmp = indexSetting == null ? new  
> > > HashMap\<String, Object\>() : indexSetting;  
> > > final Settings settings = getImmutableSettings(\*\*indexSettingTmp);
> > > 
> > > for (final String type : types)  
> > > {  
> > > esClient.admin().indices().**prepareCreate(index).**  
> > > setSettings(settings).\*\*addMapping(type, null).execute().actionGet();  
> > > }
> > > 
> > > }
> > > 
> > > 1. 
> > > 
> > > is it possible to to add new type(s) to an existing index via Java API.  
> > > I cant find any examples for Java API
> > > 
> > > Thanks in advance  
> > > Alex
> > > 
> > > --  
> > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > 
> > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > .
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 25, 2013, 11:24pm UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/5 "2013-10-25T23:24:07Z")

</div>

1. You use the addMapping() method in CreateIndexRequestBuilder with a null  
argument for the mapping. This will not work.

2. Use the PutMappingRequest

Jörg

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Alexander\_Kley](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@Alexander\_Kley](https://discuss.elastic.co/u/Alexander_Kley)\
**Post date:** [October 28, 2013, 11:00am UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/6 "2013-10-28T11:00:04Z")

</div>

Many thanks to you Ivan, also for the hint to borro code 😉 !

Cheers  
Alex

Am Samstag, 26. Oktober 2013 00:22:36 UTC+2 schrieb Ivan Brusic:

> I have never created a new type via the Java API, so I can't easily answer  
> your question, but if you ever need to find the Java equivalent of a REST  
> call, here are two suggestions:
> 
> 1. Look for the Java endpoint of the REST call. It will be in  
> the org.elasticsearch.rest.action package and will usually have a  
> recognizable name that begins with Rest, such as RestCreateIndexAction. An  
> IDE with class name completion helps.
> 2. Look at the tests. The majority of elasticsearch tasks are tested via  
> numerous unit and integration tests. Good place to steal, I mean borrow,  
> code from. 🙂
> 
> Give a man a fish, and you feed him for a day; show him how to catch fish,  
> and you feed him for a lifetime. 🙂
> 
> Cheers,
> 
> Ivan
> 
> On Fri, Oct 25, 2013 at 10:41 AM, Alexander Kley \<[alexand...@gmail.com](mailto:alexand...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi Josh,
> > 
> > thats a good hint! I'll try to add .setType() in a for-loop and after  
> > this the execute() will be append. Good idea to create multiple types in  
> > one request. But how can i add further types on existing indexes? Via REST  
> > it seems to be quite simple. But how can I do this via Java API?  
> > Have anybody ideas?
> > 
> > regards,  
> > Alex
> > 
> > Am Freitag, 25. Oktober 2013 19:20:05 UTC+2 schrieb Josh Canfield:
> > 
> > > I don't know the java api, I'm strictly using the REST, but you are  
> > > calling create index for every type which of course is giving you "index  
> > > already exists"
> > > 
> > > Have you considered not calling execute() until you've added all the  
> > > mappings?
> > > 
> > > On Fri, Oct 25, 2013 at 2:43 AM, Alex [alexand...@gmail.com](mailto:alexand...@gmail.com) wrote:
> > > 
> > > > Hi guys,
> > > > 
> > > > 1. 
> > > > 
> > > > is it possible to create an index with more than one type? Things like  
> > > > this don't work and thrown a 'Index allready exists':
> > > > 
> > > > // create index with more than one type with default mapping  
> > > > public void createIndex(final String index, final Map\<String, Object\>  
> > > > indexSetting, final String... types) throws IndexCreateException  
> > > > {  
> > > > final Map\<String, Object\> indexSettingTmp = indexSetting == null ?  
> > > > new HashMap\<String, Object\>() : indexSetting;  
> > > > final Settings settings = getImmutableSettings(\*\*indexSettingTmp);
> > > > 
> > > > for (final String type : types)  
> > > > {  
> > > > esClient.admin().indices().**prepareCreate(index).**  
> > > > setSettings(settings).\*\*addMapping(type, null).execute().actionGet();  
> > > > }
> > > > 
> > > > }
> > > > 
> > > > 1. 
> > > > 
> > > > is it possible to to add new type(s) to an existing index via Java API.  
> > > > I cant find any examples for Java API
> > > > 
> > > > Thanks in advance  
> > > > Alex
> > > > 
> > > > --  
> > > > 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 elasticsearc...@\*\*[googlegroups.com](http://googlegroups.com).
> > > > 
> > > > For more options, visit [https://groups.google.com/\*\*groups/opt\_out](https://groups.google.com/**groups/opt_out)[https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out)  
> > > > .
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:10am UTC](https://discuss.elastic.co/t/create-index-with-multiple-different-types-add-new-type-to-existing-index-via-javaapi/14110/7 "2017-07-06T02:10:15Z")

</div>


