# Geo distance sort

**URL:** <https://discuss.elastic.co/t/geo-distance-sort/13737>\
**Category:** Elasticsearch\
**Created:** [September 24, 2013, 2:19pm UTC](https://discuss.elastic.co/t/geo-distance-sort/13737 "2013-09-24T14:19:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iker\_Ibarguren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iker_ibarguren/32/2078_2.png) [@Iker\_Ibarguren](https://discuss.elastic.co/u/Iker_Ibarguren)\
**Post date:** [September 24, 2013, 2:19pm UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/1 "2013-09-24T14:19:45Z")

</div>

Hi!

I´m working with 80.000 records but only 2000 of them have location. This  
is my query:

$q = new \Elastica\Query\Builder('{  
"from" : 0, "size" : 100,  
"query":{"match\_all" : { }},  
"sort" : [  
{"\_geo\_distance" : {  
"location" : {  
"lat" : -2.172273,  
"lon" : 43.284734  
},  
"order" : "asc",  
"unit" : "m"}  
}  
]  
}');

how can I exclude those rows without location??

I need to obtain a list of shops withing 10km radio sorted by distance from  
a given point.

Thanks in advance

--  
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:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [September 24, 2013, 2:47pm UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/2 "2013-09-24T14:47:10Z")

</div>

Hi Iker,

You can use "geo distance filter".

See geo distance filter

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

On 2013/09/24, at 23:19, Iker Ibarguren [ikerib@gmail.com](mailto:ikerib@gmail.com) wrote:

> Hi!
> 
> I´m working with 80.000 records but only 2000 of them have location. This is my query:
> 
> $q = new \Elastica\Query\Builder('{  
> "from" : 0, "size" : 100,  
> "query":{"match\_all" : { }},  
> "sort" : [  
> {"\_geo\_distance" : {  
> "location" : {  
> "lat" : -2.172273,  
> "lon" : 43.284734  
> },  
> "order" : "asc",  
> "unit" : "m"}  
> }  
> ]  
> }');
> 
> how can I exclude those rows without location??
> 
> I need to obtain a list of shops withing 10km radio sorted by distance from a given point.
> 
> Thanks in advance
> 
> --  
> 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:** ![Iker\_Ibarguren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iker_ibarguren/32/2078_2.png) [@Iker\_Ibarguren](https://discuss.elastic.co/u/Iker_Ibarguren)\
**Post date:** [September 24, 2013, 2:52pm UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/3 "2013-09-24T14:52:40Z")

</div>

but with the filter I don´t have the distance from my location to the shop.  
(It will be a mobile app).

I tried this:

public function getgeocomercios2Action() {  
$view = FOSView::create();  
$finder =  
$this-\>container-\>get('fos\_elastica.finder.comercios.comercio');

```
    $q = new \Elastica\Query\Builder('{
        "query": {
                "filtered" : {
                    "query" : {
                        "match_all" : {}
                    },
                "filter" : {
                    "geo_distance" : {
                        "distance" : "100m",
                        "location" : {
                            "lat" : -2.1741771697998047,
                            "lon" : 43.28249657890983
                        }
                    }
                }
            }
        }

    }');

```

iKER

2013/9/24 Jun Ohtani [johtani@gmail.com](mailto:johtani@gmail.com)

> Hi Iker,
> 
> You can use "geo distance filter".
> 
> See geo distance filter  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> 
> * * *
> 
> Jun Ohtani  
> [johtani@gmail.com](mailto:johtani@gmail.com)  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> On 2013/09/24, at 23:19, Iker Ibarguren [ikerib@gmail.com](mailto:ikerib@gmail.com) wrote:
> 
> > Hi!
> > 
> > I´m working with 80.000 records but only 2000 of them have location.  
> > This is my query:
> > 
> > $q = new \Elastica\Query\Builder('{  
> > "from" : 0, "size" : 100,  
> > "query":{"match\_all" : { }},  
> > "sort" : [  
> > {"\_geo\_distance" : {  
> > "location" : {  
> > "lat" : -2.172273,  
> > "lon" : 43.284734  
> > },  
> > "order" : "asc",  
> > "unit" : "m"}  
> > }  
> > ]  
> > }');
> > 
> > how can I exclude those rows without location??
> > 
> > I need to obtain a list of shops withing 10km radio sorted by distance  
> > from a given point.
> > 
> > Thanks in advance
> > 
> > --  
> > 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 a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe](https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe).  
> To unsubscribe from this group and all its topics, 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:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [September 24, 2013, 3:12pm UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/4 "2013-09-24T15:12:30Z")

</div>

Hi Iker,

Oh sorry.  
You should use "exists filter".

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Like this.

{  
"query": {  
"filtered": {  
"query": {  
"matchAll": {}  
},  
"filter": {  
"exists": {  
"field": "location"  
}  
}  
}  
},  
"sort": [  
{  
"\_geo\_distance": {  
"location": {  
"lat": -2.172273,  
"lon": 43.284734  
},  
"order": "asc",  
"unit": "m"  
}  
}  
]  
}

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

On 2013/09/24, at 23:52, iKER- [ikerib@gmail.com](mailto:ikerib@gmail.com) wrote:

> but with the filter I don´t have the distance from my location to the shop. (It will be a mobile app).
> 
> I tried this:
> 
> public function getgeocomercios2Action() {  
> $view = FOSView::create();  
> $finder = $this-\>container-\>get('fos\_elastica.finder.comercios.comercio');
> 
> ```
> $q = new \Elastica\Query\Builder('{
> "query": {
> "filtered" : {
> "query" : {
> "match_all" : {}
> },
> "filter" : {
> "geo_distance" : {
> "distance" : "100m",
> "location" : {
> "lat" : -2.1741771697998047,
> "lon" : 43.28249657890983
> }
> }
> }
> }
> }
> 
> }');
> 
> ```
> 
> iKER
> 
> 2013/9/24 Jun Ohtani [johtani@gmail.com](mailto:johtani@gmail.com)  
> Hi Iker,
> 
> You can use "geo distance filter".
> 
> See geo distance filter  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> 
> * * *
> 
> Jun Ohtani  
> [johtani@gmail.com](mailto:johtani@gmail.com)  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> On 2013/09/24, at 23:19, Iker Ibarguren [ikerib@gmail.com](mailto:ikerib@gmail.com) wrote:
> 
> > Hi!
> > 
> > I´m working with 80.000 records but only 2000 of them have location. This is my query:
> > 
> > $q = new \Elastica\Query\Builder('{  
> > "from" : 0, "size" : 100,  
> > "query":{"match\_all" : { }},  
> > "sort" : [  
> > {"\_geo\_distance" : {  
> > "location" : {  
> > "lat" : -2.172273,  
> > "lon" : 43.284734  
> > },  
> > "order" : "asc",  
> > "unit" : "m"}  
> > }  
> > ]  
> > }');
> > 
> > how can I exclude those rows without location??
> > 
> > I need to obtain a list of shops withing 10km radio sorted by distance from a given point.
> > 
> > Thanks in advance
> > 
> > --  
> > 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 a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe](https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe).  
> To unsubscribe from this group and all its topics, 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).

--  
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:** ![Iker\_Ibarguren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iker_ibarguren/32/2078_2.png) [@Iker\_Ibarguren](https://discuss.elastic.co/u/Iker_Ibarguren)\
**Post date:** [September 25, 2013, 6:56am UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/5 "2013-09-25T06:56:35Z")

</div>

ooooohh oh!! thx!! 😉

On Tuesday, September 24, 2013 5:12:30 PM UTC+2, Jun Ohtani wrote:

> Hi Iker,
> 
> Oh sorry.  
> You should use "exists filter".  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/exists-filter/)
> 
> Like this.
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "matchAll": {}  
> },  
> "filter": {  
> "exists": {  
> "field": "location"  
> }  
> }  
> }  
> },  
> "sort": [  
> {  
> "\_geo\_distance": {  
> "location": {  
> "lat": -2.172273,  
> "lon": 43.284734  
> },  
> "order": "asc",  
> "unit": "m"  
> }  
> }  
> ]  
> }
> 
> * * *
> 
> Jun Ohtani  
> [joh...@gmail.com](mailto:joh...@gmail.com) \<javascript:\>  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> On 2013/09/24, at 23:52, iKER- \<[ike...@gmail.com](mailto:ike...@gmail.com) \<javascript:\>\> wrote:
> 
> > but with the filter I don´t have the distance from my location to the  
> > shop. (It will be a mobile app).
> > 
> > I tried this:
> > 
> > public function getgeocomercios2Action() {  
> > $view = FOSView::create();  
> > $finder =  
> > $this-\>container-\>get('fos\_elastica.finder.comercios.comercio');
> > 
> > ```
> > $q = new \Elastica\Query\Builder('{ 
> > "query": { 
> > "filtered" : { 
> > "query" : { 
> > "match_all" : {} 
> > }, 
> > "filter" : { 
> > "geo_distance" : { 
> > "distance" : "100m", 
> > "location" : { 
> > "lat" : -2.1741771697998047, 
> > "lon" : 43.28249657890983 
> > } 
> > } 
> > } 
> > } 
> > } 
> > 
> > }'); 
> > 
> > ```
> > 
> > iKER
> > 
> > 2013/9/24 Jun Ohtani \<[joh...@gmail.com](mailto:joh...@gmail.com) \<javascript:\>\>  
> > Hi Iker,
> > 
> > You can use "geo distance filter".
> > 
> > See geo distance filter
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-filter/)
> 
> > * * *
> > 
> > Jun Ohtani  
> > [joh...@gmail.com](mailto:joh...@gmail.com) \<javascript:\>  
> > twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> > 
> > On 2013/09/24, at 23:19, Iker Ibarguren \<[ike...@gmail.com](mailto:ike...@gmail.com) \<javascript:\>\>  
> > wrote:
> > 
> > > Hi!
> > > 
> > > I´m working with 80.000 records but only 2000 of them have location.  
> > > This is my query:
> > > 
> > > $q = new \Elastica\Query\Builder('{  
> > > "from" : 0, "size" : 100,  
> > > "query":{"match\_all" : { }},  
> > > "sort" : [  
> > > {"\_geo\_distance" : {  
> > > "location" : {  
> > > "lat" : -2.172273,  
> > > "lon" : 43.284734  
> > > },  
> > > "order" : "asc",  
> > > "unit" : "m"}  
> > > }  
> > > ]  
> > > }');
> > > 
> > > how can I exclude those rows without location??
> > > 
> > > I need to obtain a list of shops withing 10km radio sorted by distance  
> > > from a given point.
> > > 
> > > Thanks in advance
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > 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 a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe](https://groups.google.com/d/topic/elasticsearch/QsXBWpNyI08/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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:14am UTC](https://discuss.elastic.co/t/geo-distance-sort/13737/6 "2017-07-06T02:14:47Z")

</div>


