# Difference between .index() and .prepareIndex()

**URL:** <https://discuss.elastic.co/t/difference-between-index-and-prepareindex/14141>\
**Category:** Elasticsearch\
**Created:** [October 28, 2013, 3:29pm UTC](https://discuss.elastic.co/t/difference-between-index-and-prepareindex/14141 "2013-10-28T15:29:34Z")\
**Posts on this page:** 3\
**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 28, 2013, 3:29pm UTC](https://discuss.elastic.co/t/difference-between-index-and-prepareindex/14141/1 "2013-10-28T15:29:34Z")

</div>

Hi guys,

whats the differences between these both calls. Is the first one a newer  
approach:

final IndexResponse response =  
this.esClient.prepareIndex(index,type).setSource(source).execute().actionGet();

and...

final IndexRequest request = new IndexRequest(index,type).source(source);  
final IndexResponse response = this.esClient.index(request).actionGet();

Regards,  
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:** ![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 28, 2013, 5:12pm UTC](https://discuss.elastic.co/t/difference-between-index-and-prepareindex/14141/2 "2013-10-28T17:12:46Z")

</div>

There is no difference when it comes to code execution, it is just that ES  
has more than one API style.

prepareIndex() uses an IndexRequestBuilder behind the scenes, and follows a  
kind of a "fluent interface" with a builder pattern

> **[Builder pattern](https://en.wikipedia.org/wiki/Builder_pattern)**
>
> The builder pattern is an object creation software design pattern. Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern\[citation needed\] that occurs when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a b...

A more verbose example of prepareIndex() would be  
client.prepareIndex().setIndex(index).setType(type).setId(id).setSource(source).execute().actionGet()

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:** ![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/difference-between-index-and-prepareindex/14141/3 "2017-07-06T02:10:12Z")

</div>


