@@ -101,4 +101,51 @@ The response is fairly self-explanatory, but it should be noted that the
101
101
histogram keys correspond to the lower boundary of the interval. The key `0`
102
102
means `0-20,000`, the key `20000` means `20,000-40,000`, etc.
103
103
104
+ Graphically, you could represent the above data in a histogram like this:
105
+
106
+ [[barcharts-histo1]]
107
+ image::images/300_30_histo1.png["Histogram of top makes per price range"]
108
+
109
+ Of course, you can build bar charts with any aggregation which emits categories
110
+ and statistics, not just the `histogram` bucket. Let's build a bar chart of
111
+ popular makes, their average price, and then calculate the standard error
112
+ to add error bars on our chart. This will make use of the `terms` bucket
113
+ and an `extended_stats` metric:
114
+
115
+ [source,js]
116
+ ----
117
+ GET /cars/transactions/_search?search_type=count
118
+ {
119
+ "aggs": {
120
+ "makes": {
121
+ "terms": {
122
+ "field": "make",
123
+ "size": 10
124
+ },
125
+ "aggs": {
126
+ "stats": {
127
+ "extended_stats": {
128
+ "field": "price"
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+ ----
136
+
137
+ This will return a list of makes (sorted by popularity) and a variety of statistics
138
+ about each. In particular, we are interested in `stats.avg`, `stats.count`,
139
+ and `stats.std_deviation`. Using this information, we can calculate the standard error:
140
+
141
+ ................................
142
+ std_err = std_deviation / count
143
+ ................................
144
+
145
+ Which will allow us to build a chart like this:
146
+
147
+ [[barcharts-bar1]]
148
+ image::images/300_30_bar1.png["Barchart of average price per make, with error bars"]
149
+
150
+
104
151
0 commit comments