# How to update the column in a elasticsearch database using sql query

**URL:** <https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889>\
**Category:** Elasticsearch\
**Created:** [December 23, 2019, 7:24pm UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889 "2019-12-23T19:24:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sreekanth3](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sreekanth3](https://discuss.elastic.co/u/Sreekanth3)\
**Post date:** [December 23, 2019, 7:24pm UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/1 "2019-12-23T19:24:16Z")

</div>

Hi All,  
Can we update a column in a elasticsearch database using sql query.  
I have used this query  
'UPDATE from duration\_17 SET delay\_sec = '12.20' WHERE dest\_ip=192.168.1.181;'

but got this error

> Bad request [line 1:1: mismatched input 'UPDATE' expecting {'(', 'DEBUG', 'DESC', 'DESCRIBE', 'EXPLAIN', 'SELECT', 'SHOW', 'SYS', 'WITH'}]\>

Here i need to update the column directly in the database itself. Is there any possibility to do within Database.

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [December 24, 2019, 10:41am UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/2 "2019-12-24T10:41:11Z")

</div>

Currently ES SQL only allows SELECT queries.  
It's not possible to INSERT/UPDATE/DELETE documents through SQL.

If you want to update existing documents based on some match criteria please refer to the:  
[update by query section in the docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html).

---

<div class="post-metadata">

**Author:** ![Sreekanth3](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sreekanth3](https://discuss.elastic.co/u/Sreekanth3)\
**Post date:** [December 24, 2019, 11:29am UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/3 "2019-12-24T11:29:29Z")

</div>

Hi @matriv , What if i want to update a particular column , can i use the update by query. I have gone through it , There i have seen this ,

> POST twitter/\_update\_by\_query  
> {  
> "script": {  
> "source": "ctx.\_source.likes++",  
> "lang": "painless"  
> },  
> "query": {  
> "term": {  
> "user": "kimchy"  
> }  
> }  
> }

By using this , can i update the column delay\_sec

> sql\> select delay\_sec,event\_id,client\_ip,dest\_ip,message\_id from duration\_17 LIMIT 70;  
> delay\_sec | event\_id | client\_ip | dest\_ip | message\_id  
> ------------------+---------------+---------------+---------------+----------------------------------  
> 0.9620000123977661|SEND |IP ADDRESS |IP ADDRESS |!&!AA@openaccessjournals.org  
> 0.781000018119812 |SEND |IP ADDRESS |IP ADDRESS |!&!AA@openaccessjournals.org  
> 2.743000030517578 |SEND |IP ADDRESS |IP ADDRESS |[SRSR!&!AA@openaccessjournals.org](mailto:SRSR!&!AA@openaccessjournals.org)  
> 1.0399999618530273|SEND |IP ADDRESS |IP ADDRESS |[SRSR!&!AA@openaccessjournals.org](mailto:SRSR!&!AA@openaccessjournals.org)  
> 0.0 |SEND |IP ADDRESS |IP ADDRESS |[SSS!&!AA@openaccessjournals.org](mailto:SSS!&!AA@openaccessjournals.org)  
> 1.2589999437332153|SEND |IP ADDRESS |IP ADDRESS |[SSS!&!AA@openaccessjournals.org](mailto:SSS!&!AA@openaccessjournals.org)  
> 0.7419999837875366|SEND |IP ADDRESS |IP ADDRESS |[SSS!&!AA@openaccessjournals.org](mailto:SSS!&!AA@openaccessjournals.org)

This is my table looks like , i need to update the delay\_sec value.  
By using update by query , Is it possible ?

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [December 24, 2019, 4:12pm UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/4 "2019-12-24T16:12:10Z")

</div>

You should just adjust the filter:  
`"query": { "term": { "user": "kimchy" }`  
to match your `dest_ip`  
and the `"source": "ctx._source.likes++",` to just set the value you want to `delay_sec`.

---

<div class="post-metadata">

**Author:** ![Sreekanth3](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sreekanth3](https://discuss.elastic.co/u/Sreekanth3)\
**Post date:** [December 30, 2019, 4:11pm UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/5 "2019-12-30T16:11:14Z")

</div>

I don't think this will fill my requirements, That will be a live data coming into the elastic search , i need a code that runs everytime whenever the data comes into ES.

---

<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:** [January 27, 2020, 4:11pm UTC](https://discuss.elastic.co/t/how-to-update-the-column-in-a-elasticsearch-database-using-sql-query/212889/6 "2020-01-27T16:11:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
