To find difference between two integer fields and check it falls under a specific range, using scripts in elasticsearch -
i have 2 fields,let name them "fielda" , "fieldb" in documents , need find difference between them , check if value falls under specific range "rangea" or " rangeb" , return documents matches criteria. schema data shown below:
{ "fielda": 45 "fieldb":13 }
i need find document have difference between "fielda" , "fieldb" in between 30 , 35. how can using scripting in elasticsearch?
this can done using aggregations , scripts below:
{ "aggregations": { "age_diff": { "range": { "script": "doc[\"fielda\"].value - doc[\"fieldb\"].value", "ranges": [ { "from": 30, "to": 35 } ] } } } }
this way can check how many documents falls under specified range.but if want documents under aggregations can use "top_hits" aggregations.
more detailed discussion on aggregations can found here , more "top_hits" can found in detail here
Comments
Post a Comment