# Index

**URL:** https://discuss.elastic.co/t/index/6777
**Category:** Elasticsearch
**Created:** [February 22, 2012, 5:07am UTC](https://discuss.elastic.co/t/index/6777 "2012-02-22T05:07:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Abhishek\_Jajoria](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@Abhishek\_Jajoria](https://discuss.elastic.co/u/Abhishek_Jajoria)
#### Post date: [February 22, 2012, 5:07am UTC](https://discuss.elastic.co/t/index/6777/1 "2012-02-22T05:07:51Z")

</div>

How can I update a single field of all the documents using java api in  
elasticsearch?

---

<div class="post-metadata">

### Author: ![Berkay\_Mollamustafao](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@Berkay\_Mollamustafao](https://discuss.elastic.co/u/Berkay_Mollamustafao)
#### Post date: [February 22, 2012, 5:16am UTC](https://discuss.elastic.co/t/index/6777/2 "2012-02-22T05:16:51Z")

</div>

There is no single field update in ES. To update a document, you have to  
index the entire document again, hence to update a field for all documents,  
you'd have to essentially index the entire data set. You'd be better off  
creating a new index and take advantage of aliases to switch from the old  
index to the new one.

Regards,  
Berkay Mollamustafaoglu  
mberkay on yahoo, google and skype

On Wed, Feb 22, 2012 at 12:07 AM, jajoria abhishek \<  
[jajoria.abhishek@gmail.com](mailto:jajoria.abhishek@gmail.com)\> wrote:

> How can I update a single field of all the documents using java api in  
> elasticsearch?

---

<div class="post-metadata">

### Author: ![Abhishek\_Jajoria](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@Abhishek\_Jajoria](https://discuss.elastic.co/u/Abhishek_Jajoria)
#### Post date: [February 22, 2012, 5:46am UTC](https://discuss.elastic.co/t/index/6777/3 "2012-02-22T05:46:50Z")

</div>

I want to increment or decrement an integer field like we do in sql can we  
do that in ES.

How can we do that using java API?

On Wed, Feb 22, 2012 at 10:46 AM, Berkay Mollamustafaoglu \<[mberkay@gmail.com](mailto:mberkay@gmail.com)

> wrote:

> There is no single field update in ES. To update a document, you have to  
> index the entire document again, hence to update a field for all documents,  
> you'd have to essentially index the entire data set. You'd be better off  
> creating a new index and take advantage of aliases to switch from the old  
> index to the new one.
> 
> Regards,  
> Berkay Mollamustafaoglu  
> mberkay on yahoo, google and skype
> 
> On Wed, Feb 22, 2012 at 12:07 AM, jajoria abhishek \<  
> [jajoria.abhishek@gmail.com](mailto:jajoria.abhishek@gmail.com)\> wrote:
> 
> > How can I update a single field of all the documents using java api in  
> > elasticsearch?

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [February 22, 2012, 8:58am UTC](https://discuss.elastic.co/t/index/6777/4 "2012-02-22T08:58:46Z")

</div>

On Wed, 2012-02-22 at 11:16 +0530, jajoria abhishek wrote:

> I want to increment or decrement an integer field like we do in sql  
> can we do that in ES.

> How can we do that using java API?

In the 0.19\* branch, there is the new update API. What this does for  
you is to get the doc, run the script that you specify on the doc, then  
reindex the doc.

It's the same thing that you'd have to do now, but a little faster  
because it all happens on the server, without having to send the doc  
between client and server.

However, the update API only works on a single doc, at least for now.  
Shay has talked about adding an update\_by\_query API, but that is  
considerably more complex.

So for the moment, there is no easy way to do the equivalent of:

UPDATE table SET foo = foo + 1

You are limited to updating/reindexing each doc individually

clint

---

<div class="post-metadata">

### Author: ![Nate\_Sammons](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nate_sammons/32/2979_2.png) [@Nate\_Sammons](https://discuss.elastic.co/u/Nate_Sammons)
#### Post date: [February 22, 2012, 4:18pm UTC](https://discuss.elastic.co/t/index/6777/5 "2012-02-22T16:18:47Z")

</div>

You may want to look into counters in Cassandra. Depending on what you're  
doing, it might be a good fit. My project is going to end up using ES for  
some things, Cassandra for others.

-nate

"The sky calls to us. If we do not destroy ourselves, we will one day  
venture to the stars." -- Carl Sagan

On Tue, Feb 21, 2012 at 10:46 PM, jajoria abhishek \<  
[jajoria.abhishek@gmail.com](mailto:jajoria.abhishek@gmail.com)\> wrote:

> I want to increment or decrement an integer field like we do in sql can  
> we do that in ES.
> 
> How can we do that using java API?
> 
> On Wed, Feb 22, 2012 at 10:46 AM, Berkay Mollamustafaoglu \<  
> [mberkay@gmail.com](mailto:mberkay@gmail.com)\> wrote:
> 
> > There is no single field update in ES. To update a document, you have to  
> > index the entire document again, hence to update a field for all documents,  
> > you'd have to essentially index the entire data set. You'd be better off  
> > creating a new index and take advantage of aliases to switch from the old  
> > index to the new one.
> > 
> > Regards,  
> > Berkay Mollamustafaoglu  
> > mberkay on yahoo, google and skype
> > 
> > On Wed, Feb 22, 2012 at 12:07 AM, jajoria abhishek \<  
> > [jajoria.abhishek@gmail.com](mailto:jajoria.abhishek@gmail.com)\> wrote:
> > 
> > > How can I update a single field of all the documents using java api in  
> > > elasticsearch?

---

<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:38am UTC](https://discuss.elastic.co/t/index/6777/6 "2017-07-06T03:38:33Z")

</div>


