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?