Optimization: fetching documents as per geo distance

I need to fetch list of stocks sorted by a sort_score in buckets of 10x km distance from a given location.
1 : [0-10] km
2 : [10-20] km
3 : [20-30] km

Stocks in bucket p should appear before bucket q for all p<q. All stocks in bucket q should be sorted as per sort score. Also I must have facility of "from" and "size" for pagination.

To achieve this I have written the following query which is working fine to the best of my knowledge but I see a lot of room for improvement in terms of optimizations:

GET stocks/_search
   "from": 0,
   "size": 25,
   "_source": false,
   "docvalue_fields": [
   "sort": [
         "_script": {
            "type": "number",
            "script": {
               "lang": "painless",
               "inline": "double diff = doc['location'].planeDistance(params.targetLat, params.targetLong) ; return (int)((diff)/params.distance_bucket);",
               "params": {
                  "targetLat": 91.0678637,
                  "targetLong": 72.9991898,
                  "distance_bucket" : 10000
            "order": "asc"

Is it the best way to implement this?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.