# Using Java API's prepareUpdate

**URL:** <https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434>\
**Category:** Elasticsearch\
**Created:** [March 18, 2014, 5:46pm UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434 "2014-03-18T17:46:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohit\_Anchlia](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Mohit\_Anchlia](https://discuss.elastic.co/u/Mohit_Anchlia)\
**Post date:** [March 18, 2014, 5:46pm UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434/1 "2014-03-18T17:46:25Z")

</div>

I currently use prepareIndex to index a document like this:

idxResponse =

client.prepareIndex(indexName, indexType, id)

.setSource(json)

.execute()

.actionGet();

I am trying to use setSource(json) with prepareUpdate but that doesn't seem  
to work. Is there some other alternative that I can use?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAOT3TWpZbF2jQwfKZmb5\_BN%2BvFSd7c0aRhzEPSDQPH80E\_iK9Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAOT3TWpZbF2jQwfKZmb5_BN%2BvFSd7c0aRhzEPSDQPH80E_iK9Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [March 18, 2014, 7:35pm UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434/2 "2014-03-18T19:35:41Z")

</div>

Here is my code for the INDEX action. Similar code (not shown) exists for  
the CREATE action. This is for ES 1.0.0 GA, though it hasn't changed since  
its 0.90.X versions:

> 

IndexResponse response = null;  
try  
{  
IndexRequestBuilder irb = client.prepareIndex(index, type, id);  
irb.setSource(json).setOpType(IndexRequest.OpType.INDEX);  
irb.setRefresh(refresh);

/\* Set the version number and version type (internal or external) \*/  
if (rec.hasVersion())  
{  
irb.setVersion(rec.getVersion());  
if (rec.isVersionExternal())  
irb.setVersionType(VersionType.EXTERNAL);  
}

/\* Set the TTL (time to live) if there is one \*/  
TimeValue ttl = rec.getTTL();  
if (ttl != null)  
irb.setTTL(ttl.getMillis());

response = irb.execute().actionGet();  
}  
catch (Exception e)  
{  
...  
}

Don't worry about rec. It's just a class that generically describes the  
documents and contains various attributes and field values, and can  
serialize itself to JSON.

Hope this helps.

Brian

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mohit\_Anchlia](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Mohit\_Anchlia](https://discuss.elastic.co/u/Mohit_Anchlia)\
**Post date:** [March 18, 2014, 8:42pm UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434/3 "2014-03-18T20:42:05Z")

</div>

Thanks does it create a new index if it doesn't exist?

On Tue, Mar 18, 2014 at 12:35 PM, InquiringMind [brian.from.fl@gmail.com](mailto:brian.from.fl@gmail.com)wrote:

> Here is my code for the INDEX action. Similar code (not shown) exists for  
> the CREATE action. This is for ES 1.0.0 GA, though it hasn't changed since  
> its 0.90.X versions:
> 
> > 
> 
> IndexResponse response = null;  
> try  
> {  
> IndexRequestBuilder irb = client.prepareIndex(index, type, id);  
> irb.setSource(json).setOpType(IndexRequest.OpType.INDEX);  
> irb.setRefresh(refresh);
> 
> /\* Set the version number and version type (internal or external) \*/  
> if (rec.hasVersion())  
> {  
> irb.setVersion(rec.getVersion());  
> if (rec.isVersionExternal())  
> irb.setVersionType(VersionType.EXTERNAL);  
> }
> 
> /\* Set the TTL (time to live) if there is one \*/  
> TimeValue ttl = rec.getTTL();  
> if (ttl != null)  
> irb.setTTL(ttl.getMillis());
> 
> response = irb.execute().actionGet();  
> }  
> catch (Exception e)  
> {  
> ...  
> }
> 
> Don't worry about rec. It's just a class that generically describes the  
> documents and contains various attributes and field values, and can  
> serialize itself to JSON.
> 
> Hope this helps.
> 
> Brian
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/c26e586f-3df5-4c22-b239-42dc614bf9af%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAOT3TWq6fcUJNs26X%2BovxsJOvB5kjjTcSOU\_ehttj%2BEUWnf6Ug%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAOT3TWq6fcUJNs26X%2BovxsJOvB5kjjTcSOU_ehttj%2BEUWnf6Ug%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [March 18, 2014, 10:37pm UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434/4 "2014-03-18T22:37:23Z")

</div>

1. By default, it creates an index if the index does not exist. But no, it  
won't create an index if the index does not exist and the following is in  
your elsasticsearch.yml configuration:

action.auto\_create\_index: false

1. But did you mean: Does it create a _document_ if the document does not  
exist? If so, then yes: The index action updates the document if it exists  
but creates the document if it does not exist.

The create action fails if the document already exists.

1. I deleted my first post because I realized that it didn't answer your  
question: I wasn't using prepareUpdate and actually have no experience with  
that particular API action. Sorry for any confusion I may have caused.

I hope this helps.

Brian

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0819f28a-0417-4727-b7d1-90b5927439bb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0819f28a-0417-4727-b7d1-90b5927439bb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:42am UTC](https://discuss.elastic.co/t/using-java-apis-prepareupdate/16434/5 "2017-07-06T01:42:23Z")

</div>


