How to do inplace update in elasticsearch alternate to script?


#1

How to do inplace update in elasticsearch alternate to script?

Use-case is, while indexing we need to do something similar to this,

{
total: 300[emp_1+emp_2]
emp_1: 100,
emp_2: 200,
}

We tried script(ctx._source.total), but this'll be a very costly operation for us. Any alternative is there without get and put?


(Christian Dahlqvist) #2

Can you provide a full example of what you are looking to do?


#3

Christian,

PUT data_index
{
"settings": { },
"mappings": {
"data": {
"properties": {
"total_count": {
"type": "long"
},
"count_1": {
"type": "long"
},
"count_2": {
"type": "long"
}
}
}
}
}

PUT lp_data_index/data/1
{
count_1:10,
count_2:10
}

After this put, total_count value should be 20(count_1+count_2).

Another use-case,
POST data_index/data/1
{
"doc": {
"count_1": 5
}
}

So in this use-case, the total_count should be (5+10) 15. So how to achieve this without doing get operation?

Currently we're doing like this,
if(ctx._source.containsKey('count_1')){ctx._source.total_count+=ctx._source.count_1; }

So is there way to achieve without script?


(Christian Dahlqvist) #4

Any update operation will result in a full reindex of the document, so I don’t think there is a more efficient way. How frequently are you updating a single document?


#5

Christian,
In a minute, single document can be updated 10-15 times approx. So script is the best way?


(Christian Dahlqvist) #6

Frequent updates of a single document is inefficient as it results in a lot of small refreshes and merges, so it would be better to aggregate in memory in the client and the store this periodically in Elasticsearch.


#7

Christian,
Client level aggregation is not possible in our use-case because we are dealing with millions of docs. So can we bring the document at the client side, compute the value and then write to ES? will this be faster then scripted update?


(system) #8

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