# Configuring number of shards and replica using Java API

**URL:** https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133
**Category:** Elasticsearch
**Created:** [September 25, 2012, 11:56am UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133 "2012-09-25T11:56:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Percival\_Xavier](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Percival\_Xavier](https://discuss.elastic.co/u/Percival_Xavier)
#### Post date: [September 25, 2012, 11:56am UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133/1 "2012-09-25T11:56:06Z")

</div>

Hello,

How do I configure the above using Java API just before the below  
statement is called ?

IndexResponse response = \_client.prepareIndex(obj.get\_srch\_index(),  
obj.get\_srch\_name(),obj.get\_srch\_Id())  
.setSource(sourceString)  
.execute()  
.actionGet();  
System.out.println(response.id());

Percival

--

---

<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: [September 25, 2012, 4:22pm UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133/2 "2012-09-25T16:22:56Z")

</div>

The number of shards and replicas are part of the source object you  
pass in. I prefer to use CreateIndexRequests over using the  
IndexRequestBuilder directly. The number of shards and replicas are  
specified in the settings object that is passed into the  
CreateIndexRequest.

Settings settings =  
ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
shards).build();

client.admin().indices().create(createIndexRequest).actionGet();

Cheers,

Ivan

P.S. When using Java, you should follow the Java Style Guidelines.

On Tue, Sep 25, 2012 at 4:56 AM, Percival Xavier  
[xavier.percival@gmail.com](mailto:xavier.percival@gmail.com) wrote:

> Hello,
> 
> How do I configure the above using Java API just before the below statement  
> is called ?
> 
> IndexResponse response = \_client.prepareIndex(obj.get\_srch\_index(),  
> obj.get\_srch\_name(),obj.get\_srch\_Id())  
> .setSource(sourceString)  
> .execute()  
> .actionGet();  
> System.out.println(response.id());
> 
> Percival
> 
> --

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [September 25, 2012, 6:56pm UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133/3 "2012-09-25T18:56:39Z")

</div>

Percival,

Here is another example that uses CreateIndexRequestBuilder that Ivan  
mentioned:

client.admin().indices().prepareCreate("test")

.setSettings(ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
2).put("number\_of\_replicas", "0"))  
.execute().actionGet();

By the way, you can learn a lot about using Java API by looking at the  
integration tests of elasticsearch.

Igor

On Tuesday, September 25, 2012 12:23:02 PM UTC-4, Ivan Brusic wrote:

> The number of shards and replicas are part of the source object you  
> pass in. I prefer to use CreateIndexRequests over using the  
> IndexRequestBuilder directly. The number of shards and replicas are  
> specified in the settings object that is passed into the  
> CreateIndexRequest.
> 
> Settings settings =  
> ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
> shards).build();
> 
> client.admin().indices().create(createIndexRequest).actionGet();
> 
> Cheers,
> 
> Ivan
> 
> P.S. When using Java, you should follow the Java Style Guidelines.
> 
> On Tue, Sep 25, 2012 at 4:56 AM, Percival Xavier  
> \<[xavier....@gmail.com](mailto:xavier....@gmail.com) \<javascript:\>\> wrote:
> 
> > Hello,
> > 
> > How do I configure the above using Java API just before the below  
> > statement  
> > is called ?
> > 
> > IndexResponse response = \_client.prepareIndex(obj.get\_srch\_index(),  
> > obj.get\_srch\_name(),obj.get\_srch\_Id())  
> > .setSource(sourceString)  
> > .execute()  
> > .actionGet();  
> > System.out.println(response.id());
> > 
> > Percival
> > 
> > --

--

---

<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: [September 25, 2012, 7:45pm UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133/4 "2012-09-25T19:45:13Z")

</div>

Thanks for demonstrating another way with the API. My example code is  
actually incomplete since I did not show how to create the actual  
request with the settings!

createIndexRequest = new CreateIndexRequest(indexName, settings);

--  
Ivan

On Tue, Sep 25, 2012 at 11:56 AM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> Percival,
> 
> Here is another example that uses CreateIndexRequestBuilder that Ivan  
> mentioned:
> 
> client.admin().indices().prepareCreate("test")  
> .setSettings(ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
> 2).put("number\_of\_replicas", "0"))  
> .execute().actionGet();
> 
> By the way, you can learn a lot about using Java API by looking at the  
> integration tests of elasticsearch.
> 
> Igor
> 
> On Tuesday, September 25, 2012 12:23:02 PM UTC-4, Ivan Brusic wrote:
> 
> > The number of shards and replicas are part of the source object you  
> > pass in. I prefer to use CreateIndexRequests over using the  
> > IndexRequestBuilder directly. The number of shards and replicas are  
> > specified in the settings object that is passed into the  
> > CreateIndexRequest.
> > 
> > Settings settings =  
> > ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
> > shards).build();
> > 
> > client.admin().indices().create(createIndexRequest).actionGet();
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > P.S. When using Java, you should follow the Java Style Guidelines.
> > 
> > On Tue, Sep 25, 2012 at 4:56 AM, Percival Xavier  
> > [xavier....@gmail.com](mailto:xavier....@gmail.com) wrote:
> > 
> > > Hello,
> > > 
> > > How do I configure the above using Java API just before the below  
> > > statement  
> > > is called ?
> > > 
> > > IndexResponse response = \_client.prepareIndex(obj.get\_srch\_index(),  
> > > obj.get\_srch\_name(),obj.get\_srch\_Id())  
> > > .setSource(sourceString)  
> > > .execute()  
> > > .actionGet();  
> > > System.out.println(response.id());
> > > 
> > > Percival
> > > 
> > > --
> 
> --

--

---

<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, 3:11am UTC](https://discuss.elastic.co/t/configuring-number-of-shards-and-replica-using-java-api/9133/5 "2017-07-06T03:11:28Z")

</div>


