Question

Answers

  • user image
    Anurag Srivastava
    Oct 7, 2020, 3:26:22 PM |

    There are different types of bucket aggregations but I will focus on some of the common bucket aggregations like term

    aggregation, range aggregation, filters aggregation, and filter aggregation, etc. So let's start.


    Term Aggregation:

    In term aggregation, we use to bucket the data in the form of unique field values. for example:

    GET bqstack/_search?size=0
    {
      "aggs": {
        "blog_categories" : {
          "terms" : {
            "field" : "category_name",
            "size" : 5
          }
        }
      }
    }

    In the above expression, we are creating the bucket on blog categories using the term aggregation.

    Range Aggregation:

    Using range aggregation we can bucket the data using a certain range like in blogs we have different views and we can

    create range aggregation using blog views. By using the views fields we can bucket the data on a certain range. See the

    below example:

    GET bqstack/_search?size=0
    {
      "aggs": {
        "blog_categories" : {
          "range" : {
            "field" : "views",
            "ranges": [
              { "key":"Less popular", "to": 50 },
              { "key":"popular","from": 50, "to": 100 },
              { "key":"Most popular","from": 100, "to": 200}
            ]
          }
        }
        }
    }

    In the above expression, we are creating buckets based on range aggregation where we are taking the views field and provided the criteria using which we want the bucket like from 0 to 50 views, 50 to 100 views, and 100 to 200 views.

    Filter Aggregation:

    We use filter aggregation to narrow down the number of documents used for aggregation. As the filter is used to filter out

    the documents based on certain criteria and after applying the filter we can apply the aggregation. See the below example:

    GET bqstack/_search?size=0
    {
      "aggs": {
        "blog_categories": {
          "filter": {
            "term": {
              "category_name.keyword": "DevOps"
            }
          },
          "aggs": {
            "avg_views": {
              "avg": {
                "field": "views"
              }
            }
          }
        }
      }
    }

    In the above expression first I have filtered the data with the category as DevOps and then applied the aggregation to get the average of blog views.

    This way we can apply the Bucket Aggregation is Elasticsearch, if you want to know more about Aggregation in Elasticsearch then you can refer to the following blogs:

    Introduction to Elasticsearch Aggregations
    Metrics Aggregation in Elasticsearch
    Bucket Aggregation in Elasticsearch

    If you have any queries then please let me know.


    0 | 0


Related Blogs

preview thumbnail
Introduction to Kibana

Aug 1, 2020, 6:19:45 PM | Anurag Srivastava

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava

preview thumbnail
Introduction to Elasticsearch Aggregations

Aug 14, 2018, 4:47:56 PM | Anurag Srivastava

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

preview thumbnail
Basics of Data Search in Elasticsearch

Aug 4, 2018, 7:02:21 AM | Anurag Srivastava

Top Blogs

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

preview thumbnail
Elasticsearch REST APIs

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

preview thumbnail
preview thumbnail
Create a Chess board in PHP

Mar 9, 2020, 8:45:41 AM | Rocky Paul

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava