# Update Api in java

**URL:** <https://discuss.elastic.co/t/update-api-in-java/8559>\
**Category:** Elasticsearch\
**Created:** [July 30, 2012, 7:03am UTC](https://discuss.elastic.co/t/update-api-in-java/8559 "2012-07-30T07:03:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajasuja18](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ajasuja18](https://discuss.elastic.co/u/ajasuja18)\
**Post date:** [July 30, 2012, 7:03am UTC](https://discuss.elastic.co/t/update-api-in-java/8559/1 "2012-07-30T07:03:44Z")

</div>

Hello Everyone,

I wanted to update some of my indexed data with a particular id using Java  
Api.  
I came to know that I can update using any script.

My question is: Can we only update using script? Any sample code for this..?  
Can I simply update value of any already indexed field?

Thanks in advance..!!

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [July 30, 2012, 10:46am UTC](https://discuss.elastic.co/t/update-api-in-java/8559/2 "2012-07-30T10:46:38Z")

</div>

Hi Ajasuja,

> My question is: Can we only update using script? Any sample code for this..?  
> Can I simply update value of any already indexed field?  
> As far as I know script is the only way to update a document. As long  
> as the \_source of document is stored you can update any field of  
> document.

Example showing how to update a title field:  
Node node = nodeBuilder().client(true).node();  
Client client = node.client();  
String index = "my\_index";  
String type= "my\_type";  
String id = "1";  
String newTitle = "my new title";  
client.prepareUpdate(index, type, id)  
.setScript("ctx.\_source.title="" + newTitle + """)  
.execute()  
.actionGet();

Martijn

---

<div class="post-metadata">

**Author:** ![ajasuja18](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ajasuja18](https://discuss.elastic.co/u/ajasuja18)\
**Post date:** [July 30, 2012, 6:47pm UTC](https://discuss.elastic.co/t/update-api-in-java/8559/3 "2012-07-30T18:47:59Z")

</div>

Thanks Martijn. It works for me .. thanks alot.

I hava one more question if you can help me in this.

In elasticsearch, as we know, while querying we can give various aspects of  
querying.  
One category is Query\_and\_fetch v/s Query\_then\_fetch.  
Also one more add on we can provide is the dfs query with both of them.

I heard one of the vedio of Shay Banon explaining dfs query and he  
mentioned that  
in case of dfs, there is an additional phase in which frequencies from all  
the shards  
is gathered.

\*I am not sure what he means by frequencies. Is it just the distributed  
term frequencies \*  
\*or it also takes into account the distributed inverse document frequencies?  
\*

Thanks in advance.

On Monday, July 30, 2012 3:46:38 AM UTC-7, Martijn v Groningen wrote:

> Hi Ajasuja,
> 
> > My question is: Can we only update using script? Any sample code for  
> > this..?  
> > Can I simply update value of any already indexed field?  
> > As far as I know script is the only way to update a document. As long  
> > as the \_source of document is stored you can update any field of  
> > document.
> 
> Example showing how to update a title field:  
> Node node = nodeBuilder().client(true).node();  
> Client client = node.client();  
> String index = "my\_index";  
> String type= "my\_type";  
> String id = "1";  
> String newTitle = "my new title";  
> client.prepareUpdate(index, type, id)  
> .setScript("ctx.\_source.title="" + newTitle + """)  
> .execute()  
> .actionGet();
> 
> Martijn

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [July 31, 2012, 9:28am UTC](https://discuss.elastic.co/t/update-api-in-java/8559/4 "2012-07-31T09:28:10Z")

</div>

The dfs phase helps with computing more accurate scores.  
In the dfs phase on each shard for all terms that match the query the  
document frequency is computed.  
Each dfs shard result is merged and the merged result is used as input  
for the idf part of the score computing in the  
query phase.

Martijn

On 30 July 2012 20:47, ajasuja [jasuja.ankit@gmail.com](mailto:jasuja.ankit@gmail.com) wrote:

> Thanks Martijn. It works for me .. thanks alot.
> 
> I hava one more question if you can help me in this.
> 
> In elasticsearch, as we know, while querying we can give various aspects of  
> querying.  
> One category is Query\_and\_fetch v/s Query\_then\_fetch.  
> Also one more add on we can provide is the dfs query with both of them.
> 
> I heard one of the vedio of Shay Banon explaining dfs query and he mentioned  
> that  
> in case of dfs, there is an additional phase in which frequencies from all  
> the shards  
> is gathered.
> 
> I am not sure what he means by frequencies. Is it just the distributed term  
> frequencies  
> or it also takes into account the distributed inverse document frequencies?
> 
> Thanks in advance.
> 
> On Monday, July 30, 2012 3:46:38 AM UTC-7, Martijn v Groningen wrote:
> 
> > Hi Ajasuja,
> > 
> > > My question is: Can we only update using script? Any sample code for  
> > > this..?  
> > > Can I simply update value of any already indexed field?  
> > > As far as I know script is the only way to update a document. As long  
> > > as the \_source of document is stored you can update any field of  
> > > document.
> > 
> > Example showing how to update a title field:  
> > Node node = nodeBuilder().client(true).node();  
> > Client client = node.client();  
> > String index = "my\_index";  
> > String type= "my\_type";  
> > String id = "1";  
> > String newTitle = "my new title";  
> > client.prepareUpdate(index, type, id)  
> > .setScript("ctx.\_source.title="" + newTitle + """)  
> > .execute()  
> > .actionGet();
> > 
> > Martijn

---

<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:18am UTC](https://discuss.elastic.co/t/update-api-in-java/8559/5 "2017-07-06T03:18:20Z")

</div>


