# Incrementing an integer field

**URL:** <https://discuss.elastic.co/t/incrementing-an-integer-field/6643>\
**Category:** Elasticsearch\
**Created:** [February 8, 2012, 6:18pm UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643 "2012-02-08T18:18:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)\
**Post date:** [February 8, 2012, 6:18pm UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643/1 "2012-02-08T18:18:30Z")

</div>

Hi,

Is there a more efficient way to increment an integer field besides  
reading the entire document, updating the number, and re-indexing the  
document?

Thanks,

Frank

---

<div class="post-metadata">

**Author:** ![lukeforehand\_2](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lukeforehand\_2](https://discuss.elastic.co/u/lukeforehand_2)\
**Post date:** [February 8, 2012, 6:58pm UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643/2 "2012-02-08T18:58:19Z")

</div>

I believe you can do it with a script in the new release candidate:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

specific issue:

> <https://github.com/elastic/elasticsearch/issues/1583>
>
> The update action allows to directly update a specific document based on a scrip…t. The operation gets the document (collocated with the shard) from the index, runs the script (with optional script language and parameters), and index back the result (also allows to delete, or ignore the operation). 
> 
> Note, this operation still means full reindex of the document, it just removes some network roundtrips and reduces chances of version conflicts between the get and the index.
> 
> For example, lets index a simple doc:
> 
> \`\`\`
> curl -XPUT localhost:9200/test/type1/1 -d '{
> "counter" : 1,
> "tags" : \["red"\]
> }'
> \`\`\`
> 
> Now, we can execute a script that would increment the counter
> 
> \`\`\`
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> "script" : "ctx.\_source.counter += count",
> "params" : {
> "count" : 4
> }
> }'
> \`\`\`
> 
> We can also add a tag to the list of tags:
> 
> \`\`\`
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> "script" : "ctx.\_source.tags += tag",
> "params" : {
> "tag" : "blue"
> }
> }'
> \`\`\`
> 
> And, we can delete the doc if the tags contain \`blue\`, or ignore (noop):
> 
> \`\`\`
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = \\"delete\\" : ctx.op = \\"none\\"",
> "params" : {
> "tag" : "blue"
> }
> }'
> \`\`\`
> \## Parameters:
> \- \`routing\`: Sets the routing that will be used to route the document to the relevant shard.
> \- \`parent\`: Simply sets the routing.
> \- \`timeout\`: Timeout waiting for a shard to become available.
> \- \`replication\`: The replication type for the delete/index operation (\`sync\` or \`async\`).
> \- \`consistency\`: The write consistency of the index/delete operation.
> \- \`retry\_on\_conflict\`: How many times to retry if there is a version conflict between getting the document and indexing / deleting it. Defaults to \`0\`.

-Luke

On Feb 8, 12:18 pm, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:

> Hi,
> 
> Is there a more efficient way to increment an integer field besides  
> reading the entire document, updating the number, and re-indexing the  
> document?
> 
> Thanks,
> 
> Frank

---

<div class="post-metadata">

**Author:** ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)\
**Post date:** [February 8, 2012, 8:45pm UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643/3 "2012-02-08T20:45:40Z")

</div>

Thanks Luke. That looks extremely powerful indeed, if it works as  
advertised.

I'm somewhat curious as to how the server actually executes the script  
-- I'm guessing that since ES is based on Lucene, it still has to read  
and rewrite the entire document to the index. However, not having to  
pass the document over the network is still a substantial benefit.

Frank

On Feb 8, 12:58 pm, lukeforehand [lukeforeh...@gmail.com](mailto:lukeforeh...@gmail.com) wrote:

> I believe you can do it with a script in the new release candidate:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/download/2012/02/07/0.19.0.RC1.html)
> 
> specific issue:[Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/1583)
> 
> -Luke
> 
> On Feb 8, 12:18 pm, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:
> 
> > Hi,
> 
> > Is there a more efficient way to increment an integer field besides  
> > reading the entire document, updating the number, and re-indexing the  
> > document?
> 
> > Thanks,
> 
> > Frank

---

<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 8, 2012, 10:35pm UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643/4 "2012-02-08T22:35:37Z")

</div>

Yea, the document still needs to be reindexed (I explicitly stated it at the issue and in the guide, just to make sure 🙂 ).

On Wednesday, February 8, 2012 at 10:45 PM, Frank LaRosa wrote:

> Thanks Luke. That looks extremely powerful indeed, if it works as  
> advertised.
> 
> I'm somewhat curious as to how the server actually executes the script  
> -- I'm guessing that since ES is based on Lucene, it still has to read  
> and rewrite the entire document to the index. However, not having to  
> pass the document over the network is still a substantial benefit.
> 
> Frank
> 
> On Feb 8, 12:58 pm, lukeforehand \<[lukeforeh...@gmail.com](mailto:lukeforeh...@gmail.com) ([http://gmail.com](http://gmail.com))\> wrote:
> 
> > I believe you can do it with a script in the new release candidate:
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/download/2012/02/07/0.19.0.RC1.html)
> > 
> > specific issue:[Update API: Allow to update a document based on a script · Issue #1583 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/1583)
> > 
> > -Luke
> > 
> > On Feb 8, 12:18 pm, Frank LaRosa \<[fr...@studyblue.com](mailto:fr...@studyblue.com) ([http://studyblue.com](http://studyblue.com))\> wrote:
> > 
> > > Hi,
> > 
> > > Is there a more efficient way to increment an integer field besides  
> > > reading the entire document, updating the number, and re-indexing the  
> > > document?
> > 
> > > Thanks,
> > 
> > > Frank

---

<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:40am UTC](https://discuss.elastic.co/t/incrementing-an-integer-field/6643/5 "2017-07-06T03:40:05Z")

</div>


