# There's more than one way

**URL:** <https://discuss.elastic.co/t/theres-more-than-one-way/6652>\
**Category:** Elasticsearch\
**Created:** [February 9, 2012, 5:53am UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652 "2012-02-09T05:53:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [February 9, 2012, 5:53am UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652/1 "2012-02-09T05:53:18Z")

</div>

I believe the following are all equivalent? Any reason to prefer one  
over the other?

A.  
CreateIndexRequest request =  
Requests.createIndexRequest(index).settings(settings);  
client.admin().indices().create(request).actionGet();

B.  
CreateIndexRequest request = new  
CreateIndexRequest(index).settings(settings);  
client.admin().indices().create(request).actionGet();

C.

client.admin().indices().prepareCreate(index).setSettings(settings).execute().actionGet();

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 9, 2012, 8:27am UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652/2 "2012-02-09T08:27:37Z")

</div>

Yes, all are the same. A and B are "really" the same (Requests is just a simple static factory), and C was added later on in the life of elasticsearch and the on I recommend (more readable).

On Thursday, February 9, 2012 at 7:53 AM, Eric Jain wrote:

> I believe the following are all equivalent? Any reason to prefer one  
> over the other?
> 
> A.  
> CreateIndexRequest request =  
> Requests.createIndexRequest(index).settings(settings);  
> client.admin().indices().create(request).actionGet();
> 
> B.  
> CreateIndexRequest request = new  
> CreateIndexRequest(index).settings(settings);  
> client.admin().indices().create(request).actionGet();
> 
> C.
> 
> client.admin().indices().prepareCreate(index).setSettings(settings).execute().actionGet();

---

<div class="post-metadata">

**Author:** ![Eric\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eric_jain/32/834_2.png) [@Eric\_Jain](https://discuss.elastic.co/u/Eric_Jain)\
**Post date:** [February 10, 2012, 7:21pm UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652/3 "2012-02-10T19:21:20Z")

</div>

On Feb 9, 12:27 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Yes, all are the same. A and B are "really" the same (Requests is just a simple static factory), and C was added later on in the life of elasticsearch and the on I recommend (more readable).

For the record, there's also an option D: new  
SearchRequestBuilder(client)... C and D can be more useful because  
some of the builders have more fine-grained methods. For example,  
client.prepareSearch(index) returns a SearchRequestBuilder, which has  
methods like setQuery, addFacet, setSize etc.  
Requests.searchRequest(index) on the other hand returns a  
SearchRequest, which requires the same values to be set via a single  
call to source(json).

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 12, 2012, 2:47pm UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652/4 "2012-02-12T14:47:30Z")

</div>

They are nicer to be used, but the builders are just wrappers. For the search aspect, there is also a SearchSourceBuilder that builds the source (and is used internally in the SearchRequestBuilder API).

The RequestBuilders were added later in the lifespan of elasticsearch, and people should use those for simplicity. (the prepareXXX APIs).

On Friday, February 10, 2012 at 9:21 PM, Eric Jain wrote:

> On Feb 9, 12:27 am, Shay Banon \<[kim...@gmail.com](mailto:kim...@gmail.com) ([http://gmail.com](http://gmail.com))\> wrote:
> 
> > Yes, all are the same. A and B are "really" the same (Requests is just a simple static factory), and C was added later on in the life of elasticsearch and the on I recommend (more readable).
> 
> For the record, there's also an option D: new  
> SearchRequestBuilder(client)... C and D can be more useful because  
> some of the builders have more fine-grained methods. For example,  
> client.prepareSearch(index) returns a SearchRequestBuilder, which has  
> methods like setQuery, addFacet, setSize etc.  
> Requests.searchRequest(index) on the other hand returns a  
> SearchRequest, which requires the same values to be set via a single  
> call to source(json).

---

<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:39am UTC](https://discuss.elastic.co/t/theres-more-than-one-way/6652/5 "2017-07-06T03:39:45Z")

</div>


