# Problem with float in range facet

**URL:** <https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240>\
**Category:** Elasticsearch\
**Created:** [January 15, 2014, 7:10am UTC](https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240 "2014-01-15T07:10:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jettro\_Coenradie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro_coenradie/32/59704_2.png) [@Jettro\_Coenradie](https://discuss.elastic.co/u/Jettro_Coenradie)\
**Post date:** [January 15, 2014, 7:10am UTC](https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240/1 "2014-01-15T07:10:14Z")

</div>

Hi,  
I have a float field that I want to use in a range facet. I give one range  
to the facet like the following query.

{  
"query": {  
"matchAll": {}  
},  
"facets": {  
"imageSize": {  
"range": {  
"field": "imageSize",  
"ranges": [  
{  
"from": 1.9  
}  
]  
}  
}  
}  
}

If I have a document with the value 1.9 it is not included in the results.  
If I change the from part to 1.89 however it is included. I am sure the  
type of the field is 1.9, checked it in the mapping.

Any ideas what is the problem?

thanks,  
Jettro

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [January 15, 2014, 2:32pm UTC](https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240/2 "2014-01-15T14:32:39Z")

</div>

Hi,

This is a floating point rounding error. There are many numbers that  
floating points cannot represent accurately and if you assign 1.9 to a  
float, the value that will be stored is actually closer to 1.89999998.

Since the range query internally works on doubles, which have better  
precision than floats, the 1.9 from the facet definition is not precisely  
1.9 but very close thanks to double precision while your float in the index  
is still close to 1.89999998 and thus compares less than the double  
representation of 1.9.

There are two ways to work around this issue:

- either store the imageSize field as a double so that the rounding error  
will be the same at indexing and faceting time,
- or if your numbers have a finite number of digits, store them as  
integers instead. For example, if your image sizes only have two digits,  
you could store them all as (imageSize\*10^2) which is an integer, and  
change your facet definition to be `from: 190` instead of `from: 1.9`. This  
requires a bit more work on client side but a great benefit is that there  
won't be any more rounding issue.

On Wed, Jan 15, 2014 at 8:10 AM, Jettro Coenradie \<  
[jettro.coenradie@gmail.com](mailto:jettro.coenradie@gmail.com)\> wrote:

> Hi,  
> I have a float field that I want to use in a range facet. I give one range  
> to the facet like the following query.
> 
> {  
> "query": {  
> "matchAll": {}  
> },  
> "facets": {  
> "imageSize": {  
> "range": {  
> "field": "imageSize",  
> "ranges": [  
> {  
> "from": 1.9  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> If I have a document with the value 1.9 it is not included in the results.  
> If I change the from part to 1.89 however it is included. I am sure the  
> type of the field is 1.9, checked it in the mapping.
> 
> Any ideas what is the problem?
> 
> thanks,  
> Jettro
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Adrien Grand

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4A28BPaEZXnbQNyGgrT2UxfhnTsVL43cR1MpXK3sHsqA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4A28BPaEZXnbQNyGgrT2UxfhnTsVL43cR1MpXK3sHsqA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jettro\_Coenradie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro_coenradie/32/59704_2.png) [@Jettro\_Coenradie](https://discuss.elastic.co/u/Jettro_Coenradie)\
**Post date:** [January 16, 2014, 7:32am UTC](https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240/3 "2014-01-16T07:32:43Z")

</div>

Thanks, that works.

Op woensdag 15 januari 2014 15:32:39 UTC+1 schreef Adrien Grand:

> Hi,
> 
> This is a floating point rounding error. There are many numbers that  
> floating points cannot represent accurately and if you assign 1.9 to a  
> float, the value that will be stored is actually closer to 1.89999998.
> 
> Since the range query internally works on doubles, which have better  
> precision than floats, the 1.9 from the facet definition is not precisely  
> 1.9 but very close thanks to double precision while your float in the index  
> is still close to 1.89999998 and thus compares less than the double  
> representation of 1.9.
> 
> There are two ways to work around this issue:
> 
> - either store the imageSize field as a double so that the rounding error  
> will be the same at indexing and faceting time,
> - or if your numbers have a finite number of digits, store them as  
> integers instead. For example, if your image sizes only have two digits,  
> you could store them all as (imageSize\*10^2) which is an integer, and  
> change your facet definition to be `from: 190` instead of `from: 1.9`. This  
> requires a bit more work on client side but a great benefit is that there  
> won't be any more rounding issue.
> 
> On Wed, Jan 15, 2014 at 8:10 AM, Jettro Coenradie \<[jettro.c...@gmail.com](mailto:jettro.c...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi,  
> > I have a float field that I want to use in a range facet. I give one  
> > range to the facet like the following query.
> > 
> > {  
> > "query": {  
> > "matchAll": {}  
> > },  
> > "facets": {  
> > "imageSize": {  
> > "range": {  
> > "field": "imageSize",  
> > "ranges": [  
> > {  
> > "from": 1.9  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }
> > 
> > If I have a document with the value 1.9 it is not included in the  
> > results. If I change the from part to 1.89 however it is included. I am  
> > sure the type of the field is 1.9, checked it in the mapping.
> > 
> > Any ideas what is the problem?
> > 
> > thanks,  
> > Jettro
> > 
> > --  
> > 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:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/85e04977-4378-48cb-a6c1-e0c3b71fbad4%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Adrien Grand

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/28c879e7-da53-4068-9a09-28b5f506a6f6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/28c879e7-da53-4068-9a09-28b5f506a6f6%40googlegroups.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, 1:56am UTC](https://discuss.elastic.co/t/problem-with-float-in-range-facet/15240/4 "2017-07-06T01:56:29Z")

</div>


