# Range Facets on script\_fields?

**URL:** <https://discuss.elastic.co/t/range-facets-on-script-fields/6950>\
**Category:** Elasticsearch\
**Created:** [March 8, 2012, 6:12pm UTC](https://discuss.elastic.co/t/range-facets-on-script-fields/6950 "2012-03-08T18:12:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarnoult](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Jarnoult](https://discuss.elastic.co/u/Jarnoult)\
**Post date:** [March 8, 2012, 6:12pm UTC](https://discuss.elastic.co/t/range-facets-on-script-fields/6950/1 "2012-03-08T18:12:08Z")

</div>

Hi,

I would like to know if it's possible to create a Range Facet on  
script\_fields ?

Here is my request :

{  
"fields" : ["id", "name"],  
"query" : {  
"text" : { "type": "mytype" }  
},  
"script\_fields" : {  
"average" : {  
"script" : "(\_source.rate1 + \_source.rate2) / 2"  
}  
},  
"facets" : {  
"average" : {  
"range" : {  
"field" : "average",  
"ranges" : [  
{ "to" : 4 },  
{ "from" : 4, "to" : 6 },  
{ "from" : 6, "to" : 8 },  
{ "from" : 10 }  
]  
}  
}  
}  
}

This request fails ... I've tried with "script\_fields" : "average",  
"key\_field" : "average", but no result ...

It's possible on Term Facet, but I didn't find how to make it on Range  
Facet ...  
Is it possible ?

Thanks in advance.  
Jérémy

---

<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:** [March 9, 2012, 6:53pm UTC](https://discuss.elastic.co/t/range-facets-on-script-fields/6950/2 "2012-03-09T18:53:59Z")

</div>

script fields are only applicable to the top N docs that you get back (computed and returned), not for the total hits you get. Facets work on all the hits (and because of that, using \_source is not recommended, because it means you load it and parse it for each hit out of the total hits). Check the range facet docs: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html), you can have an option to provide a key script and a value script.

On Thursday, March 8, 2012 at 8:12 PM, Jarnoult wrote:

> Hi,
> 
> I would like to know if it's possible to create a Range Facet on  
> script\_fields ?
> 
> Here is my request :
> 
> {  
> "fields" : ["id", "name"],  
> "query" : {  
> "text" : { "type": "mytype" }  
> },  
> "script\_fields" : {  
> "average" : {  
> "script" : "(\_source.rate1 + \_source.rate2) / 2"  
> }  
> },  
> "facets" : {  
> "average" : {  
> "range" : {  
> "field" : "average",  
> "ranges" : [  
> { "to" : 4 },  
> { "from" : 4, "to" : 6 },  
> { "from" : 6, "to" : 8 },  
> { "from" : 10 }  
> ]  
> }  
> }  
> }  
> }
> 
> This request fails ... I've tried with "script\_fields" : "average",  
> "key\_field" : "average", but no result ...
> 
> It's possible on Term Facet, but I didn't find how to make it on Range  
> Facet ...  
> Is it possible ?
> 
> Thanks in advance.  
> Jérémy

---

<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:36am UTC](https://discuss.elastic.co/t/range-facets-on-script-fields/6950/3 "2017-07-06T03:36:33Z")

</div>


