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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -