# Range Facet doesn't work

**URL:** <https://discuss.elastic.co/t/range-facet-doesnt-work/8541>\
**Category:** Elasticsearch\
**Created:** [July 27, 2012, 2:04pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541 "2012-07-27T14:04:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guram\_Kajaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guram_kajaia/32/2749_2.png) [@Guram\_Kajaia](https://discuss.elastic.co/u/Guram_Kajaia)\
**Post date:** [July 27, 2012, 2:04pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/1 "2012-07-27T14:04:43Z")

</div>

Hello guys.  
I'm playing with range facet but it's not working for me.  
I'm running query as mentioned in guide :

> **[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.

Here is query

{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"my\_field" : [  
{ "to" : 50 },  
{ "from" : 20, "to" : 70 },  
{ "from" : 70, "to" : 120 },  
{ "from" : 150 }  
]  
}  
}  
}  
}

And here is returned error:

> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase  
> [query], total failure; shardFailures {[QKlvaSVSTU-fJtjD0R4\_9Q][deals][4]:  
> QueryPhaseExecutionException[[deals][4]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[10]: Query Failed  
> [Failed to execute main query]]; nested:  
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];  
> }{[QKlvaSVSTU-fJtjD0R4\_9Q][deals][1]:  
> QueryPhaseExecutionException[[deals][1]:  
> query[ConstantScore(NotDeleted(_:_))],from[0],size[10]: Query Failed  
> [Failed to execute main query]]; nested:  
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> cannot be cast to org.elasticsearch.index.field.data.NumericFieldData]; }]",  
> "status": 500  
> }

can anyone help me ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 27, 2012, 2:24pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/2 "2012-07-27T14:24:44Z")

</div>

> ```
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData cannot be cast to org.elasticsearch.index.field.data.NumericFieldData]; 
> 
> ```

Your 'myfield' is mapped as a string, not as a number.

clint

---

<div class="post-metadata">

**Author:** ![Guram\_Kajaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guram_kajaia/32/2749_2.png) [@Guram\_Kajaia](https://discuss.elastic.co/u/Guram_Kajaia)\
**Post date:** [July 27, 2012, 3:00pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/3 "2012-07-27T15:00:23Z")

</div>

Thanks.  
But range facet works only with Integer type ? not floats ?

On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> > 
> 
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> 
> Your 'myfield' is mapped as a string, not as a number.
> 
> clint

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 27, 2012, 3:05pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/4 "2012-07-27T15:05:13Z")

</div>

On Fri, 2012-07-27 at 19:00 +0400, Guram Kajaia wrote:

> Thanks.  
> But range facet works only with Integer type ? not floats ?

It works with floats, but your field is a string

> On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley  
> [clint@traveljury.com](mailto:clint@traveljury.com) wrote:  
> \>  
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> 
> ```
> Your 'myfield' is mapped as a string, not as a number.
>     
> clint
> 
> ```

---

<div class="post-metadata">

**Author:** ![Guram\_Kajaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guram_kajaia/32/2749_2.png) [@Guram\_Kajaia](https://discuss.elastic.co/u/Guram_Kajaia)\
**Post date:** [July 27, 2012, 4:28pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/5 "2012-07-27T16:28:28Z")

</div>

On Jul 27, 2012 7:05 PM, "Clinton Gormley" [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> On Fri, 2012-07-27 at 19:00 +0400, Guram Kajaia wrote:
> 
> > Thanks.  
> > But range facet works only with Integer type ? not floats ?
> 
> It works with floats, but your field is a string
> 
> > On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley  
> > [clint@traveljury.com](mailto:clint@traveljury.com) wrote:  
> > \>
> 
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> 
> > ```
> > Your 'myfield' is mapped as a string, not as a number.
> > 
> > clint
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Guram\_Kajaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guram_kajaia/32/2749_2.png) [@Guram\_Kajaia](https://discuss.elastic.co/u/Guram_Kajaia)\
**Post date:** [July 27, 2012, 4:29pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/6 "2012-07-27T16:29:15Z")

</div>

Thanks i'll try  
On Jul 27, 2012 7:05 PM, "Clinton Gormley" [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> On Fri, 2012-07-27 at 19:00 +0400, Guram Kajaia wrote:
> 
> > Thanks.  
> > But range facet works only with Integer type ? not floats ?
> 
> It works with floats, but your field is a string
> 
> > On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley  
> > [clint@traveljury.com](mailto:clint@traveljury.com) wrote:  
> > \>
> 
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> 
> > ```
> > Your 'myfield' is mapped as a string, not as a number.
> > 
> > clint
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Guram\_Kajaia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guram_kajaia/32/2749_2.png) [@Guram\_Kajaia](https://discuss.elastic.co/u/Guram_Kajaia)\
**Post date:** [July 27, 2012, 10:11pm UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/7 "2012-07-27T22:11:13Z")

</div>

So it works with floats too ( when I created index with curl without  
specifying number\_of\_shards and number\_of\_replicas I got error when i was  
using range facet )  
So range facet now works well but returned documents doesn't matches facet  
parameters.  
Here is query

{

> "query": {  
> "match\_all": {}  
> },  
> "facets": {  
> "range1": {  
> "range": {  
> "field": "deal\_price",  
> "ranges": [  
> {  
> "from": 4,  
> "to": 7  
> }  
> ]  
> }  
> }  
> }  
> }

count that facet returns is 3 but i'm getting more then 3 results in "hits"  
are query and facets depended to each other? or they work separately ?

On Fri, Jul 27, 2012 at 9:29 AM, Guram Kajaia [guram.kajaia@gmail.com](mailto:guram.kajaia@gmail.com)wrote:

> Thanks i'll try  
> On Jul 27, 2012 7:05 PM, "Clinton Gormley" [clint@traveljury.com](mailto:clint@traveljury.com) wrote:
> 
> > On Fri, 2012-07-27 at 19:00 +0400, Guram Kajaia wrote:
> > 
> > > Thanks.  
> > > But range facet works only with Integer type ? not floats ?
> > 
> > It works with floats, but your field is a string
> > 
> > > On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley  
> > > [clint@traveljury.com](mailto:clint@traveljury.com) wrote:  
> > > \>
> > 
> > ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData  
> > cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> > 
> > > ```
> > > Your 'myfield' is mapped as a string, not as a number.
> > > 
> > > clint
> > > 
> > > ```

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 28, 2012, 9:20am UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/8 "2012-07-28T09:20:21Z")

</div>

Hi Guram

On Fri, 2012-07-27 at 15:11 -0700, Guram Kajaia wrote:

> So it works with floats too ( when I created index with curl without  
> specifying number\_of\_shards and number\_of\_replicas I got error when i  
> was using range facet )

That was coincidental 🙂 The problem you were having (I think) is that  
you were creating the field mapping automatically by indexing a  
document, but instead of indexing (eg) { myfield: 123 } you're indexing  
{ myfield: "123" }, which means "this is a string".

> So range facet now works well but returned documents doesn't matches  
> facet parameters.  
> Here is query
> 
> ```
> {
> "query": {
> "match_all": {}
> },
> "facets": {
> "range1": {
> "range": {
> "field": "deal_price",
> "ranges": [
> {
> "from": 4,
> "to": 7
> }
> ]
> }
> }
> }
> }
> 
> ```
> 
> count that facet returns is 3 but i'm getting more then 3 results in  
> "hits"  
> are query and facets depended to each other? or they work separately ?

Facets are calculated based on the results returns from the query. So  
the facet doesn't limit the query results at all.

clint

> On Fri, Jul 27, 2012 at 9:29 AM, Guram Kajaia [guram.kajaia@gmail.com](mailto:guram.kajaia@gmail.com)  
> wrote:  
> Thanks i'll try
> 
> ```
> On Jul 27, 2012 7:05 PM, "Clinton Gormley"
> <clint@traveljury.com> wrote:
> On Fri, 2012-07-27 at 19:00 +0400, Guram Kajaia wrote:
> > Thanks.
> > But range facet works only with Integer type ? not
> floats ?
>             
> It works with floats, but your field is a string
>             
> >
> > On Fri, Jul 27, 2012 at 6:24 PM, Clinton Gormley
> > <clint@traveljury.com> wrote:
> > >
> >
> ClassCastException[org.elasticsearch.index.field.data.strings.SingleValueStringFieldData cannot be cast to org.elasticsearch.index.field.data.NumericFieldData];
> >
> >
> > Your 'myfield' is mapped as a string, not as
> a number.
> >
> > clint
> >
> >
> 
> ```

---

<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:18am UTC](https://discuss.elastic.co/t/range-facet-doesnt-work/8541/9 "2017-07-06T03:18:40Z")

</div>


