Skip to content

Instantly share code, notes, and snippets.

@glenacota
Last active March 3, 2019 21:57
Show Gist options
  • Save glenacota/bc9fed925e0f3279216c615c62832dbd to your computer and use it in GitHub Desktop.
Save glenacota/bc9fed925e0f3279216c615c62832dbd to your computer and use it in GitHub Desktop.
An exercise to practice with index templates and dynamic templates in Elasticsearch.
# GOAL: Create index templates that satisfy a given set of requirements
# INITIAL SETUP: (i) a running Elasticsearch cluster with at least one node and a Kibana instance, (ii) no index or index template that starts by `hamlet`
# Copy-paste the following instructions into your Kibana console, and work directly from there
# Create the index template `hamlet_template`, so that the template (i) matches any index that starts by "hamlet_" or "hamlet-", (ii) allocates one primary shard and no replicas for each matching index
# Create the indices `hamlet2` and `hamlet_test`
# Verify that only `hamlet_test` applies the settings defined in `hamlet_template`
# In one request, delete both `hamlet2` and `hamlet_test`
# Update `hamlet_template` by defining a mapping for the type "_doc", so that (i) the type has three fields, named `speaker`, `line_number`, and `text_entry`, (ii) `speaker` and `line_number` map to a unanalysed string, (iii) `text_entry` uses an "english" analyzer
# Create the index `hamlet-1` and add some documents by running the following _bulk command
PUT hamlet-1/_doc/_bulk
{"index":{"_index":"hamlet-1","_id":0}}
{"line_number":"1.1.1","speaker":"BERNARDO","text_entry":"Whos there?"}
{"index":{"_index":"hamlet-1","_id":1}}
{"line_number":"1.1.2","speaker":"FRANCISCO","text_entry":"Nay, answer me: stand, and unfold yourself."}
{"index":{"_index":"hamlet-1","_id":2}}
{"line_number":"1.1.3","speaker":"BERNARDO","text_entry":"Long live the king!"}
{"index":{"_index":"hamlet-1","_id":3}}
{"line_number":"1.2.1","speaker":"KING CLAUDIUS","text_entry":"Though yet of Hamlet our dear brothers death"}
# Verify that the mapping of `hamlet-1` is consistent with what defined in `hamlet_template`
# Update the mapping of `hamlet_template` so as to (i) remove all fields but `text_entry`, (ii) disable queries but not aggregations for `text_entry`
# Update `hamlet_template` so as to (i) dynamically map to an integer any field that starts by "number_", (iv) dynamically map to unanalysed text any string field
# Create the index `hamlet-2` and add a document by running the following command
POST hamlet-2/_doc/4
{
"text_entry": "With turbulent and dangerous lunacy?",
"line_number": "3.1.4",
"number_act": "3",
"speaker": "KING CLAUDIUS"
}
# Verify that the mapping of `hamlet-2` is consistent with what defined in `hamlet_template`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment