Thanks for the reply! Here's a better description.
In order to find the org nearest our user, we need to search over the set of locations comprised of each org's nearest location. If I have a potential org with offices 5 miles away, 25 miles away and 15 miles away, I should only be concerned about the one 5 miles away. distance_feature does not support that case. It only takes into account one location for each org, the one that is stored last in the index. If stored as I listed them above, this would be the 15-mile location.
Distance is dependent on each user’s location and on each org location, so we can never order the org location storage by distance. The distance will change for every user at runtime.
Put another way, if we have orgs stored in the order below with distances from a given user:
A: 5 mi, 25 mi, 15 mi
B: 10 mi
C: 2 mi, 30 mi
D: 14 mi, 16 mi, 11 mi
We should order them FOR THIS USER like this
C: 2, A: 5, B: 10, D: 11
With distance_feature query, we order them like this
B: 10, D: 11, A: 15, C: 30
And our front end finds the closest distance (we use a painless script to calculate each distance and send those out as results) for each provider, so we display them like this
B: 10, D: 11, A: 5, C: 2
Note that the documentation never addresses the case of having a list of geo_points for each item.
I have tried painless script_fields, but they cannot be used in the search query, only indexed fields can. Also, they cannot be used in another painless script, so I can't solve the problem piecewise.
I would try runtime_mapping, also called runtime_fields in some mentions, but unfortunately my management moved from ES to OS and it doesn't have that.
I have tried sorting in Python after the fact, but then that clobbers some of the other sorting we do inside the query.
Thanks for your time.