Elasticsearch_ Aggregate query of data

The aggregation framework helps to provide aggregated data based on search queries. Aggregation query is an important fe...
max min sum avg

The aggregation framework helps to provide aggregated data based on search queries. Aggregation query is an important feature in database. As a search engine and database, ES also provides powerful aggregation analysis ability. It is a method to divide and calculate data based on query conditions. It is similar to the operation of group by plus some function methods in SQL. Aggregations can be nested to form complex operations (bagging aggregations can include sub aggregations).

The value of aggregation calculation can take the value of the field or the result of script calculation. Query the syntax definition of the aggregations node in the request body:

"aggregations" : { //It can also be abbreviated as aggs "<aggregation_name>" : { //Aggregate name "<aggregation_type>" : { //Type of aggregation <aggregation_body> //Aggregate: which fields are aggregated } [,"meta" : { [<meta_data_body>] } ]? //element [,"aggregations" : { [<sub_aggregation>]+ } ]? //In aggregation, sub aggregation is defined } [,"<aggregation_name_2>" : { ... } ]* //Aggregate name }

1. Data preparation

(1) Create employee index employee

PUT employee { "mappings": { "properties": { "id": { "type": "integer" }, "name": { "type": "keyword" }, "job": { "type": "keyword" }, "age": { "type": "integer" }, "gender": { "type": "keyword" } } }, "settings":{ "index":{ "number_of_shards":3, #Number of slices "number_of_replicas":2 #Number of copies } } }

(2) Insert data

POST employee/_bulk {"index": {"_id": 1}} {"id": 1, "name": "Bob", "job": "java", "age": 21, "sal": 8000, "gender": "male"} {"index": {"_id": 2}} {"id": 2, "name": "Rod", "job": "html", "age": 31, "sal": 18000, "gender": "female"} {"index": {"_id": 3}} {"id": 3, "name": "Gaving", "job": "java", "age": 24, "sal": 12000, "gender": "male"} {"index": {"_id": 4}} {"id": 4, "name": "King", "job": "dba", "age": 26, "sal": 15000, "gender": "female"} {"index": {"_id": 5}} {"id": 5, "name": "Jonhson", "job": "dba", "age": 29, "sal": 16000, "gender": "male"} {"index": {"_id": 6}} {"id": 6, "name": "Douge", "job": "java", "age": 41, "sal": 20000, "gender": "female"} {"index": {"_id": 7}} {"id": 7, "name": "cutting", "job": "dba", "age": 27, "sal": 7000, "gender": "male"} {"index": {"_id": 8}} {"id": 8, "name": "Bona", "job": "html", "age": 22, "sal": 14000, "gender": "female"} {"index": {"_id": 9}} {"id": 9, "name": "Shyon", "job": "dba", "age": 20, "sal": 19000, "gender": "female"} {"index": {"_id": 10}} {"id": 10, "name": "James", "job": "html", "age": 18, "sal": 22000, "gender": "male"} {"index": {"_id": 11}} {"id": 11, "name": "Golsling", "job": "java", "age": 32, "sal": 23000, "gender": "female"} {"index": {"_id": 12}} {"id": 12, "name": "Lily", "job": "java", "age": 24, "sal": 2000, "gender": "male"} {"index": {"_id": 13}} {"id": 13, "name": "Jack", "job": "html", "age": 23, "sal": 3000, "gender": "female"} {"index": {"_id": 14}} {"id": 14, "name": "Rose", "job": "java", "age": 36, "sal": 6000, "gender": "female"} {"index": {"_id": 15}} {"id": 15, "name": "Will", "job": "dba", "age": 38, "sal": 4500, "gender": "male"} {"index": {"_id": 16}} {"id": 16, "name": "smith", "job": "java", "age": 32, "sal": 23000, "gender": "male"} #There is a newline here

Data Description: the inserted data is employee information. Name is the employee's name, job is the employee's type of work, age is the employee's age, sal is the employee's salary, and gender is the employee's gender.

Index aggregation

Indicator aggregation is to calculate the weight of documents (for example, calculate the maximum, minimum, sum and average values of a certain field of all documents). The output result is often the weight of documents, which is equivalent to adding some statistical information to documents.

It calculates the numerical weight of documents in the aggregation based on specific field s or generated using scripts. Numerical weight aggregation (note that the classification is only for numerical weight aggregation, and there is no such classification for non numerical values). Those that output a single weight are also called single value numerical metrics. Others that generate multiple weights (such as stats) are called multi value numerical metrics.

max min sum avg

Max Aggregation, find the maximum value. Calculate the average value in the aggregated document based on a value of the document (which can be a specific numerical field or calculated through a script).

Min Aggregation, find the minimum value. ditto

Sum Aggregation, sum. ditto

Avg Aggregation, average. ditto

Barrel polymerization

Matrix aggregation

Pipeline polymerization

6 November 2021, 21:10 | Views: 2732

Add new comment

For adding a comment, please log in
or create account

0 comments