I added several docs to an index in a single 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 of field 'lines' among docs whose cn is "zf", which is actually 51.
Now, I am looking for a way to add another field named difference_to_zf_mean_lines to docs whose 'cn' is 'ibm', indicating the difference between the 'lines' values and the previously calculated mean (51).
This is the kind of 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 compute basic operations like this one to add new fields to documents inside an index.