When doing an update, create the object if it doesn't exist


(Chris A) #1

Hi all

We are using ElasticSearch to track a counter. When an update arrives in
our system we want to increment or decrement a field on a specific object.

It currently works well for removals, as we use the scripting functionality
to decrement the counter then remove the object if it hits zero.

However, for increments, we have the issue where if we issue an update for
an object that does exist, it works correctly (and increments the value);
however there seems to be no way to specify creating an object with that
name if it doesn't exist.

Our current workaround is to do a create (with the op_type=create argument)
before each update, but it seems like a waste to do two requests when it
would ideally be accomplished with only one.

Please see this gist with how we are performing the update and
create/update at the moment: https://gist.github.com/3246473

Does anyone have any thoughts on how it would be possible to do this update
and "create if not exists" in one request? The update endpoint doesn't
currently support PUT and I couldn't find anything in the docs.

Thanks

Chris


(Benjamin Devèze) #2

Hi,

the upsert feature has already been added to incoming ES 0.20 see
https://github.com/elasticsearch/elasticsearch/issues/2008 for more
information


(Chris A) #3

Excellent thanks.

Chris

On 3 August 2012 11:31, Benjamin Devèze benjamin.deveze@gmail.com wrote:

Hi,

the upsert feature has already been added to incoming ES 0.20 see
https://github.com/elasticsearch/elasticsearch/issues/2008 for more
information


(system) #4