# My Geo Distance Query

**URL:** <https://discuss.elastic.co/t/my-geo-distance-query/144096>\
**Category:** Elasticsearch\
**Created:** [August 13, 2018, 7:37am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096 "2018-08-13T07:37:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orange-jacky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orange-jacky/32/34416_2.png) [@orange-jacky](https://discuss.elastic.co/u/orange-jacky)\
**Post date:** [August 13, 2018, 7:37am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096/1 "2018-08-13T07:37:16Z")

</div>

hello, anyone  
i have a geo distance query function, and anyone can tell me how to realize it with elastichsearch.

hope replies. thank you

description:  
there are lots of records, one record one line, every record has a field "positions"  
the data format of "positions" is an array, the element is also an array, and it has three fields, first field is lat,  
the second field is lon, the last field is distance, unit is meter, such as  
"postitions" : [[125.3721,52.7839,50],[128.3799,52.7639,100]] , or "postitions": []  
there is a request param {"lat":xxxxx, "lon":yyyy }, i hope it will calculate  
the distance with postitions,  
the process is:  
1. calculate {"lat":xxxxx, "lon":yyyy } and the first element of postitions [125.3721,52.7839,50] , and the result distance is low to 50 the thrid filed of the first element, it matchs or , it not match  
2. calculate {"lat":xxxxx, "lon":yyyy } and the second element of postitions [128.3799,52.7639,100] , and the result distance is low to 100 the thrid filed of the first element, it matchs or , it not match  
3. element of postitions has only one match {"lat":xxxxx, "lon":yyyy } it will return the reocord  
4. if postitions is empty, it will match any {"lat":xxxxx, "lon":yyyy } , and it will return the record

---

<div class="post-metadata">

**Author:** ![Ethan-Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ethan-zhang/32/54812_2.png) [@Ethan-Zhang](https://discuss.elastic.co/u/Ethan-Zhang)\
**Post date:** [August 13, 2018, 9:51am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096/2 "2018-08-13T09:51:42Z")

</div>

You may define you field positions as type of Geo-point. ES will save the the pair of latitude and longitude in one Geo-point field.  
Here is an Example  
PUT my\_index/\_doc/1  
{  
"location": {  
"lat": 41.12,  
"lon": -71.34  
}  
}

And then you can search all the points you saved  
within the distance of the pin point using a geo distance query.

---

<div class="post-metadata">

**Author:** ![orange-jacky](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orange-jacky/32/34416_2.png) [@orange-jacky](https://discuss.elastic.co/u/orange-jacky)\
**Post date:** [August 14, 2018, 9:49am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096/3 "2018-08-14T09:49:39Z")

</div>

thank you Ethan-zhang. it's not my hope. the data format of postitions is fixed, such as "postitions" : [[125.3721,52.7839,50],[128.3799,52.7639,100]], i want input [125.3721,52.7839] named p, p need compare every element of postitions. such as  
[125.3721,52.7839] compare [125.3721,52.7839,50] calculate the distance name d, d \< 50, it match. or not match.  
[125.3721,52.7839] compare ,[128.3799,52.7639,100] calculate the distance name d, d \< 100, it match. or not match.

if one match , it think this record match and return it

and there are 40 million records

---

<div class="post-metadata">

**Author:** ![Ethan-Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ethan-zhang/32/54812_2.png) [@Ethan-Zhang](https://discuss.elastic.co/u/Ethan-Zhang)\
**Post date:** [August 15, 2018, 6:52am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096/4 "2018-08-15T06:52:47Z")

</div>

In this case, you probably need to calculate the distance by youself. 😁

---

<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:** [September 12, 2018, 6:52am UTC](https://discuss.elastic.co/t/my-geo-distance-query/144096/5 "2018-09-12T06:52:48Z")

</div>

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