# Geo\_distance not working

**URL:** <https://discuss.elastic.co/t/geo-distance-not-working/55151>\
**Category:** Elasticsearch\
**Created:** [July 11, 2016, 4:46am UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151 "2016-07-11T04:46:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabiriqbal](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sabiriqbal](https://discuss.elastic.co/u/sabiriqbal)\
**Post date:** [July 11, 2016, 4:46am UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/1 "2016-07-11T04:46:34Z")

</div>

I am trying to write geo\_distance query, but not getting correct result.  
I am using Elasticsearch 2.3.3  
I have two documents one is located in Los Angeles and other one is in New York.  
I am trying to get document near Los Angeles. But as a result I am getting two documents.  
Below here is all my requests.

1. Create index

POST: [http://localhost:9200/retail](http://localhost:9200/retail)  
{  
"settings": {  
"number\_of\_shards": 1,  
"number\_of\_replicas": 0  
},  
"mappings": {  
"restaurant": {  
"properties": {  
"location": {"type": "geo\_point"}  
}  
}  
}  
}

1. Indexed documents  
PUT: [http://localhost:9200/retail/restaurant/1](http://localhost:9200/retail/restaurant/1)  
{  
"name": "Restaurant1",  
"description": "Nice restaurant",  
"address": {  
"street": "464 Main St",  
"City": "Los Angeles",  
"State": "CA",  
"zip": "90013"  
},  
"location": {  
"lat": 34.0466,  
"lon": -118.248144  
}  
"tags": [  
"italian",  
"spaghetti",  
"pasta"  
],  
"rating": "4.5"  
}

PUT: [http://localhost:9200/retail/restaurant/2](http://localhost:9200/retail/restaurant/2)

{  
"name": "Restaurant2",  
"description": "Good restaurant",  
"address": {  
"street": "230 W 4th St",  
"City": "New York",  
"State": "NY",  
"zip": "10014"  
},  
"location": {  
"lat": 40.7543385,  
"lon": -73.976313  
},  
"tags": [  
"maxican",  
"tacons",  
"burritos"  
],  
"rating": "4.3"  
}

Query string : as per [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html)

GET: [http://localhost:9200/retail/restaurant/\_search](http://localhost:9200/retail/restaurant/_search)  
{  
"bool" : {  
"must" : {  
"match\_all" : {}  
},  
"filter" : {  
"geo\_distance" : {  
"distance" : "2km",  
"location" : {  
"lat": 34.0466,  
"lon": -118.248144  
}  
}  
}  
}  
}

Total result found 2 but expected 1.

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [July 11, 2016, 5:59am UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/2 "2016-07-11T05:59:09Z")

</div>

Are you sure your index only contains the two documents you've listed and you don't have some older test data in there?

---

<div class="post-metadata">

**Author:** ![sabiriqbal](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sabiriqbal](https://discuss.elastic.co/u/sabiriqbal)\
**Post date:** [July 11, 2016, 12:22pm UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/3 "2016-07-11T12:22:41Z")

</div>

This is my test index. I created it only for this post. Just to make sure geo distance works fine so that I can use the same for my production index which has 1 million data.  
Yes I do not have any other document under this index.  
And I did check the output. Output has two sources which are same as documents I indexed under this index.  
I am looking for your kind help.  
Thanks

---

<div class="post-metadata">

**Author:** ![sabiriqbal](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sabiriqbal](https://discuss.elastic.co/u/sabiriqbal)\
**Post date:** [July 11, 2016, 11:15pm UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/4 "2016-07-11T23:15:59Z")

</div>

Finally I am able to get proper result.  
I used below query. With POST request. Previously I was using GET.  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"geo\_distance": {  
"distance": "10000km",  
"location": {  
"lat": 40.7543385,  
"lon": -73.976313  
}  
}  
}  
}  
}  
}

Thanks Joshua for your response. I appreciate your time.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 12, 2016, 4:28am UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/5 "2016-07-12T04:28:39Z")

</div>

Some clients don't support GET with body.  
IIRC Sense rewrite GET with body requests to POST requests.

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [July 13, 2016, 6:49am UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/6 "2016-07-13T06:49:34Z")

</div>

Good catch @sabiriqbal. As @dadoonet mentions, you can use GET or POST for this request, but some clients don't support a GET request with a body. If unsure, use POST 🙂

---

<div class="post-metadata">

**Author:** ![sabiriqbal](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sabiriqbal](https://discuss.elastic.co/u/sabiriqbal)\
**Post date:** [July 13, 2016, 10:27pm UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/7 "2016-07-13T22:27:04Z")

</div>

Thanks @dadoonet and @Joshua_Rich for your kind help. Yes I got it. I was using Elasticsearch "head" plugin to execute test query. As David said "some client doesn't support GET with body", I think "head" plugin is one of those kind of client.

---

<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 5, 2017, 10:35pm UTC](https://discuss.elastic.co/t/geo-distance-not-working/55151/8 "2017-07-05T22:35:55Z")

</div>


