...

Search Engine - Elastic Search

Back to Course

Lesson Description


Lession - #134 ES Aggregation APIs


Elasticsearch Aggregation APIs

Elasticsearch gives collection API , which is utilized for the collection of information. Collection system gives amassed information in light of the hunt question. In basic words, conglomeration structure gathers every one of the information that is chosen by the pursuit question and gives to the client. It contains a few structure hinders that assistance to construct a complicated outline of information. Conglomerations create the insightful data accessible in Elasticsearch . The following are a few significant places of total should be noted: Total can be made together to assemble complex synopses of information. It tends to be considered as a unit-of-work, which makes investigation data over a bunch of archives that are accessible in elasticsearch. It is essentially founded on the structure blocks. Accumulation work are same as SQL AVERAGE and GROUP BY COUNT capacities. While involving accumulation in elasticsearch, we can perform GROUP BY conglomeration on any numeric field, yet we should be type catchphrase (watchword is resembling a record>
or have fielddata = valid for text fields.



Elasticsearch Aggregation APIs


"aggregation" : {  
    "" : {  
         "" : {  
                    
              "field " : "document_field_name"   
}  
  
[ , "meta" : { [] } ]?  
[ , "aggregation" : { [] + } ]?  
     }  
     [ , ""  : { . . . } ]*  
}  


We can involve more than one conglomeration in a single shot. aggregation- It is an article in JSON that holds the conglomerations to figure. You can likewise utilize the aggs catchphrase instead of collection. aggregation_name - Each accumulation has an intelligent name that is characterized by the client. For instance, use avg_price for figuring normal cost. aggregation_type - It is a kind of conglomeration as every collection has a particular name. aggregation_body - Each accumulation type comprises of its own conglomeration body, which relies upon the idea of collection. field - It is a field watchword. document_field_name - It is the name of the section name being designated in a report

Types of Aggregation

In Elasticsearch, a few kinds of accumulations are accessible, where every conglomeration has its own motivation and result. They are summed up in 4 significant families for improvement, which are as per the following - 1.Metric aggregation 2.Bucketing aggregation 3.Matrix aggregation 4.Pipeline aggregation

Metric Aggregation

Metric collection is a kind of total, which is liable for continue to follow the measurements. Metric accumulation registers the grids from the field's upsides of the collected report. It additionally assists with processing the measurements over a bunch of archives. A few collections produce numeric measurements, which are either - Single-esteemed numeric metric accumulation, i.e., normal total or Multi-esteemed numeric metric accumulation, i.e., details Bucketing Bucketing is a group of collections, which is answerable for building containers. It doesn't ascertain measurements over the fields like metric total. In this accumulation, each container is related with a key and a record. Container collection is utilized to bunch or make information cans. These information pails can be made in view of the current fields, runs, and tweaked channels, and so forth.

Grid Aggregation

Metrix conglomeration is a total that works on numerous fields. It chips away at more than one fields and delivers a lattice result out of the qualities, which is extricated from the solicitation report fields. Framework doesn't uphold prearranging.

Pipeline

As the actual name recommend, it takes input from the result of different conglomerations. In different terms we can say that, - Pipeline accumulations are answerable for conglomerating the result of different collections.

Four important aggregations

Some essential aggregations of elasticsearch are described below with example. 1.Average aggregation 2.Terms aggregation 3.Cardinality aggregation 4.Stats aggregation

Avg Aggregation

Normal total is utilized to work out the normal of any numeric field in a file. Indicate the collection name avg in inquiry while making question. Check out at the accompanying guide to track down the normal of field "expenses":

Copy Code

POST student1/ _search/  
{  
  "aggs": {    
       "avg_fees": {  
               "avg" : {   
                    "field": "fees"  
                }  
          }  
    }  
}  

Response You will get the output like the below response.

{   
"took": 1251,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 3,  
 "relation": "eq"  
          },  
"max_score": 1,  
"hits": [  
   {  
         "index": "student1",  
         "type": "_doc",  
         "id": "01",  
         "score": 1,  
         "_source": {  
 "name ": "Denial Parygen",  
 "dob": "07/Aug/1998",  
 "course": "Mass Communication",  
 "Addmission year": "2018",  
 "email": "denial@gmail.com",  
 "street": "3511 Rodney Street",   
 "state": "Missouri",   
 "country": "United States",   
 "zip": "62208",  
 "fees": "24800"  
   }  
            },  
 {  
         "index": "student1",  
         "type": "_doc",  
         "id": "03",  
         "score": 1,  
         "_source": {  
 "name ": "Bob Hana",  
 "dob": "13/Sep/1998",  
 "course": "BFA",  
 "Addmission year": "2019",  
 "email": "bob@gmail.com",  
 "street": "724 Monroe Street",   
 "state": "Hauston",   
 "country": "United States",   
 "zip": "77063",  
 "fees": "18900"  
   }  
           },  
{  
         "index": "student1",  
         "type": "_doc",  
         "id": "02",  
         "score": 1,  
         "_source": {  
 "name ": "Jass Fernandiz",  
 "dob": "07/Aug/1996",  
 "course": "Bcom (H>
", "Addmission year": "2019", "email": "jassf@gmail.com", "street": "4225 Ersel Street", "state": "Texas", "country": "United States", "zip": "76011", "fees": "22900" } } ] }, "aggregations": { "avg_fees": { "value": "22200" } } }

Assuming that the field is absent

On the off chance that the field is absent (for which you are ascertaining normal worth>
in the report, it gets overlooked naturally and an invalid worth is returned. You can add a missing field ("missing": 0>
in total to think about missing worth as default. Execute the accompanying code:

Copy Code

POST new_student/ _search/  
{  
    "aggs": {    
       "avr_fees": {  
               "avg" : {   
                    "field": "fees",  
                    "missing": 0  
                }  
          }  
    }  
}  


Terms Aggregation

The terms total is liable for creating containers by the field values. By choosing a field (like name, confirmation year, and so on>
, it produces the containers. Determine the total name in question while making inquiry. Execute the accompanying code to look through the qualities gathered by affirmation year field: Copy Code

POST student/ _search/  
{  
   "size": 0,    
    "aggs": {    
       "group_by_Addmission year": {  
               "terms" : {   
                    "field": "Addmission year.keyword"  
                }  
          }  
    }  
}  


Response You will get the output like the below response.

{   
"took": 179,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 3,  
 "relation": "eq"  
          },  
"max_score": null,  
"hits": [ ]  
},  
  "aggregations":  {  
         "group_by_Addmission year": {  
             "student1",  
             "doc_count_error_upper_bound": 0,  
             "sum_other_doc_count": 0,  
              "buckets": [  
              {  
      "key ": "2019",  
      "doc_count": 2   
 },  
 {  
      "key": "2018",  
      "doc_count": 1  
}  
                  ]  
          }  
     }  
}  




Cardinality Aggregation

It is a typical prerequisite to track down an exceptional incentive for a field. Cardinality accumulation is useful for tracking down exceptional incentive for a specific field. It assists with deciding the quantity of interesting components present in a record. Indicate the total name in inquiry while making question. Execute the accompanying code to track down the quantity of extraordinary qualities for a field: Copy Code

POST student/ _search/  
{  
   "size": 0,    
    "aggs": {    
       "unique_fees": {  
               "cardinality" : {   
                    "field": "fees"  
                }  
          }  
    }  
}  


Response You will get the output like the below response.

{   
"took": 85,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 3,  
 "relation": "eq"  
          },  
"max_score": null,  
"hits": [ ]  
},  
  "total":  {  
              "value": 3,  
              "relation": "eq":               
       }  
       "max_score ": null  
        "hits": [ ]  
},  
"aggregations": {  
      "unique_fees": {  
      "value": 3  
}  
                  ]  
          }  
     }  
}  






