📖 Scenario: You are building a simple search system for a small online bookstore. You want to store book information in Elasticsearch. Some fields need to be searchable by full text, while others need exact matching.
🎯 Goal: Create an Elasticsearch index mapping that uses text and keyword field types correctly. Then add a document and query it to see the difference.
📋 What You'll Learn
Create an index mapping with a title field of type text
Create an index mapping with an isbn field of type keyword
Index a document with a sample book title and ISBN
Query the document by full text search on title
Query the document by exact match on isbn
💡 Why This Matters
🌍 Real World
Online stores and libraries use Elasticsearch to let users search books by title (full text) and filter by ISBN or categories (exact match).
💼 Career
Understanding <code>text</code> vs <code>keyword</code> fields is essential for building efficient search applications and indexing data correctly in Elasticsearch.
Progress0 / 4 steps
1
Create index mapping with title as text and isbn as keyword
Create an index called books with a mapping where title is of type text and isbn is of type keyword. Use the Elasticsearch PUT API with the exact JSON structure shown.
Elasticsearch
Hint
Use the PUT method to create the index with the mapping inside the mappings key.
2
Index a document with title and isbn
Index a document into the books index with title set to "The Great Gatsby" and isbn set to "9780743273565". Use the POST API with the exact JSON body shown.
Elasticsearch
Hint
Use POST /books/_doc to add the document with the given fields.
3
Search the document by full text on title
Write a search query using the GET /books/_search API that searches for the word "Great" in the title field using a match query.
Elasticsearch
Hint
Use a match query on the title field to find documents containing the word "Great".
4
Search the document by exact match on isbn
Write a search query using the GET /books/_search API that finds the document with isbn exactly equal to "9780743273565" using a term query.
Elasticsearch
Hint
Use a term query on the isbn field to find the exact match.
Practice
(1/5)
1. Which Elasticsearch field type should you use if you want to perform full-text search inside sentences?
easy
A. text
B. keyword
C. integer
D. date
Solution
Step 1: Understand the purpose of text fields
Text fields are designed for full-text search, allowing analysis and tokenization of sentences.
Step 2: Compare with keyword fields
Keyword fields are for exact matches and do not analyze the text, so they are not suitable for searching inside sentences.
Final Answer:
text -> Option A
Quick Check:
Full-text search = text [OK]
Hint: Full-text search needs analyzed text fields [OK]
Common Mistakes:
Choosing keyword for full-text search
Confusing integer or date types with text
Thinking keyword fields tokenize text
2. Which of the following is the correct way to define a keyword field in an Elasticsearch mapping?
easy
A. "name": { "type": "text" }
B. "name": { "type": "keyword" }
C. "name": { "type": "string" }
D. "name": { "type": "fulltext" }
Solution
Step 1: Recall Elasticsearch field types
Elasticsearch uses "keyword" as the type for exact match fields.
Step 2: Identify the correct syntax
"keyword" is the correct type name; "text" is for analyzed fields, "string" and "fulltext" are invalid types.
Final Answer:
"name": { "type": "keyword" } -> Option B
Quick Check:
Keyword field syntax = "name": { "type": "keyword" } [OK]
Hint: Keyword type is exactly "keyword" in mapping [OK]