What if you could find any piece of information instantly, no matter how big your data is?
Creating an index in Elasticsearch - Why You Should Know This
Imagine you have a huge pile of books scattered all over your room. You want to find a specific book quickly, but there is no order or system to locate it.
Searching through every book one by one takes forever and is frustrating. You might lose track or pick the wrong book. It's slow and full of mistakes.
Creating an index is like making a neat catalog or table of contents for your books. It organizes information so you can jump straight to what you want without searching everything.
Search all documents one by one to find matches
Create an index first, then search quickly using the index
With an index, you can find data instantly, even in huge collections, making your searches fast and reliable.
Online stores use indexes to quickly show you products matching your search, even if they have millions of items.
Manual searching is slow and error-prone.
Indexes organize data for fast access.
Creating an index makes searching efficient and scalable.
Practice
Solution
Step 1: Understand the role of an index
An index in Elasticsearch is like a folder that organizes documents to make searching fast and efficient.Step 2: Compare options with index purpose
Deleting, backing up, or encrypting data are not the main functions of an index.Final Answer:
To organize data for fast searching -> Option DQuick Check:
Index = Organize data for search [OK]
- Confusing index with backup or encryption
- Thinking index deletes data
- Assuming index automatically secures data
products in Elasticsearch?Solution
Step 1: Recall HTTP methods for index creation
Elasticsearch uses the PUT method to create an index with a specific name.Step 2: Match method with index name
PUT /products correctly creates the index named 'products'. POST is used for adding documents, GET for retrieving, DELETE for removing.Final Answer:
PUT /products -> Option AQuick Check:
PUT creates index [OK]
- Using POST instead of PUT for index creation
- Confusing GET with creation
- Trying DELETE to create index
{
"settings": {"number_of_shards": 1},
"mappings": {
"properties": {
"name": {"type": "text"},
"price": {"type": "float"}
}
}
}What will be the type of the
price field in the index?Solution
Step 1: Read the mapping for the price field
The mapping defines "price" with "type": "float" which means it stores decimal numbers.Step 2: Understand field types
Float is a number with decimals, integer is whole number, text is for full text search, keyword is for exact matches.Final Answer:
float -> Option BQuick Check:
price type = float [OK]
- Confusing float with integer
- Assuming text type for numbers
- Mixing keyword with numeric types
PUT /library
{
"settings": {
"number_of_shards": "two"
},
"mappings": {
"properties": {
"title": {"type": "text"}
}
}
}Solution
Step 1: Check the settings syntax
number_of_shards must be a numeric value, but "two" is a string, causing an error.Step 2: Validate other parts
Field type 'text' is valid for title, PUT is correct method, index name 'library' is present.Final Answer:
number_of_shards should be a number, not a string -> Option AQuick Check:
number_of_shards = number [OK]
- Using string instead of number for shards
- Confusing field types for text fields
- Using wrong HTTP method for index creation
users with a field age that should only accept whole numbers and be searchable. Which mapping is correct?Solution
Step 1: Understand the data type needed for age
Age is a whole number, so integer type is appropriate to store numeric whole values.Step 2: Check searchability and field types
Text is for full text search, float allows decimals, keyword is for exact matches but not numeric operations.Final Answer:
{ "mappings": { "properties": { "age": { "type": "integer" } } } } -> Option CQuick Check:
Whole number field = integer type [OK]
- Using text type for numeric fields
- Choosing float when decimals not needed
- Using keyword for numeric data
Ready to prove it?
Pass the test and this pattern moves unknown
