Efficient sorting by distance to a point

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.
:slight_smile:

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/5bc53bbe-dc00-40bf-97cb-33b54fa81802%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a550bdd2-64f6-4c1f-95b0-55c61923beba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/55b586ca-1507-4389-baae-d61b77f6b030%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.