# Custom routing Java example

**URL:** <https://discuss.elastic.co/t/custom-routing-java-example/13534>\
**Category:** Elasticsearch\
**Created:** [September 10, 2013, 9:39am UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534 "2013-09-10T09:39:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enoon](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@enoon](https://discuss.elastic.co/u/enoon)\
**Post date:** [September 10, 2013, 9:39am UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534/1 "2013-09-10T09:39:57Z")

</div>

Hi,

Can anyone provide an example on how custom routing on a path can be  
achieved via the java API?

Here is how I create my index:

HashMap\<String, Object\> settings = new HashMap\<String, Object\>();  
settings.put("number\_of\_shards", noShards);  
settings.put("number\_of\_replicas", noReplicas);  
CreateIndexRequestBuilder createIndexRequestBuilder =  
esClient.admin().indices().prepareCreate(name);  
createIndexRequestBuilder.setSettings(settings);  
CreateIndexResponse createIndexResponse =  
createIndexRequestBuilder.execute().actionGet();  
LOG.debug("Index " + name + " created: " +  
createIndexResponse.isAcknowledged());

Thank you!

--  
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:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 10, 2013, 5:15pm UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534/2 "2013-09-10T17:15:04Z")

</div>

You can specify a path for your custom routing in your mapping. The  
documents will need to contain it in a specific field. Have a look at the routing  
field [http://www.elasticsearch.org/guide/reference/mapping/routing-field/](http://www.elasticsearch.org/guide/reference/mapping/routing-field/)docs, path section.  
You need to add the mapping to your create index request using the  
addMapping method.

On Tuesday, September 10, 2013 11:39:57 AM UTC+2, Elod Balazs wrote:

> Hi,
> 
> Can anyone provide an example on how custom routing on a path can be  
> achieved via the java API?
> 
> Here is how I create my index:
> 
> HashMap\<String, Object\> settings = new HashMap\<String, Object\>();  
> settings.put("number\_of\_shards", noShards);  
> settings.put("number\_of\_replicas", noReplicas);  
> CreateIndexRequestBuilder createIndexRequestBuilder =  
> esClient.admin().indices().prepareCreate(name);  
> createIndexRequestBuilder.setSettings(settings);  
> CreateIndexResponse createIndexResponse =  
> createIndexRequestBuilder.execute().actionGet();  
> LOG.debug("Index " + name + " created: " +  
> createIndexResponse.isAcknowledged());
> 
> Thank you!

--  
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:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [September 11, 2013, 12:44am UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534/3 "2013-09-11T00:44:26Z")

</div>

Hi Luca - Actually I read somewhere that specifying a routing value using  
"path" parameter is expensive since the document needs to be parsed to  
retrieve that routing value (apart from the parsing that would be done when  
putting it into index). Hence it was recommended to specify routing as a  
query param in the url.

Pl let me know what you think!

-Amit.

On Tue, Sep 10, 2013 at 10:15 AM, Luca Cavanna [cavannaluca@gmail.com](mailto:cavannaluca@gmail.com)wrote:

> You can specify a path for your custom routing in your mapping. The  
> documents will need to contain it in a specific field. Have a look at the routing  
> field[http://www.elasticsearch.org/guide/reference/mapping/routing-field/](http://www.elasticsearch.org/guide/reference/mapping/routing-field/)docs, path section.  
> You need to add the mapping to your create index request using the  
> addMapping method.
> 
> On Tuesday, September 10, 2013 11:39:57 AM UTC+2, Elod Balazs wrote:
> 
> > Hi,
> > 
> > Can anyone provide an example on how custom routing on a path can be  
> > achieved via the java API?
> > 
> > Here is how I create my index:
> > 
> > HashMap\<String, Object\> settings = new HashMap\<String, Object\>();  
> > settings.put("number\_of\_\*\*shards", noShards);  
> > settings.put("number\_of\_\*\*replicas", noReplicas);  
> > CreateIndexRequestBuilder createIndexRequestBuilder =  
> > esClient.admin().indices().**prepareCreate(name);  
> > createIndexRequestBuilder.setSettings(settings);  
> > CreateIndexResponse createIndexResponse = createIndexRequestBuilder.  
> > execute().actionGet();  
> > LOG.debug("Index " + name + " created: " + createIndexResponse.**  
> > isAcknowledged());
> > 
> > Thank you!
> 
> --  
> 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:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 11, 2013, 8:10am UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534/4 "2013-09-11T08:10:30Z")

</div>

Hi,  
that is true. In a distributed environment, you can send your index  
requests to any node in the cluster. Based on the id or the custom routing,  
that node will forward the request to all the other nodes where the  
document is supposed to be indexed (nodes that hold its primary shard and  
all the replicas). That happens without needing to parse the document on  
the node that receives the request, unless you have the id or the routing  
specified as path in the document itself. It's just an additional step that  
needs to happen to determine where the document needs to be sent, not a big  
deal since we do pull parsing but still, if you can pass the routing as a  
parameter, outside of your document, that's the most recommended way to do  
it.

Cheers  
Luca

On Wed, Sep 11, 2013 at 2:44 AM, Amit Soni [amitsoni29@gmail.com](mailto:amitsoni29@gmail.com) wrote:

> Hi Luca - Actually I read somewhere that specifying a routing value using  
> "path" parameter is expensive since the document needs to be parsed to  
> retrieve that routing value (apart from the parsing that would be done when  
> putting it into index). Hence it was recommended to specify routing as a  
> query param in the url.
> 
> Pl let me know what you think!
> 
> -Amit.
> 
> On Tue, Sep 10, 2013 at 10:15 AM, Luca Cavanna [cavannaluca@gmail.com](mailto:cavannaluca@gmail.com)wrote:
> 
> > You can specify a path for your custom routing in your mapping. The  
> > documents will need to contain it in a specific field. Have a look at the routing  
> > field[http://www.elasticsearch.org/guide/reference/mapping/routing-field/](http://www.elasticsearch.org/guide/reference/mapping/routing-field/)docs, path section.  
> > You need to add the mapping to your create index request using the  
> > addMapping method.
> > 
> > On Tuesday, September 10, 2013 11:39:57 AM UTC+2, Elod Balazs wrote:
> > 
> > > Hi,
> > > 
> > > Can anyone provide an example on how custom routing on a path can be  
> > > achieved via the java API?
> > > 
> > > Here is how I create my index:
> > > 
> > > HashMap\<String, Object\> settings = new HashMap\<String, Object\>();  
> > > settings.put("number\_of\_\*\*shards", noShards);  
> > > settings.put("number\_of\_\*\*replicas", noReplicas);  
> > > CreateIndexRequestBuilder createIndexRequestBuilder =  
> > > esClient.admin().indices().**prepareCreate(name);  
> > > createIndexRequestBuilder.setSettings(settings);  
> > > CreateIndexResponse createIndexResponse = createIndexRequestBuilder.  
> > > execute().actionGet();  
> > > LOG.debug("Index " + name + " created: " + createIndexResponse.**  
> > > isAcknowledged());
> > > 
> > > Thank you!
> > 
> > --  
> > 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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/waIu6oZKghk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/waIu6oZKghk/unsubscribe).  
> To unsubscribe from this group and all its topics, 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:** ![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:17am UTC](https://discuss.elastic.co/t/custom-routing-java-example/13534/5 "2017-07-06T02:17:10Z")

</div>