Details Aggregation

Details accumulation represents measurements, which is a multi-esteem numeric matric conglomeration. It assists with producing total, avg, min, max, and include in a solitary shot. Whenever the accumulated archives are enormous, this total permits to produce every one of the measurements for a particular numeric field. The inquiry structure is same as the other accumulation. Execute the accompanying code to track down the aggregate, avg, min, max, and include in a solitary shot:



POST student/ _search/  
{  
       "aggs": {    
       "stats_fees": {  
               "extended_stats" : {   
                    "field": "fees"  
                }  
          }  
    }  
}    

Response By executing the above code, you will get the output like the below response.

{   
"took": 75,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 3,  
 "relation": "eq"  
          },  
"max_score": null,  
"hits": [ ]  
},  
"aggregation": {  
    "stats_fees": {  
            "count": 3,  
"min": 18900,  
"max": 24800,  
"avg": 22200,  
"sum": 66600,  
"sum_of_square": 1496660000,  
"variance": 9070000,  
"std_deviation": 3011.644,  
"std_deviation_bounds": {  
       "upper": 2600,  
       "lower": 700  
                  }  
          }  
     }  
}  


Filter Aggregation

The channel collection assists with sifting the archives in a solitary container. Its fundamental object is to give the best outcomes to its clients by sifting the report. How about we take a guide to channel the reports in light of "expenses" and "Addmission year". This will return reports that coordinated with the circumstances indicated in the question. You can channel the record utilizing any field you need. Execute the accompanying code to channel the report which coordinated with the circumstances indicated by you in a question: Copy Code

POST student/ _search/  
{  
       "query": {    
            "bool": {  
                "filter": [  
                     { "term": { "fees": "22900" } },  
                     { "term": { "Addmission year": "2019" } },  
                ]  
          }  
    }  
}  

Response By executing the above code, you will get the output like the below response.

{   
"took": 5,  
"timed_out": false,  
"_shards": {  
"total": 1,  
"successful": 1,  
"skipped": 0,  
"failed": 0  
},  
"hits": {  
                   "total": {  
 "value": 1,  
 "relation": "eq"  
          },  
"max_score": 0,  
"hits": [ ]  
{  
          "index": "student",  
         "type": "_doc",  
         "id": "02",  
         "score": 1,  
         "_source": {  
 "name ": "Jass Fernandiz",  
 "dob": "07/Aug/1996",  
 "course": "Bcom (H>
", "Addmission year": "2019", "email": "jassf@gmail.com", "street": "4225 Ersel Street", "state": "Texas", "country": "United States", "zip": "76011", "fees": "22900" } } ] } }






docker elasticsearch on docker

docker elasticsearch on docker