Bird
Raised Fist0
Elasticsearchquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between object and nested types in Elasticsearch?
easy
A. Both types store data the same way but nested type is faster.
B. Object type stores arrays of objects separately, while nested type stores grouped fields as one JSON object.
C. Nested type is used only for numeric fields, object type for text fields.
D. Nested type stores arrays of objects separately to keep their fields linked during searches, while object type stores grouped fields as one JSON object.

Solution

  1. Step 1: Understand object type storage

    Object type stores grouped fields as a single JSON object inside a document, meaning all fields are flattened and stored together.
  2. Step 2: Understand nested type storage

    Nested type stores arrays of objects separately to maintain the relationship between fields within each object during searches.
  3. Final Answer:

    Nested type stores arrays of objects separately to keep their fields linked during searches, while object type stores grouped fields as one JSON object. -> Option D
  4. Quick Check:

    Nested keeps object fields linked = C [OK]
Hint: Nested keeps object arrays linked; object flattens fields [OK]
Common Mistakes:
  • Confusing object and nested storage behavior
  • Thinking nested is only for numeric fields
  • Assuming both types store data the same way
2. Which of the following is the correct way to define a nested type field in an Elasticsearch mapping?
easy
A. "comments": { "type": "object" }
B. "comments": { "type": "keyword" }
C. "comments": { "type": "nested" }
D. "comments": { "type": "text" }

Solution

  1. Step 1: Recall nested type syntax

    To define a nested field, the type must be explicitly set to "nested" in the mapping.
  2. Step 2: Check options

    "comments": { "type": "nested" } correctly sets "comments" field type to "nested". Other options use incorrect types for nested data.
  3. Final Answer:

    "comments": { "type": "nested" } -> Option C
  4. Quick Check:

    Nested type requires "type": "nested" [OK]
Hint: Use "type": "nested" to define nested fields [OK]
Common Mistakes:
  • Using "object" instead of "nested" for nested arrays
  • Confusing text or keyword types with nested
  • Omitting the type field
3. Given this mapping with a nested field comments and this document:
{"title": "Post", "comments": [{"user": "alice", "message": "Hi"}, {"user": "bob", "message": "Hello"}]}

Which query will correctly find documents where comments.user is "alice" and comments.message is "Hello" in the same comment object?
medium
A. { "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } } }
B. { "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } }
C. { "query": { "term": { "comments.user": "alice" } } }
D. { "query": { "nested": { "path": "comments", "query": { "term": { "comments.message": "Hi" } } } } }

Solution

  1. Step 1: Understand nested query usage

    To search inside nested fields and keep field relationships, use a nested query with the correct path and combined conditions inside it.
  2. Step 2: Analyze options

    { "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } } } uses a nested query on "comments" with a bool must for both user "alice" and message "Hello" in the same nested object. { "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } searches flattened fields, losing the link. { "query": { "term": { "comments.user": "alice" } } } only matches user. { "query": { "nested": { "path": "comments", "query": { "term": { "comments.message": "Hi" } } } } } searches for message "Hi" only.
  3. Final Answer:

    Uses nested query to match both conditions in the same nested object. -> Option A
  4. Quick Check:

    Nested query with bool must inside = A [OK]
Hint: Use nested query with bool must for multiple nested conditions [OK]
Common Mistakes:
  • Using bool query without nested for nested fields
  • Searching fields separately losing object linkage
  • Using term query without nested path
4. You wrote this mapping for a nested field:
{ "properties": { "tags": { "type": "nested", "properties": { "name": { "type": "keyword" } } } } }

But your nested query returns no results even though documents have matching tags. What is the likely mistake?
medium
A. You forgot to use a nested query; used a regular bool query instead.
B. You defined tags as object type instead of nested.
C. You used match query on keyword field instead of term query.
D. You indexed tags as text instead of keyword.

Solution

  1. Step 1: Check mapping correctness

    The mapping correctly defines "tags" as nested with keyword "name" field, so mapping is fine.
  2. Step 2: Identify query mistake

    If nested query returns no results, often the cause is using a regular bool query instead of a nested query to search nested fields, losing the link between nested objects.
  3. Final Answer:

    You forgot to use a nested query; used a regular bool query instead. -> Option A
  4. Quick Check:

    Nested fields require nested queries [OK]
Hint: Always use nested query for nested fields [OK]
Common Mistakes:
  • Using bool query without nested for nested fields
  • Confusing object and nested types in mapping
  • Using match query on keyword fields
5. You have documents with a nested field orders containing product and quantity. You want to find documents where there is an order with product "apple" and quantity greater than 10, but also an order with product "banana" and quantity less than 5. Which query structure correctly finds such documents?
hard
A. { "query": { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } }, { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } } }
B. { "query": { "bool": { "must": [ { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } } ] } } } }, { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } } } ] } } }
C. { "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } }, { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } }
D. { "query": { "nested": { "path": "orders", "query": { "term": { "orders.product": "apple" } } } } }

Solution

  1. Step 1: Understand nested query for multiple conditions

    Each nested object must be queried separately to keep conditions linked inside the same nested object.
  2. Step 2: Analyze query options

    { "query": { "bool": { "must": [ { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } } ] } } } }, { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } } } ] } } } uses two nested queries combined with bool must, one for "apple" orders with quantity > 10, another for "banana" orders with quantity < 5, correctly matching documents with both conditions in different nested objects.
  3. Step 3: Identify incorrect options

    { "query": { "nested": { "path": "orders", "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } }, { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } } } tries to combine all conditions in one nested query, which requires all conditions in the same nested object (impossible for different products). { "query": { "bool": { "must": [ { "term": { "orders.product": "apple" } }, { "range": { "orders.quantity": { "gt": 10 } } }, { "term": { "orders.product": "banana" } }, { "range": { "orders.quantity": { "lt": 5 } } } ] } } } ignores nested queries, losing object linkage. { "query": { "nested": { "path": "orders", "query": { "term": { "orders.product": "apple" } } } } } only queries for "apple" product.
  4. Final Answer:

    Uses separate nested queries for each product condition. -> Option B
  5. Quick Check:

    Separate nested queries for each condition = A [OK]
Hint: Use separate nested queries for different nested object conditions [OK]
Common Mistakes:
  • Combining multiple product conditions in one nested query
  • Not using nested queries for nested fields
  • Assuming all conditions apply to the same nested object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard