# Efficient sorting by distance to a point

**URL:** <https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256>\
**Category:** Elasticsearch\
**Created:** [February 19, 2015, 3:15am UTC](https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256 "2015-02-19T03:15:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Gledhill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicholas_gledhill/32/68250_2.png) [@Nicholas\_Gledhill](https://discuss.elastic.co/u/Nicholas_Gledhill)\
**Post date:** [February 19, 2015, 3:15am UTC](https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256/1 "2015-02-19T03:15:22Z")

</div>

Situation, we have an index of "Features" that contain Polygon and  
MultiPolygon coordinate descriptions of geographical spaces.om the Earth's  
surface.

Some of these "Features" contain a "point" value that represents a fairly  
arbitrary point inside the given Polygon... but most don't. Most are just  
described by their border coordinates.

I've designed a query to return a list of Features that are near a certain  
point (or rather within a certain distance of that point).

That's all fairly straight forward.

What we also want to do is return those points listed in ORDER of distance  
from the point given.

"Distance from the point" would preferably be "distance to the closest  
point on the border described in the Polygon (or Multi Polygon)".

But it could also (more efficiently) be the "distance to the nearest point  
in the Polygon (or MultiPolygon) coordinate list".

I have written a Groovy script to calculate a score based on the second of  
these 2 options. It works but it is not performant. Neither of them are. I  
started with one ("geo-distance-score") which ended up being far simpler  
than I wanted it to - and then further simplified it  
("estimate-distance-score") to show that no matter how simple I made the  
calculation, it was the act of scoring them at all that was causing the  
problem; not the computational complexity of the algorithm.

Does anyone have any ideas for how to make this more efficient? Any and all  
ideas - even the slightly crazy ones - are welcome and will be considered.  
🙂

I'll paste the Elasticsearch query below, and attach 2 slightly different  
versions of the Groovy script I was playing around with. Details in the  
comments in those files.

Thanks!

ES Query:  
{  
"query": {  
"function\_score": {  
"boost\_mode": "replace",  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"status": "usable"  
}  
},  
{  
"geo\_shape": {  
"geometry": {  
"relation": "intersects",  
"shape": {  
"type": "circle",  
"coordinates": [  
151.186209,  
-33.892861  
],  
"radius": "100km"  
}  
}  
}  
}  
]  
}  
},  
"script\_score": {  
"script": "geo-distance-score",  
"params": {  
"lat": -33.892861,  
"lon": 151.186209  
}  
}  
}  
},  
"size": 10  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5bc53bbe-dc00-40bf-97cb-33b54fa81802%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5bc53bbe-dc00-40bf-97cb-33b54fa81802%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Nicholas\_Gledhill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicholas_gledhill/32/68250_2.png) [@Nicholas\_Gledhill](https://discuss.elastic.co/u/Nicholas_Gledhill)\
**Post date:** [February 19, 2015, 3:18am UTC](https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256/2 "2015-02-19T03:18:21Z")

</div>

Possibly should have specified - elasticsearch 1.4.2

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a550bdd2-64f6-4c1f-95b0-55c61923beba%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a550bdd2-64f6-4c1f-95b0-55c61923beba%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Nicholas\_Gledhill](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicholas_gledhill/32/68250_2.png) [@Nicholas\_Gledhill](https://discuss.elastic.co/u/Nicholas_Gledhill)\
**Post date:** [February 19, 2015, 6:08am UTC](https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256/3 "2015-02-19T06:08:41Z")

</div>

As a final addition to the problem... here's an example of what a Feature  
looks like:

```
     {
        "_index": "location-v1",
        "_type": "Feature",
        "_id": "mvbjukyzRSatqOsq6CgwvA",
        "_score": 2324118,
        "_source": {
           "name": "10753650000",
           "geometry": {
              "type": "Polygon",
              "coordinates": [
                 [
                    [
                       151.18538041600002,
                       -33.8939659845
                    ],
                    [
                       151.185184928,
                       -33.8933718015
                    ],
                    [
                       151.185956032,
                       -33.893205394
                    ],
                    [
                       151.185920704,
                       -33.8931036995
                    ],
                    [
                       151.18615344,
                       -33.8930332145
                    ],
                    [
                       151.18614886400002,
                       -33.8930247415
                    ],
                    [
                       151.186198496,
                       -33.8930146405
                    ],
                    [
                       151.186202304,
                       -33.893022873
                    ],
                    [
                       151.1864536,
                       -33.893595448
                    ],
                    [
                       151.18620553600002,
                       -33.893669337
                    ],
                    [
                       151.18616032,
                       -33.8936747205
                    ],
                    [
                       151.186197248,
                       -33.8937595245
                    ],
                    [
                       151.185987712,
                       -33.893822332
                    ],
                    [
                       151.18538041600002,
                       -33.8939659845
                    ]
                 ]
              ]
           },
           "FeatureCollection": "MB_2011_NSW",
           "point": null,
           "predictive_text": "10753650000",
           "owner": "admin",
           "properties": {
              "MB_2011_NSW.MB_CODE11": "10753650000",
              "MB_2011_NSW.MB_CAT11": "Residential",
              "MB_2011_NSW.SA1_MAIN11": "11703133251",
              "MB_2011_NSW.SA1_7DIG11": "1133251",
              "MB_2011_NSW.SA2_MAIN11": "117031332",
              "MB_2011_NSW.SA2_5DIG11": "11332",
              "MB_2011_NSW.SA2_NAME11": "Newtown - Camperdown - 

```

Darlington",  
"MB\_2011\_NSW.SA3\_CODE11": "11703",  
"MB\_2011\_NSW.SA3\_NAME11": "Sydney Inner City",  
"MB\_2011\_NSW.SA4\_CODE11": "117",  
"MB\_2011\_NSW.SA4\_NAME11": "Sydney - City and Inner South",  
"MB\_2011\_NSW.GCC\_CODE11": "1GSYD",  
"MB\_2011\_NSW.GCC\_NAME11": "Greater Sydney",  
"MB\_2011\_NSW.STE\_CODE11": "1",  
"MB\_2011\_NSW.STE\_NAME11": "New South Wales",  
"MB\_2011\_NSW.ALBERS\_SQM": 7065.8147810602  
},  
"status": "usable"  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/55b586ca-1507-4389-baae-d61b77f6b030%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/55b586ca-1507-4389-baae-d61b77f6b030%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:31am UTC](https://discuss.elastic.co/t/efficient-sorting-by-distance-to-a-point/22256/4 "2017-07-06T00:31:38Z")

</div>


