Operations between document fields in elasticsearch 5.6

I added several documents to the index in the same type in my elasticsearch database by running the following lines:

curl -XPUT localhost:9200/dt/cc/1 -d '{"cn": "ibm","name": "cat1", "lines": 80 }'
curl -XPUT localhost:9200/dt/cc/2 -d '{"cn": "zf", "name": "fox1", "lines": 90 }'
curl -XPUT localhost:9200/dt/cc/3 -d '{"cn": "zf", "name": "fox2", "lines": 42 }'
curl -XPUT localhost:9200/dt/cc/4 -d '{"cn": "ibm","name": "cat2", "lines": 50 }'
curl -XPUT localhost:9200/dt/cc/5 -d '{"cn": "ibm","name": "cat3", "lines": 20 }'
curl -XPUT localhost:9200/dt/cc/6 -d '{"cn": "zf", "name": "fox3", "lines": 20 }'
curl -XPUT localhost:9200/dt/cc/7 -d '{"cn": "ibm","name": "cat4"}'
curl -XPUT localhost:9200/dt/cc/8 -d '{"cn": "zf", "name": "fox4"}'

I know how to calculate the average value of the “line” fields among documents whose cn“zf” is, which is actually 51.

Now I'm looking for a way to add another field with a name difference_to_zf_mean_linesto the documents whose "cn" is "ibm", indicating the difference between the values ​​of the "strings" and the previously calculated average value (51). This is the structure I want to get:

'{"cn": "ibm","name": "cat1", "lines": 80, difference_to_zf_mean_lines: 29}'
'{"cn": "ibm","name": "cat2", "lines": 50, difference_to_zf_mean_lines: -1}'
'{"cn": "ibm","name": "cat3", "lines": 20, difference_to_zf_mean_lines: -31}'

But I would like to know if elasticsearch provides a way to calculate basic operations like this to add new fields to documents inside the index.

+4
source share

Source: https://habr.com/ru/post/1686494/


All Articles