summaryrefslogtreecommitdiff
path: root/docs/reference/search/facets/range-facet.asciidoc
blob: fa263ee55957dbaa173578cb6beb549fd7ee8f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
[[search-facets-range-facet]]
=== Range Facets

`range` facet allows to specify a set of ranges and get both the number
of docs (count) that fall within each range, and aggregated data either
based on the field, or using another field. Here is a simple example:

[source,js]
--------------------------------------------------
{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "range1" : {
            "range" : {
                "field" : "field_name",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 20, "to" : 70 },
                    { "from" : 70, "to" : 120 },
                    { "from" : 150 }
                ]
            }
        }
    }
}
--------------------------------------------------

Another option which is a bit more DSL enabled is to provide the ranges
on the actual field name, for example:

[source,js]
--------------------------------------------------
{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "range1" : {
            "range" : {
                "my_field" : [
                    { "to" : 50 },
                    { "from" : 20, "to" : 70 },
                    { "from" : 70, "to" : 120 },
                    { "from" : 150 }
                ]
            }
        }
    }
}
--------------------------------------------------

The `range` facet always includes the `from` parameter and excludes the
`to` parameter for each range.

==== Key and Value

The `range` facet allows to use a different field to check if its value
falls within a range, and another field to compute aggregated data per
range (like total). For example:

[source,js]
--------------------------------------------------
{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "range1" : {
            "range" : {
                "key_field" : "field_name",
                "value_field" : "another_field_name",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 20, "to" : 70 },
                    { "from" : 70, "to" : 120 },
                    { "from" : 150 }
                ]
            }
        }
    }
}
--------------------------------------------------

==== Script Key and Value

Sometimes, some munging of both the key and the value are needed. In the
key case, before it is checked if it falls within a range, and for the
value, when the statistical data is computed per range scripts can be
used. Here is an example:

[source,js]
--------------------------------------------------
{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "range1" : {
            "range" : {
                "key_script" : "doc['date'].date.minuteOfHour",
                "value_script" : "doc['num1'].value",
                "ranges" : [
                    { "to" : 50 },
                    { "from" : 20, "to" : 70 },
                    { "from" : 70, "to" : 120 },
                    { "from" : 150 }
                ]
            }
        }
    }
}
--------------------------------------------------

==== Date Ranges

The range facet support also providing the range as string formatted
dates.