0
0
Elasticsearchquery~10 mins

Nested aggregations in Elasticsearch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a nested aggregation named "genres".

Elasticsearch
{
  "aggs": {
    "genres": {
      "[1]": {
        "path": "genres"
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Anested
Brange
Cfilter
Dterms
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'terms' instead of 'nested' for nested fields.
Forgetting to specify the 'path' for the nested aggregation.
2fill in blank
medium

Complete the code to add a sub-aggregation that counts documents by "genres.name".

Elasticsearch
{
  "aggs": {
    "genres": {
      "nested": {
        "path": "genres"
      },
      "aggs": {
        "genre_names": {
          "[1]": {
            "field": "genres.name.keyword"
          }
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aterms
Bnested
Cavg
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'nested' again inside the nested aggregation.
Using metric aggregations like 'avg' or 'max' instead of 'terms'.
3fill in blank
hard

Fix the error in the aggregation by completing the missing key to calculate the average rating inside the nested aggregation.

Elasticsearch
{
  "aggs": {
    "genres": {
      "nested": {
        "path": "genres"
      },
      "aggs": {
        "avg_rating": {
          "[1]": {
            "field": "genres.rating"
          }
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Aterms
Bsum
Cmax
Davg
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'terms' which is a bucket aggregation instead of 'avg'.
Using 'sum' or 'max' which calculate different metrics.
4fill in blank
hard

Fill both blanks to filter nested genres with rating greater than 4 and then get the average rating.

Elasticsearch
{
  "aggs": {
    "genres": {
      "nested": {
        "path": "genres"
      },
      "aggs": {
        "filtered_genres": {
          "[1]": {
            "range": {
              "genres.rating": {
                "gt": 4
              }
            }
          },
          "aggs": {
            "avg_rating": {
              "[2]": {
                "field": "genres.rating"
              }
            }
          }
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Afilter
Bavg
Cterms
Dnested
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'terms' instead of 'filter' for filtering.
Using 'nested' inside the nested aggregation again incorrectly.
5fill in blank
hard

Fill all three blanks to create a nested aggregation on "comments", filter comments with "likes" greater than 10, and get the max likes.

Elasticsearch
{
  "aggs": {
    "comments": {
      "[1]": {
        "path": "comments"
      },
      "aggs": {
        "popular_comments": {
          "[2]": {
            "range": {
              "comments.likes": {
                "gt": 10
              }
            }
          },
          "aggs": {
            "max_likes": {
              "[3]": {
                "field": "comments.likes"
              }
            }
          }
        }
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Anested
Bfilter
Cmax
Dterms
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'terms' instead of 'filter' for filtering.
Using 'avg' instead of 'max' for maximum value.