# Non Numeric Range Facet

**URL:** <https://discuss.elastic.co/t/non-numeric-range-facet/4297>\
**Category:** Elasticsearch\
**Created:** [April 26, 2011, 3:35pm UTC](https://discuss.elastic.co/t/non-numeric-range-facet/4297 "2011-04-26T15:35:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joec](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@joec](https://discuss.elastic.co/u/joec)\
**Post date:** [April 26, 2011, 3:35pm UTC](https://discuss.elastic.co/t/non-numeric-range-facet/4297/1 "2011-04-26T15:35:35Z")

</div>

Hi all,

I'm new to the elastic search boards. As a long time Endeca user, I'm really enjoying working with ElasticSearch.

I would like to know if it is currently possible to generate a range facet against string data. Basically provide a range based on the String.compareTo function

Essentially something like this  
"facets" : {  
"Letters" : {  
"range" : {  
"key\_script" : "\_source.title",  
"value\_script" : "\_source.title",  
"ranges" : [  
{ "from" : "A", "to" : "I" },  
{ "from" : "I", "to" : "N" },  
{ "from" : "N" }  
]  
}  
}  
}

I could obviously bake in the ranges at index time and do a simple term facet if I had to.  
I was able to use a script (using js) to sort of achieve this goal - but I don't think I can get overlapping ranges like I can in the range facet, unless the script could return an array of term values - that would be cool 🙂

"facets" : {  
"Title A-Z" : {  
"terms" : {  
"fields" : ["title.titleSort"],  
"size" : 10,  
"script" : "var x={"A":"A-E","B":"A-E","C":"A-E","D":"A-E","E":"A-E","F":"F-M"}; var y=x[term.substring(0,1).toUpperCase()]; y==null ? "Others" : y"  
}  
}  
}  
I'm super impressed how fast this runs on my laptop on a 35K record index. Awesome work!

Is there a convenient way to plug in additional Facet types?

Thanks,

-Joe

---

<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:** [April 27, 2011, 5:11pm UTC](https://discuss.elastic.co/t/non-numeric-range-facet/4297/2 "2011-04-27T17:11:39Z")

</div>

There isn't a range facet support for string fields, can be added ... . One option is to use a set of filter facets ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/filter-facet.html)), each filter includes the relevant range using a range filter. It should be faster compared to what you are doing now (especially for subsequent executions).  
On Tuesday, April 26, 2011 at 6:35 PM, joec wrote:

> Hi all,
> 
> I'm new to the Elasticsearch boards. As a long time Endeca user, I'm really  
> enjoying working with Elasticsearch.
> 
> I would like to know if it is currently possible to generate a range facet  
> against string data. Basically provide a range based on the String.compareTo  
> function
> 
> Essentially something like this  
> "facets" : {  
> "Letters" : {  
> "range" : {  
> "key\_script" : "\_source.title",  
> "value\_script" : "\_source.title",  
> "ranges" : [  
> { "from" : "A", "to" : "I" },  
> { "from" : "I", "to" : "N" },  
> { "from" : "N" }  
> ]  
> }  
> }  
> }
> 
> I could obviously bake in the ranges at index time and do a simple term  
> facet if I had to.  
> I was able to use a script (using js) to sort of achieve this goal - but I  
> don't think I can get overlapping ranges like I can in the range facet,  
> unless the script could return an array of term values - that would be cool  
> 🙂
> 
> "facets" : {  
> "Title A-Z" : {  
> "terms" : {  
> "fields" : ["title.titleSort"],  
> "size" : 10,  
> "script" : "var  
> x={"A":"A-E","B":"A-E","C":"A-E","D":"A-E","E":"A-E","F":"F-M"};  
> var y=x[term.substring(0,1).toUpperCase()]; y==null ? "Others" : y"  
> }  
> }  
> }  
> I'm super impressed how fast this runs on my laptop on a 35K record index.  
> Awesome work!
> 
> Is there a convenient way to plug in additional Facet types?
> 
> Thanks,
> 
> -Joe
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Non-Numeric-Range-Facet-tp2866042p2866042.html](http://elasticsearch-users.115913.n3.nabble.com/Non-Numeric-Range-Facet-tp2866042p2866042.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:07am UTC](https://discuss.elastic.co/t/non-numeric-range-facet/4297/3 "2017-07-06T04:07:30Z")

</div>


