# Is it possible to compute the geo distance for an array of sub objects containing a geo point

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634>\
**Category:** Elasticsearch\
**Created:** [September 17, 2013, 12:42am UTC](https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634 "2013-09-17T00:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicktackes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicktackes/32/1174_2.png) [@nicktackes](https://discuss.elastic.co/u/nicktackes)\
**Post date:** [September 17, 2013, 12:42am UTC](https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634/1 "2013-09-17T00:42:19Z")

</div>

I have an index describing clinical trials. Often a clinical trial has  
multiple locations that a patient can receive care at. Ideally, I would  
like to compute the distance from a user's current geolocation to each of  
the trial locations, so that I can display the distance to the closest  
location pertaining to the trial.

I attempted to used a script\_field in order to do this.  
I created a gist that creates my simple index and populates some data and  
then attempts to calculate the shortest distance from current location.  
Any insight as to what I can do to resolve this issue is greatly  
appreciated.

> <https://gist.github.com/nicktackes/6588662>

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![nicktackes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicktackes/32/1174_2.png) [@nicktackes](https://discuss.elastic.co/u/nicktackes)\
**Post date:** [September 17, 2013, 12:53am UTC](https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634/2 "2013-09-17T00:53:52Z")

</div>

one other note. I cant sort by distance. I sort based on a custom scoring  
algorithm that determines which clinical trials match a patients condition.

On Monday, September 16, 2013 5:42:19 PM UTC-7, Nick Tackes wrote:

> I have an index describing clinical trials. Often a clinical trial has  
> multiple locations that a patient can receive care at. Ideally, I would  
> like to compute the distance from a user's current geolocation to each of  
> the trial locations, so that I can display the distance to the closest  
> location pertaining to the trial.
> 
> I attempted to used a script\_field in order to do this.  
> I created a gist that creates my simple index and populates some data and  
> then attempts to calculate the shortest distance from current location.  
> Any insight as to what I can do to resolve this issue is greatly  
> appreciated.
> 
> [clinical trials with multiple locations. Attempt to find the distance from your current geo location to the closest trial location · GitHub](https://gist.github.com/nicktackes/6588662)

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [September 17, 2013, 4:34pm UTC](https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634/3 "2013-09-17T16:34:10Z")

</div>

Hi Nick

You could use `sort_mode: min` in your \_geo\_distance sort clause to get the  
minimum distance. Just pull the value out of the `sort` key in the search  
results. (Note: it SHOULD be called "sort\_mode" but is actually called  
"mode" in released versions of ES. I've just fixed that in master)

See: [gist:6596872 · GitHub](https://gist.github.com/clintongormley/6596872)

clint

On 17 September 2013 02:53, Nick Tackes [ntackes@gmail.com](mailto:ntackes@gmail.com) wrote:

> one other note. I cant sort by distance. I sort based on a custom  
> scoring algorithm that determines which clinical trials match a patients  
> condition.
> 
> On Monday, September 16, 2013 5:42:19 PM UTC-7, Nick Tackes wrote:
> 
> > I have an index describing clinical trials. Often a clinical trial has  
> > multiple locations that a patient can receive care at. Ideally, I would  
> > like to compute the distance from a user's current geolocation to each of  
> > the trial locations, so that I can display the distance to the closest  
> > location pertaining to the trial.
> > 
> > I attempted to used a script\_field in order to do this.  
> > I created a gist that creates my simple index and populates some data and  
> > then attempts to calculate the shortest distance from current location.  
> > Any insight as to what I can do to resolve this issue is greatly  
> > appreciated.
> > 
> > [https://gist.github.com/\*\*nicktackes/6588662](https://gist.github.com/**nicktackes/6588662)[https://gist.github.com/nicktackes/6588662](https://gist.github.com/nicktackes/6588662)
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:15am UTC](https://discuss.elastic.co/t/is-it-possible-to-compute-the-geo-distance-for-an-array-of-sub-objects-containing-a-geo-point/13634/4 "2017-07-06T02:15:58Z")

</div>


