# RangeFacet, proposed enhancement

**URL:** https://discuss.elastic.co/t/rangefacet-proposed-enhancement/5560
**Category:** Elasticsearch
**Created:** [October 11, 2011, 12:25pm UTC](https://discuss.elastic.co/t/rangefacet-proposed-enhancement/5560 "2011-10-11T12:25:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Stephane\_Bastian](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stephane\_Bastian](https://discuss.elastic.co/u/Stephane_Bastian)
#### Post date: [October 11, 2011, 12:25pm UTC](https://discuss.elastic.co/t/rangefacet-proposed-enhancement/5560/1 "2011-10-11T12:25:57Z")

</div>

Hi,

We have been using facets quite a bit lately and found minors shortcomings  
with RangeFacet. Let me explain the problems a little bit further

Most of the time, we are using RangeFacet on multiple ranges. A range of  
price, dates or something else. Lets take an example: lets say that we want  
to get back the following ranges:

- 0\>= price \<=49
- 50\>= price \<=99
- 99\>= price \<=150

It is easily done by defining a RangeFacet, with 3 ranges as follow:

{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"field" : "price",  
"ranges" : [  
{ "from" : 0, "to" : 49 },  
{ "from" : 50, "to" : 99 },  
{ "from" : 100, "to" : 149 }  
]  
}  
}  
}  
}

ES returns something similar to this:

"range1" : {

```
  "_type" : "range",
  "ranges" : [ 

      {
          "from" : 0,
          "to" : 49,
          "count" : 20,
          .....
      },
      {
          "from" : 50,
          "to" : 99,
          "count" : 16,
          .....
      },
      {
          "from" : -100,
          "to" : 149,
          "count" : 3,
          .....
      }

   ]

}

```

While this is fine, it would be really handy to give a name to each range  
and get them back by name instead of their value. this would look like this:

{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"field" : "price",  
"ranges" : [  
{ _"name": "0to49",_ "from" : 0, "to" : 49 },  
{ _"name": "50to99"_, "from" : 50, "to" : 99 },  
{ _"name": "100to149"_, "from" : 100, "to" : 149 }  
]  
}  
}  
}  
}

ES would then return something like this:

"range1" : {

```
  "_type" : "range",
  "ranges" : [ 

      {
          *"name" : "0to49",*
          "from" : 0,
          "to" : 49,
          "count" : 20,
          .....
      },
      {
            *"name" : "50to99",*
          "from" : 50,
          "to" : 99,
          "count" : 16,
          .....
      },
      {
          *"name" : "100to149",*
          "from" : 100,
          "to" : 149,
          "count" : 3,
          .....
      }

   ]

}

```

This enhancement doesn't change a lot but make it easier to figure out which  
ranges are returned by ES. This is very handy especially when you are  
working with range of dates as bellow:

{  
"query" : {  
"match\_all" : {}  
},  
"facets" : {  
"range1" : {  
"range" : {  
"field" : "date",  
"ranges" : [  
{ _"name": "today",_ "from" : xx, "to" : xx },  
{ _"name": "tomorrow"_, "from" : xx, "to" : xx },  
{ _"name": "next7days"_, "from" : xx, "to" : xx }  
]  
}  
}  
}  
}

Any feedback?

---

<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: [October 12, 2011, 9:03pm UTC](https://discuss.elastic.co/t/rangefacet-proposed-enhancement/5560/2 "2011-10-12T21:03:06Z")

</div>

Its certainly possible, open an issue. Note, range facet the from is  
inclusive, and to is exclusive.

On Tue, Oct 11, 2011 at 2:25 PM, Stephane Bastian \<  
[stephane.bastian.dev@gmail.com](mailto:stephane.bastian.dev@gmail.com)\> wrote:

> Hi,
> 
> We have been using facets quite a bit lately and found minors shortcomings  
> with RangeFacet. Let me explain the problems a little bit further
> 
> Most of the time, we are using RangeFacet on multiple ranges. A range of  
> price, dates or something else. Lets take an example: lets say that we want  
> to get back the following ranges:
> 
> - 0\>= price \<=49
> - 50\>= price \<=99
> - 99\>= price \<=150
> 
> It is easily done by defining a RangeFacet, with 3 ranges as follow:
> 
> {
> 
> ```
> "query" : {
> 
> "match_all" : {}
> 
> },
> 
> "facets" : {
> 
> "range1" : {
> 
> "range" : {
> 
> "field" : "price",
> 
> "ranges" : [
> 
> { "from" : 0, "to" : 49 },
> 
> { "from" : 50, "to" : 99 },
> 
> { "from" : 100, "to" : 149 }
> ]
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> ES returns something similar to this:
> 
> "range1" : {
> 
> ```
> "_type" : "range",
> "ranges" : [
> 
> {
> "from" : 0,
> "to" : 49,
> "count" : 20,
> .....
> },
> {
> "from" : 50,
> "to" : 99,
> "count" : 16,
> .....
> },
> {
> "from" : -100,
> "to" : 149,
> "count" : 3,
> .....
> }
> 
> ]
> 
> }
> 
> ```
> 
> While this is fine, it would be really handy to give a name to each range  
> and get them back by name instead of their value. this would look like this:
> 
> {
> 
> ```
> "query" : {
> 
> "match_all" : {}
> 
> },
> 
> "facets" : {
> 
> "range1" : {
> 
> "range" : {
> 
> "field" : "price",
> 
> "ranges" : [
> 
> { *"name": "0to49",* "from" : 0, "to" : 49 },
> 
> { *"name": "50to99"*, "from" : 50, "to" : 99 },
> 
> { *"name": "100to149"*, "from" : 100, "to" : 149 }
> ]
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> ES would then return something like this:
> 
> "range1" : {
> 
> ```
> "_type" : "range",
> "ranges" : [
> 
> {
> *"name" : "0to49",*
> "from" : 0,
> "to" : 49,
> "count" : 20,
> .....
> },
> {
> *"name" : "50to99",*
> "from" : 50,
> "to" : 99,
> "count" : 16,
> .....
> },
> {
> *"name" : "100to149",*
> "from" : 100,
> "to" : 149,
> "count" : 3,
> .....
> }
> 
> ]
> 
> }
> 
> ```
> 
> This enhancement doesn't change a lot but make it easier to figure out  
> which ranges are returned by ES. This is very handy especially when you are  
> working with range of dates as bellow:
> 
> {
> 
> ```
> "query" : {
> 
> "match_all" : {}
> 
> },
> 
> "facets" : {
> 
> "range1" : {
> 
> "range" : {
> 
> "field" : "date",
> 
> "ranges" : [
> 
> { *"name": "today",* "from" : xx, "to" : xx },
> 
> { *"name": "tomorrow"*, "from" : xx, "to" : xx },
> 
> { *"name": "next7days"*, "from" : xx, "to" : xx }
> ]
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> Any feedback?

---

<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:52am UTC](https://discuss.elastic.co/t/rangefacet-proposed-enhancement/5560/3 "2017-07-06T03:52:14Z")

</div>


