# Insert and Update in less than a second. Is it safe?

**URL:** <https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881>\
**Category:** Elasticsearch\
**Created:** [December 2, 2016, 4:03pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881 "2016-12-02T16:03:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ranieri\_Mazili\_de\_Ol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ranieri_mazili_de_ol/32/15815_2.png) [@Ranieri\_Mazili\_de\_Ol](https://discuss.elastic.co/u/Ranieri_Mazili_de_Ol)\
**Post date:** [December 2, 2016, 4:03pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881/1 "2016-12-02T16:03:34Z")

</div>

Hello,  
I'm completely new with elasticsearch and don't have any experience with it yet, and now I'm evaluating if it can be part of a solution I'm creating, because I would like to use kibana.

I've a JMS Queue where events are being added by other system. I'm converting these events in JSON like below:

{ "instanceId":123, "name":"TST Process", "status":"Active" }

First question. Can I update this JSON record inside elasticsearch using as filter the parameter instanceId? Something like UPDATE set status='Running' where instanceId = 123 in SQL

Second question. The update action can occur in a really small period of time (miliseconds) and I'm not sure if this kind of action is safe in elasticsearch because in the article below I read:

> Elasticsearch provides data manipulation and search capabilities in near real time. By default, you can expect a one second delay (refresh interval) from the time you index/update/delete your data until the time that it appears in your search results.  
> [https://www.elastic.co/guide/en/elasticsearch/reference/current/\_modifying\_your\_data.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html)

I really appreciate any help.  
Thanks

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 2, 2016, 4:22pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881/2 "2016-12-02T16:22:08Z")

</div>

> [@Ranieri\_Mazili\_de\_Ol](#):
>
> First question. Can I update this JSON record inside elasticsearch using as filter the parameter instanceId? Something like UPDATE set status='Running' where instanceId = 123 in SQL

Yes, `_update_by_query` does this. But it is a much "heavier" thing than doing an `_update` using the documents ID. You should prefer the `_update` API if possible.

> [@Ranieri\_Mazili\_de\_Ol](#):
>
> Second question. The update action can occur in a really small period of time (miliseconds) and I'm not sure if this kind of action is safe

Well, with `_update` it is quite safe. `_update` performs a realtime get, which, in 5.0, if the document hasn't been refreshed already, will force a refresh of the shard to make the change visible immediately and then update. Pre 5.0 it is actually more efficient because the refresh isn't required. The change in 5.0 was to enable some other optimizations for more common use cases.

With `_update_by_query` you'll get a version conflict and have to manually retry.

---

<div class="post-metadata">

**Author:** ![Ranieri\_Mazili\_de\_Ol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ranieri_mazili_de_ol/32/15815_2.png) [@Ranieri\_Mazili\_de\_Ol](https://discuss.elastic.co/u/Ranieri_Mazili_de_Ol)\
**Post date:** [December 2, 2016, 4:30pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881/3 "2016-12-02T16:30:30Z")

</div>

Can I choose my own document ID, I mean, can I use instanceId value as document ID or only the system (elasticsearch) can give this auto-generated id?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 2, 2016, 4:53pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881/4 "2016-12-02T16:53:02Z")

</div>

You can chose your document ID. You should if you plan on updating it. Have a look at the first example [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html).

---

<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:** [December 30, 2016, 4:53pm UTC](https://discuss.elastic.co/t/insert-and-update-in-less-than-a-second-is-it-safe/67881/5 "2016-12-30T16:53:08Z")

</div>

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