# Geo\_distance filter

**URL:** https://discuss.elastic.co/t/geo-distance-filter/12068
**Category:** Elasticsearch
**Created:** [May 22, 2013, 8:57am UTC](https://discuss.elastic.co/t/geo-distance-filter/12068 "2013-05-22T08:57:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mehrez\_Marouani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehrez_marouani/32/1218_2.png) [@Mehrez\_Marouani](https://discuss.elastic.co/u/Mehrez_Marouani)
#### Post date: [May 22, 2013, 8:57am UTC](https://discuss.elastic.co/t/geo-distance-filter/12068/1 "2013-05-22T08:57:38Z")

</div>

Hi,

I'm using the geo\_distance filter to lookup a list of vehicle dealers  
within a radius starting from a GeoPoint location.  
The problem is that the returned terms facets ignore the filter and return  
the whole count.  
Is there any way to get the facets aware of the filters the same as with  
queries or to achieve the same thing with the geo\_shape query ?

The document mapping:  
{  
"vehicle": {  
"properties": {  
"id": {  
"type": "long"  
},  
"label": {  
"type": "string"  
},  
"model": {  
"type": "string"  
},  
"dealer": {  
"type": "object",  
"properties": {  
"id": {  
"type": "long"  
},  
"location": {  
"type": "geo\_point"  
}  
}  
}  
}  
}  
}

The query:  
{  
"query": {  
"match\_all": {}  
},  
"filter": {  
"geo\_distance": {  
"distance": "200km",  
"pin.location": {  
"lat": 40,  
"lon": -70  
}  
}  
},  
"facets": {  
"models": {  
"terms": {  
"field": "model"  
}  
}  
}  
}

Here the model facet returns the same count as if we remove the filter.

I will really appreciate your help.

Regards,

--  
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: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [May 22, 2013, 9:10am UTC](https://discuss.elastic.co/t/geo-distance-filter/12068/2 "2013-05-22T09:10:06Z")

</div>

Hey,

if you want the facets to use the filter, use a filtered query inside of  
your "query" field which incorporates the "filter" field. A top level  
filter is never used for facets. Alternatively you can also add facet  
filters to your facet query in order to reduce results.

Hope this helps.

--Alex

On Wed, May 22, 2013 at 10:57 AM, Mehrez Marouani \<[mehrez.marouani@gmail.com](mailto:mehrez.marouani@gmail.com)

> wrote:

> Hi,
> 
> I'm using the geo\_distance filter to lookup a list of vehicle dealers  
> within a radius starting from a GeoPoint location.  
> The problem is that the returned terms facets ignore the filter and return  
> the whole count.  
> Is there any way to get the facets aware of the filters the same as with  
> queries or to achieve the same thing with the geo\_shape query ?
> 
> The document mapping:  
> {  
> "vehicle": {  
> "properties": {  
> "id": {  
> "type": "long"  
> },  
> "label": {  
> "type": "string"  
> },  
> "model": {  
> "type": "string"  
> },  
> "dealer": {  
> "type": "object",  
> "properties": {  
> "id": {  
> "type": "long"  
> },  
> "location": {  
> "type": "geo\_point"  
> }  
> }  
> }  
> }  
> }  
> }
> 
> The query:  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "geo\_distance": {  
> "distance": "200km",  
> "pin.location": {  
> "lat": 40,  
> "lon": -70  
> }  
> }  
> },  
> "facets": {  
> "models": {  
> "terms": {  
> "field": "model"  
> }  
> }  
> }  
> }
> 
> Here the model facet returns the same count as if we remove the filter.
> 
> I will really appreciate your help.
> 
> Regards,
> 
> --  
> 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: ![Mehrez\_Marouani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mehrez_marouani/32/1218_2.png) [@Mehrez\_Marouani](https://discuss.elastic.co/u/Mehrez_Marouani)
#### Post date: [May 22, 2013, 10:01am UTC](https://discuss.elastic.co/t/geo-distance-filter/12068/3 "2013-05-22T10:01:31Z")

</div>

It's working perfectly. Thanks you Alex for your clear explanation 🙂

Regards,

On Wednesday, May 22, 2013 11:10:06 AM UTC+2, Alexander Reelsen wrote:

> Hey,
> 
> if you want the facets to use the filter, use a filtered query inside of  
> your "query" field which incorporates the "filter" field. A top level  
> filter is never used for facets. Alternatively you can also add facet  
> filters to your facet query in order to reduce results.
> 
> Hope this helps.
> 
> --Alex
> 
> On Wed, May 22, 2013 at 10:57 AM, Mehrez Marouani \<[mehrez....@gmail.com](mailto:mehrez....@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,
> > 
> > I'm using the geo\_distance filter to lookup a list of vehicle dealers  
> > within a radius starting from a GeoPoint location.  
> > The problem is that the returned terms facets ignore the filter and  
> > return the whole count.  
> > Is there any way to get the facets aware of the filters the same as with  
> > queries or to achieve the same thing with the geo\_shape query ?
> > 
> > The document mapping:  
> > {  
> > "vehicle": {  
> > "properties": {  
> > "id": {  
> > "type": "long"  
> > },  
> > "label": {  
> > "type": "string"  
> > },  
> > "model": {  
> > "type": "string"  
> > },  
> > "dealer": {  
> > "type": "object",  
> > "properties": {  
> > "id": {  
> > "type": "long"  
> > },  
> > "location": {  
> > "type": "geo\_point"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > The query:  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "geo\_distance": {  
> > "distance": "200km",  
> > "pin.location": {  
> > "lat": 40,  
> > "lon": -70  
> > }  
> > }  
> > },  
> > "facets": {  
> > "models": {  
> > "terms": {  
> > "field": "model"  
> > }  
> > }  
> > }  
> > }
> > 
> > Here the model facet returns the same count as if we remove the filter.
> > 
> > I will really appreciate your help.
> > 
> > Regards,
> > 
> > --  
> > 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:35am UTC](https://discuss.elastic.co/t/geo-distance-filter/12068/4 "2017-07-06T02:35:26Z")

</div>


