Is there a way to loop through all documents with scripted fields?
I want to divide two sums for a given day, and since dividing two aggregated values is impossible, I was thinking if there is a workaround with scripted fileds.
I am thinking of doing something like this:
double totalprice=0;
double totalquantity=0;
def current_day=doc['day_of_week'].value;
for (int i=0;i< ALL DOCUMENTS ;++i)
{
if (current_day==doc['day_of_week'][i])
{ totalprice=totalprice+doc['products.price'][i];
totalquantity=totalquantity+doc['products.quantity'][i];
}
}
return totalprice/totalquantity;
Thank you for your answer. I forgot to mention that I want the ratio to be visualised in kibana . Therefore, how could I use the average aggregation and scripted metric to show the ratio in kibana? Should I create a new index?
In Kibana the average will be calculated for the time range you'll choose.
If you want to get the average on all the documents regardless the time picker, you will have to create an index pattern without time field.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.