[Elasticsearch] Aggregation-Create bar chart (bar chart)

Source: Internet
Author: User

Create a bar chart (Building bar Charts)

An exciting property of aggregation is that it can be easily converted into graphs and graphs. In this chapter, we will use the previous sample datasets to create a variety of analysis cases. We will also show the types of aggregations that can be supported.

The histogram bucket (histogram bucket) is very useful. Bars are essentially bar charts, and if you create a report or Analysis panel (analytics Dashboard), there's no doubt that there are some bar charts. A bar chart works by specifying an interval (Interval). If we use a histogram to represent the sales price, you might specify a value of 20000 for the interval. So every 20000 American knives will create a bucket. The document is then assigned to the bucket.

For our dashboards, we want to know how many vehicles are in each price range. We also want to know how much revenue has been generated in this price bucket. This is calculated by summing up the selling price of all the cars in the interval.

To achieve this, we used a histogram type of aggregation and nested a sum indicator in it:

get/cars/transactions/_search?search_type=count{ "aggs" :{ "price" :{ "histogram" :{ "field" : "price" , "interval" :20000}, "aggs" :{ "revenue" : { "sum" : { "field" : "price" }             }         }      }   }}

As you can see, our query is built around price aggregation, which contains a histogram bucket. The bucket requires a numeric field and a value for the interval to be calculated. The interval is used to define "how wide" each bucket has. An interval of 20000 means we can have intervals [0-19999, 20000-39999, etc.].

Next, we define a nested indicator in the histogram. It is an indicator of the sum type, which accumulates the price field of the document in that interval. This gets the revenue from each price range, so we can see whether it's a regular car or a luxury car that earns more.

The following is the resulting response:

{...    "aggregations" : { "price" : { "buckets" : [            { "key" :0, "doc_count" :3, "revenue" : { "value" :37000}            },            { "key" :20000, "doc_count" :4, "revenue" : { "value" :95000}            },            { "key" :80000, "doc_count" :1, "revenue" : { "value" :80000}            }         ]      }   }}

the response is fairly self-explanatory and it should be noted that the histogram keys correspond to the lower boundary of the interval. The key 0 means 0-19,999, the key 20000 means 20,000-39,999, and so forth. The response is capable of explaining its meaning, but it is worth noting that the histogram key corresponds to the lower bounds of the interval. The key value 0 represents 0-19999, the key value 20000 represents 20000-39999, and so on.

NOTE Missing Empty bucket

You may notice that the 40000-60000 American knife This interval does not appear in the response. The histogram bucket is omitted by default because containing empty buckets can cause the output to be too large, which may not be the result we want.

In the next section we will discuss how to include empty buckets, return empty buckets

From the graph, you can represent the preceding data as follows:

Of course, you can use any aggregation of generated categories and statistics to create a bar chart, and not just use histogram buckets. Let's create a bar chart of a popular car manufacturer that contains their average price and standard error. You need to use the terms bucket and a extended_stats indicator:

get/cars/transactions/_search?search_type=count{ "aggs" : { "makes" : { "terms" : { "field" : "make" , "size" :Ten}, "aggs" : { "stats" : { "extended_stats" : { "field" : "price" }        }      }    }  }}

It returns a list of manufacturers (sorted by popularity) and some column statistics for each manufacturer. Among them, we are interested in Stats.avg,stats.count and stats.std_deviation. With this information, we are able to calculate the standard error:

Std_err = Std_deviation/count

The resulting graphs are as follows:


[Elasticsearch] Aggregation-Create bar chart (bar chart)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.