# Geo\_distance\_range filter applied to a facet returns values outside the range

**URL:** <https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571>\
**Category:** Elasticsearch\
**Created:** [May 6, 2012, 1:37am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571 "2012-05-06T01:37:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)\
**Post date:** [May 6, 2012, 1:37am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571/1 "2012-05-06T01:37:21Z")

</div>

Hello!

I have a list of documents that contains geoloc field which is a geo\_point  
type as seen in the mapping below (some fields has been stripped).

{  
"schools": {  
"properties": {  
....  
"geoloc": {  
"store": "yes",  
"type": "geo\_point"  
},  
"country": {  
"index": "not\_analyzed",  
"store": "yes",  
"type": "string"  
},  
"username": {  
"index": "not\_analyzed",  
"type": "string"  
}  
....  
....  
}  
}  
}

The default facet attached to the query is a geo\_distance facet using  
to/from to define range as seen below (again some range were stripped to  
shorten this post).

"facets": {  
"geoloc": {  
"geo\_distance": {  
"ranges": [  
….  
….  
{  
"from": "7001",  
"to": "8000"  
},  
{  
"from": "8001",  
"to": "9000"  
},  
{  
"from": "9001",  
"to": "10000"  
},  
{  
"from": "10001",  
"to": "50000"  
}  
],  
"geoloc": {  
"lat": 51.5128,  
"lon": -0.0918  
},  
"unit": "miles"  
}  
}  
}

The above facet query returns the following response.  
"facets": {  
"geoloc": {  
"\_type": "geo\_distance",  
"ranges": [  
....  
....  
{  
"from": 6001,  
"to": 7000,  
"min": "Infinity",  
"max": "-Infinity",  
"total\_count": 0,  
"total": 0,  
"mean": "NaN"  
},  
{  
"from": 7001,  
"to": 8000,  
"min": 7971.960904721318,  
"max": 7971.960904721318,  
"total\_count": 4,  
"total": 31887.843618885272,  
"mean": 7971.960904721318  
},  
{  
"from": 8001,  
"to": 9000,  
"min": 8127.135465210893,  
"max": 8876.39022791625,  
"total\_count": 8,  
"total": 67474.56494917188,  
"mean": 8434.320618646485  
},  
{  
"from": 9001,  
"to": 10000,  
"min": 9094.99960175981,  
"max": 9998.014376489728,  
"total\_count": 1533,  
"total": 14749196.606093463,  
"mean": 9621.132815455618  
},  
{  
"from": 10001,  
"to": 50000,  
"min": 10107.137498621609,  
"max": 11991.886489810546,  
"total\_count": 1241,  
"total": 12977180.049243744,  
"mean": 10457.034689156926  
}  
]  
}  
}

_Effective Range with counts_

7,001 - 8,000 miles (4)

8,001 - 9,000 miles (8)

9,001 - 10,000 miles (1,531)

More than 10,000 miles (1,239)

In my application, I selected 7001 to 8000 miles with (4) expected records  
within this range. The following filters are applied to the new  
Request/Query:

_Query Filter:_

"filter": {

```
"and": [
  {
    "geo_distance_range": {
      "from": "7001 mi",
      "to": "8000 mi",
      "geoloc": {
        "lat": 51.5128,
        "lon": -0.0918
      }
    }
  }
]

```

}

_Facet Filter (geoloc):_

"facets": {  
"geoloc": {  
"facet\_filter": {  
"and": [  
{  
"geo\_distance\_range": {  
"from": "7001 mi",  
"to": "8000 mi",  
"geoloc": {  
"lat": 51.5128,  
"lon": -0.0918  
}  
}

```
      }
    ]
  },
  "geo_distance": {
    "ranges": [
      {
        "from": "7001",
        "to": "8000"
      },
      {
        "from": "8001",
        "to": "9000"
      },
      {
        "from": "9001",
        "to": "10000"
      },
      {
        "from": "10001",
        "to": "50000"
      }
    ],
    "geoloc": {
      "lat": 51.5128,
      "lon": -0.0918
    },

    "unit": "miles"
  }
}

```

The Response contains the following facet values:

"facets": {  
"geoloc": {  
"\_type": "geo\_distance",  
"ranges": [  
…  
...  
{  
"from": 6001,  
"to": 7000,  
"min": "Infinity",  
"max": "-Infinity",  
"total\_count": 0,  
"total": 0,  
"mean": "NaN"  
},  
{  
"from": 7001,  
"to": 8000,  
"min": 7971.960904721318,  
"max": 7971.960904721318,  
"total\_count": 4,  
"total": 31887.843618885272,  
"mean": 7971.960904721318  
},  
{  
"from": 8001,  
"to": 9000,  
"min": "Infinity",  
"max": "-Infinity",  
"total\_count": 0,  
"total": 0,  
"mean": "NaN"  
},  
{  
"from": 9001,  
"to": 10000,  
"min": "Infinity",  
"max": "-Infinity",  
"total\_count": 0,  
"total": 0,  
"mean": "NaN"  
},  
{  
"from": 10001,  
"to": 50000,  
"min": 10214.110601712535,  
"max": 10214.110601712535,  
"total\_count": 4,  
"total": 40856.44240685014,  
"mean": 10214.110601712535  
}  
]  
}  
}

\*Range with values: \*

- 7001 - 8000 (4 records)
- 10001 - 50000 (4 records)

\*Other observations: \*

- Sending 7001 to 8000 range returns four records
- Sending 10001 to 50000 range returns greater than four records.

ElasticSearch version Used:

- 0.19.3
- 0.17.6

Has anyone experience this kind of behavior? It looks like  
geo\_distance\_range is not working correctly for me or I maybe I am missing  
something.

Thank you!

Raul

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 9, 2012, 8:43am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571/2 "2012-05-09T08:43:54Z")

</div>

By default, the geo distance range filter includes both from and to values.  
You get back 7001-8000, and then do 7000-8000 (inclusive), maybe thats the  
problem? If not, can you please gist a sample repro indexing some sample  
data and showing it? it will be fastest to check.

On Sun, May 6, 2012 at 4:37 AM, Raul, Jr. Martinez [juneym@gmail.com](mailto:juneym@gmail.com) wrote:

> Hello!
> 
> I have a list of documents that contains geoloc field which is a geo\_point  
> type as seen in the mapping below (some fields has been stripped).
> 
> {  
> "schools": {  
> "properties": {  
> ....  
> "geoloc": {  
> "store": "yes",  
> "type": "geo\_point"  
> },  
> "country": {  
> "index": "not\_analyzed",  
> "store": "yes",  
> "type": "string"  
> },  
> "username": {  
> "index": "not\_analyzed",  
> "type": "string"  
> }  
> ....  
> ....  
> }  
> }  
> }
> 
> The default facet attached to the query is a geo\_distance facet using  
> to/from to define range as seen below (again some range were stripped to  
> shorten this post).
> 
> "facets": {  
> "geoloc": {  
> "geo\_distance": {  
> "ranges": [  
> ….  
> ….  
> {  
> "from": "7001",  
> "to": "8000"  
> },  
> {  
> "from": "8001",  
> "to": "9000"  
> },  
> {  
> "from": "9001",  
> "to": "10000"  
> },  
> {  
> "from": "10001",  
> "to": "50000"  
> }  
> ],  
> "geoloc": {  
> "lat": 51.5128,  
> "lon": -0.0918  
> },  
> "unit": "miles"  
> }  
> }  
> }
> 
> The above facet query returns the following response.  
> "facets": {  
> "geoloc": {  
> "\_type": "geo\_distance",  
> "ranges": [  
> ....  
> ....  
> {  
> "from": 6001,  
> "to": 7000,  
> "min": "Infinity",  
> "max": "-Infinity",  
> "total\_count": 0,  
> "total": 0,  
> "mean": "NaN"  
> },  
> {  
> "from": 7001,  
> "to": 8000,  
> "min": 7971.960904721318,  
> "max": 7971.960904721318,  
> "total\_count": 4,  
> "total": 31887.843618885272,  
> "mean": 7971.960904721318  
> },  
> {  
> "from": 8001,  
> "to": 9000,  
> "min": 8127.135465210893,  
> "max": 8876.39022791625,  
> "total\_count": 8,  
> "total": 67474.56494917188,  
> "mean": 8434.320618646485  
> },  
> {  
> "from": 9001,  
> "to": 10000,  
> "min": 9094.99960175981,  
> "max": 9998.014376489728,  
> "total\_count": 1533,  
> "total": 14749196.606093463,  
> "mean": 9621.132815455618  
> },  
> {  
> "from": 10001,  
> "to": 50000,  
> "min": 10107.137498621609,  
> "max": 11991.886489810546,  
> "total\_count": 1241,  
> "total": 12977180.049243744,  
> "mean": 10457.034689156926  
> }  
> ]  
> }  
> }
> 
> _Effective Range with counts_
> 
> 7,001 - 8,000 miles (4)
> 
> 8,001 - 9,000 miles (8)
> 
> 9,001 - 10,000 miles (1,531)
> 
> More than 10,000 miles (1,239)
> 
> In my application, I selected 7001 to 8000 miles with (4) expected records  
> within this range. The following filters are applied to the new  
> Request/Query:
> 
> _Query Filter:_
> 
> "filter": {
> 
> ```
> "and": [
> {
> "geo_distance_range": {
> "from": "7001 mi",
> "to": "8000 mi",
> "geoloc": {
> "lat": 51.5128,
> "lon": -0.0918
> }
> }
> }
> ]
> 
> ```
> 
> }
> 
> _Facet Filter (geoloc):_
> 
> "facets": {  
> "geoloc": {  
> "facet\_filter": {  
> "and": [  
> {  
> "geo\_distance\_range": {  
> "from": "7001 mi",  
> "to": "8000 mi",  
> "geoloc": {  
> "lat": 51.5128,  
> "lon": -0.0918  
> }  
> }
> 
> ```
> }
> ]
> },
> "geo_distance": {
> "ranges": [
> {
> "from": "7001",
> "to": "8000"
> },
> {
> "from": "8001",
> "to": "9000"
> },
> {
> "from": "9001",
> "to": "10000"
> },
> {
> "from": "10001",
> "to": "50000"
> }
> ],
> "geoloc": {
> "lat": 51.5128,
> "lon": -0.0918
> },
> 
> "unit": "miles"
> }
> }
> 
> ```
> 
> The Response contains the following facet values:
> 
> "facets": {  
> "geoloc": {  
> "\_type": "geo\_distance",  
> "ranges": [  
> …  
> ...  
> {  
> "from": 6001,  
> "to": 7000,  
> "min": "Infinity",  
> "max": "-Infinity",  
> "total\_count": 0,  
> "total": 0,  
> "mean": "NaN"  
> },  
> {  
> "from": 7001,  
> "to": 8000,  
> "min": 7971.960904721318,  
> "max": 7971.960904721318,  
> "total\_count": 4,  
> "total": 31887.843618885272,  
> "mean": 7971.960904721318  
> },  
> {  
> "from": 8001,  
> "to": 9000,  
> "min": "Infinity",  
> "max": "-Infinity",  
> "total\_count": 0,  
> "total": 0,  
> "mean": "NaN"  
> },  
> {  
> "from": 9001,  
> "to": 10000,  
> "min": "Infinity",  
> "max": "-Infinity",  
> "total\_count": 0,  
> "total": 0,  
> "mean": "NaN"  
> },  
> {  
> "from": 10001,  
> "to": 50000,  
> "min": 10214.110601712535,  
> "max": 10214.110601712535,  
> "total\_count": 4,  
> "total": 40856.44240685014,  
> "mean": 10214.110601712535  
> }  
> ]  
> }  
> }
> 
> \*Range with values: \*
> 
> - 7001 - 8000 (4 records)
> - 10001 - 50000 (4 records)
> 
> \*Other observations: \*
> 
> - Sending 7001 to 8000 range returns four records
> - Sending 10001 to 50000 range returns greater than four records.
> 
> Elasticsearch version Used:
> 
> - 0.19.3
> - 0.17.6
> 
> Has anyone experience this kind of behavior? It looks like  
> geo\_distance\_range is not working correctly for me or I maybe I am missing  
> something.
> 
> Thank you!
> 
> Raul

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 9, 2012, 8:52am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571/3 "2012-05-09T08:52:05Z")

</div>

One more thing, the geo distance facet is inclusive of the lower end, and  
exclusive of the upper end, so you should have ranges like this: 7000-8000,  
8000-9000, and then, the geo\_distance\_filter range should be from: 7000,  
to: 8000, include\_upper: false.

On Wed, May 9, 2012 at 11:43 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> By default, the geo distance range filter includes both from and to  
> values. You get back 7001-8000, and then do 7000-8000 (inclusive), maybe  
> thats the problem? If not, can you please gist a sample repro indexing some  
> sample data and showing it? it will be fastest to check.
> 
> On Sun, May 6, 2012 at 4:37 AM, Raul, Jr. Martinez [juneym@gmail.com](mailto:juneym@gmail.com)wrote:
> 
> > Hello!
> > 
> > I have a list of documents that contains geoloc field which is a  
> > geo\_point type as seen in the mapping below (some fields has been stripped).
> > 
> > {  
> > "schools": {  
> > "properties": {  
> > ....  
> > "geoloc": {  
> > "store": "yes",  
> > "type": "geo\_point"  
> > },  
> > "country": {  
> > "index": "not\_analyzed",  
> > "store": "yes",  
> > "type": "string"  
> > },  
> > "username": {  
> > "index": "not\_analyzed",  
> > "type": "string"  
> > }  
> > ....  
> > ....  
> > }  
> > }  
> > }
> > 
> > The default facet attached to the query is a geo\_distance facet using  
> > to/from to define range as seen below (again some range were stripped to  
> > shorten this post).
> > 
> > "facets": {  
> > "geoloc": {  
> > "geo\_distance": {  
> > "ranges": [  
> > ….  
> > ….  
> > {  
> > "from": "7001",  
> > "to": "8000"  
> > },  
> > {  
> > "from": "8001",  
> > "to": "9000"  
> > },  
> > {  
> > "from": "9001",  
> > "to": "10000"  
> > },  
> > {  
> > "from": "10001",  
> > "to": "50000"  
> > }  
> > ],  
> > "geoloc": {  
> > "lat": 51.5128,  
> > "lon": -0.0918  
> > },  
> > "unit": "miles"  
> > }  
> > }  
> > }
> > 
> > The above facet query returns the following response.  
> > "facets": {  
> > "geoloc": {  
> > "\_type": "geo\_distance",  
> > "ranges": [  
> > ....  
> > ....  
> > {  
> > "from": 6001,  
> > "to": 7000,  
> > "min": "Infinity",  
> > "max": "-Infinity",  
> > "total\_count": 0,  
> > "total": 0,  
> > "mean": "NaN"  
> > },  
> > {  
> > "from": 7001,  
> > "to": 8000,  
> > "min": 7971.960904721318,  
> > "max": 7971.960904721318,  
> > "total\_count": 4,  
> > "total": 31887.843618885272,  
> > "mean": 7971.960904721318  
> > },  
> > {  
> > "from": 8001,  
> > "to": 9000,  
> > "min": 8127.135465210893,  
> > "max": 8876.39022791625,  
> > "total\_count": 8,  
> > "total": 67474.56494917188,  
> > "mean": 8434.320618646485  
> > },  
> > {  
> > "from": 9001,  
> > "to": 10000,  
> > "min": 9094.99960175981,  
> > "max": 9998.014376489728,  
> > "total\_count": 1533,  
> > "total": 14749196.606093463,  
> > "mean": 9621.132815455618  
> > },  
> > {  
> > "from": 10001,  
> > "to": 50000,  
> > "min": 10107.137498621609,  
> > "max": 11991.886489810546,  
> > "total\_count": 1241,  
> > "total": 12977180.049243744,  
> > "mean": 10457.034689156926  
> > }  
> > ]  
> > }  
> > }
> > 
> > _Effective Range with counts_
> > 
> > 7,001 - 8,000 miles (4)
> > 
> > 8,001 - 9,000 miles (8)
> > 
> > 9,001 - 10,000 miles (1,531)
> > 
> > More than 10,000 miles (1,239)
> > 
> > In my application, I selected 7001 to 8000 miles with (4) expected  
> > records within this range. The following filters are applied to the new  
> > Request/Query:
> > 
> > _Query Filter:_
> > 
> > "filter": {
> > 
> > ```
> > "and": [
> > {
> > "geo_distance_range": {
> > "from": "7001 mi",
> > "to": "8000 mi",
> > "geoloc": {
> > "lat": 51.5128,
> > "lon": -0.0918
> > }
> > }
> > }
> > ]
> > 
> > ```
> > 
> > }
> > 
> > _Facet Filter (geoloc):_
> > 
> > "facets": {  
> > "geoloc": {  
> > "facet\_filter": {  
> > "and": [  
> > {  
> > "geo\_distance\_range": {  
> > "from": "7001 mi",  
> > "to": "8000 mi",  
> > "geoloc": {  
> > "lat": 51.5128,  
> > "lon": -0.0918  
> > }  
> > }
> > 
> > ```
> > }
> > ]
> > },
> > "geo_distance": {
> > "ranges": [
> > {
> > "from": "7001",
> > "to": "8000"
> > },
> > {
> > "from": "8001",
> > "to": "9000"
> > },
> > {
> > "from": "9001",
> > "to": "10000"
> > },
> > {
> > "from": "10001",
> > "to": "50000"
> > }
> > ],
> > "geoloc": {
> > "lat": 51.5128,
> > "lon": -0.0918
> > },
> > 
> > "unit": "miles"
> > }
> > }
> > 
> > ```
> > 
> > The Response contains the following facet values:
> > 
> > "facets": {  
> > "geoloc": {  
> > "\_type": "geo\_distance",  
> > "ranges": [  
> > …  
> > ...  
> > {  
> > "from": 6001,  
> > "to": 7000,  
> > "min": "Infinity",  
> > "max": "-Infinity",  
> > "total\_count": 0,  
> > "total": 0,  
> > "mean": "NaN"  
> > },  
> > {  
> > "from": 7001,  
> > "to": 8000,  
> > "min": 7971.960904721318,  
> > "max": 7971.960904721318,  
> > "total\_count": 4,  
> > "total": 31887.843618885272,  
> > "mean": 7971.960904721318  
> > },  
> > {  
> > "from": 8001,  
> > "to": 9000,  
> > "min": "Infinity",  
> > "max": "-Infinity",  
> > "total\_count": 0,  
> > "total": 0,  
> > "mean": "NaN"  
> > },  
> > {  
> > "from": 9001,  
> > "to": 10000,  
> > "min": "Infinity",  
> > "max": "-Infinity",  
> > "total\_count": 0,  
> > "total": 0,  
> > "mean": "NaN"  
> > },  
> > {  
> > "from": 10001,  
> > "to": 50000,  
> > "min": 10214.110601712535,  
> > "max": 10214.110601712535,  
> > "total\_count": 4,  
> > "total": 40856.44240685014,  
> > "mean": 10214.110601712535  
> > }  
> > ]  
> > }  
> > }
> > 
> > \*Range with values: \*
> > 
> > - 7001 - 8000 (4 records)
> > - 10001 - 50000 (4 records)
> > 
> > \*Other observations: \*
> > 
> > - Sending 7001 to 8000 range returns four records
> > - Sending 10001 to 50000 range returns greater than four records.
> > 
> > Elasticsearch version Used:
> > 
> > - 0.19.3
> > - 0.17.6
> > 
> > Has anyone experience this kind of behavior? It looks like  
> > geo\_distance\_range is not working correctly for me or I maybe I am missing  
> > something.
> > 
> > Thank you!
> > 
> > Raul

---

<div class="post-metadata">

**Author:** ![Raul\_Jr\_Martinez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raul_jr_martinez/32/1286_2.png) [@Raul\_Jr\_Martinez](https://discuss.elastic.co/u/Raul_Jr_Martinez)\
**Post date:** [May 14, 2012, 5:25am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571/4 "2012-05-14T05:25:06Z")

</div>

Hello Shay,

Apologies for the delayed response. I finally figured out the problem.

```
 - my data has a dirty geo point values. I reviewed my pipeline to 

```

ensure that values that gets into "geoloc" field is an array of values  
[long/lat]  
- I was trying to use "distance\_units" option for geo\_distance\_range  
filter. This is causing an inconsistent results. Some selected range  
returns empty results while other range values returns some values. I fixed  
the problem by concatening "mi" (miles) unit when setting the "to" and  
"from" fields.

Everything is working in 0.17.6 but I'm going to be upgrading to the latest  
release as soon as possible.

Thanks!  
Raul

On Wednesday, May 9, 2012 4:52:05 PM UTC+8, kimchy wrote:

> One more thing, the geo distance facet is inclusive of the lower end, and  
> exclusive of the upper end, so you should have ranges like this: 7000-8000,  
> 8000-9000, and then, the geo\_distance\_filter range should be from: 7000,  
> to: 8000, include\_upper: false.
> 
> On Wed, May 9, 2012 at 11:43 AM, Shay Banon wrote:
> 
> > By default, the geo distance range filter includes both from and to  
> > values. You get back 7001-8000, and then do 7000-8000 (inclusive), maybe  
> > thats the problem? If not, can you please gist a sample repro indexing some  
> > sample data and showing it? it will be fastest to check.
> > 
> > On Sun, May 6, 2012 at 4:37 AM, Raul, Jr. Martinez wrote:
> > 
> > > Hello!
> > > 
> > > I have a list of documents that contains geoloc field which is a  
> > > geo\_point type as seen in the mapping below (some fields has been stripped).
> > > 
> > > {  
> > > "schools": {  
> > > "properties": {  
> > > ....  
> > > "geoloc": {  
> > > "store": "yes",  
> > > "type": "geo\_point"  
> > > },  
> > > "country": {  
> > > "index": "not\_analyzed",  
> > > "store": "yes",  
> > > "type": "string"  
> > > },  
> > > "username": {  
> > > "index": "not\_analyzed",  
> > > "type": "string"  
> > > }  
> > > ....  
> > > ....  
> > > }  
> > > }  
> > > }
> > > 
> > > The default facet attached to the query is a geo\_distance facet using  
> > > to/from to define range as seen below (again some range were stripped to  
> > > shorten this post).
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "geo\_distance": {  
> > > "ranges": [  
> > > ….  
> > > ….  
> > > {  
> > > "from": "7001",  
> > > "to": "8000"  
> > > },  
> > > {  
> > > "from": "8001",  
> > > "to": "9000"  
> > > },  
> > > {  
> > > "from": "9001",  
> > > "to": "10000"  
> > > },  
> > > {  
> > > "from": "10001",  
> > > "to": "50000"  
> > > }  
> > > ],  
> > > "geoloc": {  
> > > "lat": 51.5128,  
> > > "lon": -0.0918  
> > > },  
> > > "unit": "miles"  
> > > }  
> > > }  
> > > }
> > > 
> > > The above facet query returns the following response.  
> > > "facets": {  
> > > "geoloc": {  
> > > "\_type": "geo\_distance",  
> > > "ranges": [  
> > > ....  
> > > ....  
> > > {  
> > > "from": 6001,  
> > > "to": 7000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 7001,  
> > > "to": 8000,  
> > > "min": 7971.960904721318,  
> > > "max": 7971.960904721318,  
> > > "total\_count": 4,  
> > > "total": 31887.843618885272,  
> > > "mean": 7971.960904721318  
> > > },  
> > > {  
> > > "from": 8001,  
> > > "to": 9000,  
> > > "min": 8127.135465210893,  
> > > "max": 8876.39022791625,  
> > > "total\_count": 8,  
> > > "total": 67474.56494917188,  
> > > "mean": 8434.320618646485  
> > > },  
> > > {  
> > > "from": 9001,  
> > > "to": 10000,  
> > > "min": 9094.99960175981,  
> > > "max": 9998.014376489728,  
> > > "total\_count": 1533,  
> > > "total": 14749196.606093463,  
> > > "mean": 9621.132815455618  
> > > },  
> > > {  
> > > "from": 10001,  
> > > "to": 50000,  
> > > "min": 10107.137498621609,  
> > > "max": 11991.886489810546,  
> > > "total\_count": 1241,  
> > > "total": 12977180.049243744,  
> > > "mean": 10457.034689156926  
> > > }  
> > > ]  
> > > }  
> > > }
> > > 
> > > _Effective Range with counts_
> > > 
> > > 7,001 - 8,000 miles (4)
> > > 
> > > 8,001 - 9,000 miles (8)
> > > 
> > > 9,001 - 10,000 miles (1,531)
> > > 
> > > More than 10,000 miles (1,239)
> > > 
> > > In my application, I selected 7001 to 8000 miles with (4) expected  
> > > records within this range. The following filters are applied to the new  
> > > Request/Query:
> > > 
> > > _Query Filter:_
> > > 
> > > "filter": {
> > > 
> > > ```
> > > "and": [
> > > {
> > > "geo_distance_range": {
> > > "from": "7001 mi",
> > > "to": "8000 mi",
> > > "geoloc": {
> > > "lat": 51.5128,
> > > "lon": -0.0918
> > > }
> > > }
> > > }
> > > ]
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > _Facet Filter (geoloc):_
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "facet\_filter": {  
> > > "and": [  
> > > {  
> > > "geo\_distance\_range": {  
> > > "from": "7001 mi",  
> > > "to": "8000 mi",  
> > > "geoloc": {  
> > > "lat": 51.5128,  
> > > "lon": -0.0918  
> > > }  
> > > }
> > > 
> > > ```
> > > }
> > > ]
> > > },
> > > "geo_distance": {
> > > "ranges": [
> > > {
> > > "from": "7001",
> > > "to": "8000"
> > > },
> > > {
> > > "from": "8001",
> > > "to": "9000"
> > > },
> > > {
> > > "from": "9001",
> > > "to": "10000"
> > > },
> > > {
> > > "from": "10001",
> > > "to": "50000"
> > > }
> > > ],
> > > "geoloc": {
> > > "lat": 51.5128,
> > > "lon": -0.0918
> > > },
> > > 
> > > "unit": "miles"
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > The Response contains the following facet values:
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "\_type": "geo\_distance",  
> > > "ranges": [  
> > > …  
> > > ...  
> > > {  
> > > "from": 6001,  
> > > "to": 7000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 7001,  
> > > "to": 8000,  
> > > "min": 7971.960904721318,  
> > > "max": 7971.960904721318,  
> > > "total\_count": 4,  
> > > "total": 31887.843618885272,  
> > > "mean": 7971.960904721318  
> > > },  
> > > {  
> > > "from": 8001,  
> > > "to": 9000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 9001,  
> > > "to": 10000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 10001,  
> > > "to": 50000,  
> > > "min": 10214.110601712535,  
> > > "max": 10214.110601712535,  
> > > "total\_count": 4,  
> > > "total": 40856.44240685014,  
> > > "mean": 10214.110601712535  
> > > }  
> > > ]  
> > > }  
> > > }
> > > 
> > > \*Range with values: \*
> > > 
> > > - 7001 - 8000 (4 records)
> > > - 10001 - 50000 (4 records)
> > > 
> > > \*Other observations: \*
> > > 
> > > - Sending 7001 to 8000 range returns four records
> > > - Sending 10001 to 50000 range returns greater than four records.
> > > 
> > > Elasticsearch version Used:
> > > 
> > > - 0.19.3
> > > - 0.17.6
> > > 
> > > Has anyone experience this kind of behavior? It looks like  
> > > geo\_distance\_range is not working correctly for me or I maybe I am missing  
> > > something.
> > > 
> > > Thank you!
> > > 
> > > Raul

On Wednesday, May 9, 2012 4:52:05 PM UTC+8, kimchy wrote:

> One more thing, the geo distance facet is inclusive of the lower end, and  
> exclusive of the upper end, so you should have ranges like this: 7000-8000,  
> 8000-9000, and then, the geo\_distance\_filter range should be from: 7000,  
> to: 8000, include\_upper: false.
> 
> On Wed, May 9, 2012 at 11:43 AM, Shay Banon wrote:
> 
> > By default, the geo distance range filter includes both from and to  
> > values. You get back 7001-8000, and then do 7000-8000 (inclusive), maybe  
> > thats the problem? If not, can you please gist a sample repro indexing some  
> > sample data and showing it? it will be fastest to check.
> > 
> > On Sun, May 6, 2012 at 4:37 AM, Raul, Jr. Martinez wrote:
> > 
> > > Hello!
> > > 
> > > I have a list of documents that contains geoloc field which is a  
> > > geo\_point type as seen in the mapping below (some fields has been stripped).
> > > 
> > > {  
> > > "schools": {  
> > > "properties": {  
> > > ....  
> > > "geoloc": {  
> > > "store": "yes",  
> > > "type": "geo\_point"  
> > > },  
> > > "country": {  
> > > "index": "not\_analyzed",  
> > > "store": "yes",  
> > > "type": "string"  
> > > },  
> > > "username": {  
> > > "index": "not\_analyzed",  
> > > "type": "string"  
> > > }  
> > > ....  
> > > ....  
> > > }  
> > > }  
> > > }
> > > 
> > > The default facet attached to the query is a geo\_distance facet using  
> > > to/from to define range as seen below (again some range were stripped to  
> > > shorten this post).
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "geo\_distance": {  
> > > "ranges": [  
> > > ….  
> > > ….  
> > > {  
> > > "from": "7001",  
> > > "to": "8000"  
> > > },  
> > > {  
> > > "from": "8001",  
> > > "to": "9000"  
> > > },  
> > > {  
> > > "from": "9001",  
> > > "to": "10000"  
> > > },  
> > > {  
> > > "from": "10001",  
> > > "to": "50000"  
> > > }  
> > > ],  
> > > "geoloc": {  
> > > "lat": 51.5128,  
> > > "lon": -0.0918  
> > > },  
> > > "unit": "miles"  
> > > }  
> > > }  
> > > }
> > > 
> > > The above facet query returns the following response.  
> > > "facets": {  
> > > "geoloc": {  
> > > "\_type": "geo\_distance",  
> > > "ranges": [  
> > > ....  
> > > ....  
> > > {  
> > > "from": 6001,  
> > > "to": 7000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 7001,  
> > > "to": 8000,  
> > > "min": 7971.960904721318,  
> > > "max": 7971.960904721318,  
> > > "total\_count": 4,  
> > > "total": 31887.843618885272,  
> > > "mean": 7971.960904721318  
> > > },  
> > > {  
> > > "from": 8001,  
> > > "to": 9000,  
> > > "min": 8127.135465210893,  
> > > "max": 8876.39022791625,  
> > > "total\_count": 8,  
> > > "total": 67474.56494917188,  
> > > "mean": 8434.320618646485  
> > > },  
> > > {  
> > > "from": 9001,  
> > > "to": 10000,  
> > > "min": 9094.99960175981,  
> > > "max": 9998.014376489728,  
> > > "total\_count": 1533,  
> > > "total": 14749196.606093463,  
> > > "mean": 9621.132815455618  
> > > },  
> > > {  
> > > "from": 10001,  
> > > "to": 50000,  
> > > "min": 10107.137498621609,  
> > > "max": 11991.886489810546,  
> > > "total\_count": 1241,  
> > > "total": 12977180.049243744,  
> > > "mean": 10457.034689156926  
> > > }  
> > > ]  
> > > }  
> > > }
> > > 
> > > _Effective Range with counts_
> > > 
> > > 7,001 - 8,000 miles (4)
> > > 
> > > 8,001 - 9,000 miles (8)
> > > 
> > > 9,001 - 10,000 miles (1,531)
> > > 
> > > More than 10,000 miles (1,239)
> > > 
> > > In my application, I selected 7001 to 8000 miles with (4) expected  
> > > records within this range. The following filters are applied to the new  
> > > Request/Query:
> > > 
> > > _Query Filter:_
> > > 
> > > "filter": {
> > > 
> > > ```
> > > "and": [
> > > {
> > > "geo_distance_range": {
> > > "from": "7001 mi",
> > > "to": "8000 mi",
> > > "geoloc": {
> > > "lat": 51.5128,
> > > "lon": -0.0918
> > > }
> > > }
> > > }
> > > ]
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > _Facet Filter (geoloc):_
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "facet\_filter": {  
> > > "and": [  
> > > {  
> > > "geo\_distance\_range": {  
> > > "from": "7001 mi",  
> > > "to": "8000 mi",  
> > > "geoloc": {  
> > > "lat": 51.5128,  
> > > "lon": -0.0918  
> > > }  
> > > }
> > > 
> > > ```
> > > }
> > > ]
> > > },
> > > "geo_distance": {
> > > "ranges": [
> > > {
> > > "from": "7001",
> > > "to": "8000"
> > > },
> > > {
> > > "from": "8001",
> > > "to": "9000"
> > > },
> > > {
> > > "from": "9001",
> > > "to": "10000"
> > > },
> > > {
> > > "from": "10001",
> > > "to": "50000"
> > > }
> > > ],
> > > "geoloc": {
> > > "lat": 51.5128,
> > > "lon": -0.0918
> > > },
> > > 
> > > "unit": "miles"
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > The Response contains the following facet values:
> > > 
> > > "facets": {  
> > > "geoloc": {  
> > > "\_type": "geo\_distance",  
> > > "ranges": [  
> > > …  
> > > ...  
> > > {  
> > > "from": 6001,  
> > > "to": 7000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 7001,  
> > > "to": 8000,  
> > > "min": 7971.960904721318,  
> > > "max": 7971.960904721318,  
> > > "total\_count": 4,  
> > > "total": 31887.843618885272,  
> > > "mean": 7971.960904721318  
> > > },  
> > > {  
> > > "from": 8001,  
> > > "to": 9000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 9001,  
> > > "to": 10000,  
> > > "min": "Infinity",  
> > > "max": "-Infinity",  
> > > "total\_count": 0,  
> > > "total": 0,  
> > > "mean": "NaN"  
> > > },  
> > > {  
> > > "from": 10001,  
> > > "to": 50000,  
> > > "min": 10214.110601712535,  
> > > "max": 10214.110601712535,  
> > > "total\_count": 4,  
> > > "total": 40856.44240685014,  
> > > "mean": 10214.110601712535  
> > > }  
> > > ]  
> > > }  
> > > }
> > > 
> > > \*Range with values: \*
> > > 
> > > - 7001 - 8000 (4 records)
> > > - 10001 - 50000 (4 records)
> > > 
> > > \*Other observations: \*
> > > 
> > > - Sending 7001 to 8000 range returns four records
> > > - Sending 10001 to 50000 range returns greater than four records.
> > > 
> > > Elasticsearch version Used:
> > > 
> > > - 0.19.3
> > > - 0.17.6
> > > 
> > > Has anyone experience this kind of behavior? It looks like  
> > > geo\_distance\_range is not working correctly for me or I maybe I am missing  
> > > something.
> > > 
> > > Thank you!
> > > 
> > > Raul

---

<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, 3:29am UTC](https://discuss.elastic.co/t/geo-distance-range-filter-applied-to-a-facet-returns-values-outside-the-range/7571/5 "2017-07-06T03:29:01Z")

</div>


