0
0
Elasticsearchquery~20 mins

Object and nested types in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Elasticsearch Nested Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Elasticsearch query?

Given the following Elasticsearch mapping and query, what will be the result count?

Elasticsearch
{
  "mappings": {
    "properties": {
      "user": {
        "type": "object",
        "properties": {
          "name": { "type": "keyword" },
          "age": { "type": "integer" }
        }
      }
    }
  }
}

POST /users/_search
{
  "query": {
    "bool": {
      "must": [
        { "term": { "user.name": "alice" } },
        { "range": { "user.age": { "gte": 30 } } }
      ]
    }
  }
}
AReturns documents where user.age is 30 or more but ignores user.name filter
BReturns documents where user.name contains 'alice' anywhere and user.age is exactly 30
CReturns documents where user.name is 'alice' but ignores user.age filter
DReturns all documents where user.name is exactly 'alice' and user.age is 30 or more
Attempts:
2 left
💡 Hint

Remember that term queries match exact values and range queries filter by numeric ranges.

🧠 Conceptual
intermediate
1:30remaining
Why use nested type instead of object type in Elasticsearch?

Choose the best reason to use nested type instead of object type for a field containing arrays of objects.

ANested type allows querying each object in the array independently, preserving relationships between fields inside each object.
BNested type stores data in a separate index, improving write speed.
CNested type automatically flattens all objects into a single document for faster search.
DNested type disables indexing of the field to save storage space.
Attempts:
2 left
💡 Hint

Think about how Elasticsearch matches fields inside arrays of objects.

🔧 Debug
advanced
2:30remaining
Why does this nested query return zero results?

Given this mapping and query, why does the query return no results?

Elasticsearch
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "author": { "type": "keyword" },
          "message": { "type": "text" }
        }
      }
    }
  }
}

POST /posts/_search
{
  "query": {
    "bool": {
      "must": [
        { "term": { "comments.author": "john" } },
        { "match": { "comments.message": "great" } }
      ]
    }
  }
}
AThe mapping is incorrect; nested type is not supported for comments.
BThe term query is case-sensitive and 'john' does not match the stored value.
CThe query does not use a nested query wrapper, so it treats comments as flat fields causing no matches.
DThe match query cannot be used on nested fields.
Attempts:
2 left
💡 Hint

Check how nested fields must be queried in Elasticsearch.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this nested mapping definition

Which option contains the correct syntax for defining a nested field items with properties product (keyword) and quantity (integer)?

A
{
  "items": {
    "type": "object",
    "nested": true,
    "properties": {
      "product": { "type": "keyword" },
      "quantity": { "type": "integer" }
    }
  }
}
B
{
  "items": {
    "type": "nested",
    "properties": {
      "product": { "type": "keyword" },
      "quantity": { "type": "integer" }
    }
  }
}
C
{
  "items": {
    "type": "nested",
    "fields": {
      "product": { "type": "keyword" },
      "quantity": { "type": "integer" }
    }
  }
}
D
{
  "items": {
    "nested": true,
    "properties": {
      "product": { "type": "keyword" },
      "quantity": { "type": "integer" }
    }
  }
}
Attempts:
2 left
💡 Hint

Check the correct keyword to specify nested type in Elasticsearch mappings.

🚀 Application
expert
3:00remaining
How many documents are returned by this nested aggregation?

Given this mapping and data, what is the count of unique authors returned by the nested aggregation?

Elasticsearch
{
  "mappings": {
    "properties": {
      "reviews": {
        "type": "nested",
        "properties": {
          "author": { "type": "keyword" },
          "rating": { "type": "integer" }
        }
      }
    }
  }
}

Data example:
{
  "reviews": [
    {"author": "alice", "rating": 5},
    {"author": "bob", "rating": 4},
    {"author": "alice", "rating": 3}
  ]
}

Aggregation query:
{
  "aggs": {
    "nested_reviews": {
      "nested": { "path": "reviews" },
      "aggs": {
        "unique_authors": {
          "cardinality": { "field": "reviews.author" }
        }
      }
    }
  }
}
A2
B3
C0
D1
Attempts:
2 left
💡 Hint

Count distinct authors inside the nested reviews array.