Bird
Raised Fist0
Elasticsearchquery~20 mins

Dynamic templates in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Templates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the mapping result of this dynamic template?

Given the following dynamic template in an Elasticsearch index mapping, what will be the data type of fields named user_email and user_age?

Elasticsearch
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "integers_as_long": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "long"
          }
        }
      }
    ]
  }
}
Auser_email: keyword, user_age: long
Buser_email: text, user_age: integer
Cuser_email: keyword, user_age: integer
Duser_email: text, user_age: long
Attempts:
2 left
💡 Hint

Look at the match_mapping_type and the mapping type for each template.

🧠 Conceptual
intermediate
2:00remaining
Which dynamic template matches fields ending with '_id'?

Which dynamic template configuration will match any field name that ends with _id and map it as a keyword type?

A{ "match": "*_id", "mapping": { "type": "keyword" } }
B{ "match_pattern": "regex", "match": ".*_id$", "mapping": { "type": "keyword" } }
C{ "match": "_id", "mapping": { "type": "keyword" } }
D{ "match_pattern": "wildcard", "match": "*_id", "mapping": { "type": "keyword" } }
Attempts:
2 left
💡 Hint

Use match_pattern with regex to match field name endings.

🔧 Debug
advanced
2:00remaining
Why does this dynamic template cause a mapping error?

Consider this dynamic template snippet. Why will it cause an error when applied?

Elasticsearch
{
  "dynamic_templates": [
    {
      "strings_as_text": {
        "match_mapping_type": "string",
        "mapping": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    },
    {
      "strings_as_keyword": {
        "match": "*_id",
        "mapping": {
          "type": "keyword"
        }
      }
    }
  ]
}
AThe templates conflict because both match string fields, causing a mapping conflict error.
BThe first template's nested fields are not allowed inside dynamic templates.
CThe second template's match pattern is invalid because it uses '*' instead of '?' wildcard.
DThe second template uses wildcard match without specifying match_pattern, causing an error.
Attempts:
2 left
💡 Hint

Check if the wildcard pattern requires a special setting.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this dynamic template

What is the syntax error in this dynamic template snippet?

Elasticsearch
{
  "dynamic_templates": [
    {
      "template_1": {
        "match_mapping_type": "string",
        "mapping": {
          "type": "keyword"
        }
      }
    },
    {
      "template_2": {
        "match": "*_date",
        "mapping": {
          "type": "date"
        }
      }
    }
  ]
}
AThe second template uses wildcard match but lacks match_pattern specification.
BMissing comma after the first template object inside the array.
CThe mapping type 'date' is invalid in dynamic templates.
DThe dynamic_templates array must contain only one template object.
Attempts:
2 left
💡 Hint

Check the use of wildcards in match fields.

🚀 Application
expert
2:00remaining
How many fields will be mapped as 'text' in this dynamic template setup?

Given this dynamic template configuration, how many fields will be mapped as text if the document contains fields: title, description, user_id, created_at?

Elasticsearch
{
  "dynamic_templates": [
    {
      "strings_as_text": {
        "match_mapping_type": "string",
        "unmatch": "*_id",
        "mapping": {
          "type": "text"
        }
      }
    },
    {
      "ids_as_keyword": {
        "match": "*_id",
        "mapping": {
          "type": "keyword"
        }
      }
    },
    {
      "dates_as_date": {
        "match_mapping_type": "date",
        "mapping": {
          "type": "date"
        }
      }
    }
  ]
}
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Check which fields match each template and consider the unmatch clause.

Practice

(1/5)
1. What is the main purpose of dynamic_templates in Elasticsearch?
easy
A. To automatically map new fields based on user-defined rules
B. To manually define each field's mapping one by one
C. To delete old indexes automatically
D. To backup data to another cluster

Solution

  1. Step 1: Understand dynamic templates concept

    Dynamic templates let Elasticsearch automatically apply mapping rules to new fields without manual intervention.
  2. Step 2: Compare options with concept

    Only To automatically map new fields based on user-defined rules describes automatic mapping based on rules, which matches the purpose of dynamic templates.
  3. Final Answer:

    To automatically map new fields based on user-defined rules -> Option A
  4. Quick Check:

    Dynamic templates = automatic mapping rules [OK]
Hint: Dynamic templates automate field mapping using rules [OK]
Common Mistakes:
  • Thinking dynamic templates delete or backup data
  • Confusing manual mapping with dynamic templates
  • Assuming dynamic templates only work for existing fields
2. Which of the following is the correct syntax to define a dynamic template that matches fields ending with _date and maps them as date type?
easy
A. "dynamic_templates": [{"dates": {"match": "*_date", "mapping": {"type": "date"}}}]
B. "dynamic_templates": [{"dates": {"match_pattern": "regex", "match": ".*_date$", "mapping": {"type": "date"}}}]
C. "dynamic_templates": [{"dates": {"match": "date", "mapping": {"type": "date"}}}]
D. "dynamic_templates": [{"dates": {"path_match": "*_date", "mapping": {"type": "date"}}}]

Solution

  1. Step 1: Identify correct syntax for regex matching

    To match fields ending with '_date', we use match_pattern": "regex" and match": ".*_date$".
  2. Step 2: Verify mapping type

    The mapping type should be date as required.
  3. Final Answer:

    "dynamic_templates": [{"dates": {"match_pattern": "regex", "match": ".*_date$", "mapping": {"type": "date"}}}] -> Option B
  4. Quick Check:

    Regex match + date type = "dynamic_templates": [{"dates": {"match_pattern": "regex", "match": ".*_date$", "mapping": {"type": "date"}}}] [OK]
Hint: Use match_pattern: regex for pattern matching [OK]
Common Mistakes:
  • Using match without match_pattern for regex
  • Using path_match instead of match for field names
  • Incorrect regex syntax or missing $ anchor
3. Given this dynamic template snippet in Elasticsearch mapping:
{
  "dynamic_templates": [
    {
      "strings_as_keywords": {
        "match_mapping_type": "string",
        "mapping": {
          "type": "keyword"
        }
      }
    }
  ]
}
What will be the type of a newly indexed field named username with string value?
medium
A. "text"
B. "object"
C. "keyword"
D. "string"

Solution

  1. Step 1: Analyze the dynamic template rule

    The template matches fields of type "string" and maps them as "keyword" type.
  2. Step 2: Apply rule to field "username"

    Since "username" is a string field, it will be mapped as "keyword" according to the template.
  3. Final Answer:

    "keyword" -> Option C
  4. Quick Check:

    String fields mapped as keyword = "keyword" [OK]
Hint: match_mapping_type: string maps to keyword here [OK]
Common Mistakes:
  • Assuming default type is text instead of keyword
  • Confusing match and match_mapping_type
  • Thinking string type is valid mapping in Elasticsearch 7+
4. You wrote this dynamic template but it does not apply to fields ending with _id. What is the error?
{
  "dynamic_templates": [
    {
      "ids": {
        "match": "*_id",
        "mapping": {
          "type": "keyword"
        }
      }
    }
  ]
}
medium
A. Incorrect mapping type, should be "text" instead of "keyword"
B. Dynamic templates must be outside the mappings block
C. Field name should be "path_match" not "match"
D. Missing "match_pattern": "regex" to enable wildcard matching

Solution

  1. Step 1: Identify the matching issue

    The pattern "*_id" uses a wildcard, which requires "match_pattern": "regex" or "wildcard" to work properly.
  2. Step 2: Confirm mapping and placement

    The mapping type "keyword" is correct for IDs, and dynamic templates are correctly placed inside mappings.
  3. Final Answer:

    Missing "match_pattern": "regex" to enable wildcard matching -> Option D
  4. Quick Check:

    Wildcard match needs match_pattern [OK]
Hint: Add match_pattern for wildcard or regex matches [OK]
Common Mistakes:
  • Forgetting match_pattern with wildcards
  • Changing mapping type unnecessarily
  • Placing dynamic templates outside mappings
5. You want to create a dynamic template that maps all numeric fields with names containing _count as long type, but only if the field value is greater than zero. Which approach correctly applies this logic?
hard
A. Dynamic templates cannot filter by field value; use mapping for "long" and filter in queries
B. Use a dynamic template with match for "*_count" and mapping type "long"; add a runtime field to check > 0
C. Use a dynamic template with match for "*_count" and mapping type "long"; then filter documents with a query for values > 0
D. Use a dynamic template with match for "*_count" and mapping type "long" with a condition for > 0

Solution

  1. Step 1: Understand dynamic templates capabilities

    Dynamic templates define field mappings based on name or type patterns, but cannot apply conditions based on field values.
  2. Step 2: Identify correct approach for value filtering

    Filtering by field value (> 0) must be done at query time, not mapping time.
  3. Final Answer:

    Dynamic templates cannot filter by field value; use mapping for "long" and filter in queries -> Option A
  4. Quick Check:

    Value filtering = query, not mapping [OK]
Hint: Dynamic templates map fields; value filters happen in queries [OK]
Common Mistakes:
  • Trying to filter field values in dynamic templates
  • Adding unsupported 'condition' in mapping
  • Confusing mapping with query filtering
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