Build a Simple Full-Text Search with Elasticsearch
📖 Scenario: You are creating a small search engine for a library's book collection. The library wants to quickly find books by searching words in the title or description.
🎯 Goal: Build a basic Elasticsearch setup that stores book data and allows searching for books by keywords in their titles or descriptions.
📋 What You'll Learn
Create an Elasticsearch index called library with appropriate mappings for text fields
Add a few book documents with title and description fields
Write a search query to find books matching a keyword in either title or description
Display the search results showing the book titles
💡 Why This Matters
🌍 Real World
Full-text search engines help users find relevant information quickly in large collections of documents, like books, articles, or websites.
💼 Career
Understanding how to set up and query Elasticsearch is valuable for roles in data engineering, backend development, and search engine optimization.
Progress0 / 4 steps
1
Create the Elasticsearch index with mappings
Write a command to create an Elasticsearch index called library with mappings for title and description fields as text type.
Elasticsearch
Hint
Use the PUT method to create the index and define mappings with properties for title and description as text.
2
Add book documents to the index
Add three book documents to the library index with these exact entries: 1: title: 'The Great Gatsby', description: 'A novel set in the Jazz Age.' 2: title: 'Learning Elasticsearch', description: 'A guide to full-text search.' 3: title: 'Cooking 101', description: 'Basic recipes for beginners.'
Elasticsearch
Hint
Use POST /library/_doc/<id> to add each book with the exact title and description.
3
Write a search query for a keyword
Write a search query to find books in the library index where the keyword search appears in either the title or description fields. Use a multi_match query with query set to search and fields set to ["title", "description"].
Elasticsearch
Hint
Use GET /library/_search with a multi_match query specifying query as search and fields as ["title", "description"].
4
Display the titles of the search results
Write a command to display only the title fields from the search results of the previous query. Use _source filtering to include only title in the output.
Elasticsearch
Hint
Add "_source": ["title"] to the search query to show only the titles in the results.
Practice
(1/5)
1. What is the main purpose of a full-text search engine like Elasticsearch?
easy
A. To create user interfaces for websites
B. To store images and videos efficiently
C. To quickly find words or phrases inside large amounts of text
D. To perform mathematical calculations on numbers
Solution
Step 1: Understand full-text search
Full-text search engines are designed to search text data quickly and efficiently.
Step 2: Identify the main use case
They help find words or phrases inside large text collections, unlike storing media or calculations.
Final Answer:
To quickly find words or phrases inside large amounts of text -> Option C
Quick Check:
Full-text search = find words fast [OK]
Hint: Full-text search = fast text lookup [OK]
Common Mistakes:
Confusing full-text search with media storage
Thinking it does math calculations
Assuming it builds website interfaces
2. Which Elasticsearch query is commonly used to search text fields for matching words?
easy
A. term
B. match
C. range
D. exists
Solution
Step 1: Review query types
Elasticsearch has different queries: term for exact matches, range for numbers, exists for presence.
Step 2: Identify text search query
The match query is designed to find words inside text fields using full-text search.
Final Answer:
match -> Option B
Quick Check:
Text search query = match [OK]
Hint: Use match query for text search in Elasticsearch [OK]
Common Mistakes:
Using term query for full-text search
Confusing range query with text search
Using exists query to find words
3. Given this Elasticsearch query snippet, what does it do?
A. Searches documents where the description field exists
B. Searches documents where the description contains the exact phrase "fast car"
C. Searches documents where the description contains both words "fast" and "car" in order
D. Searches documents where the description contains either "fast" or "car"
Solution
Step 1: Understand match query behavior
The match query analyzes the input text and searches for documents containing any of the words.
Step 2: Analyze the query text "fast car"
It looks for documents containing "fast" or "car", not necessarily as an exact phrase or order.
Final Answer:
Searches documents where the description contains either "fast" or "car" -> Option D
Quick Check:
Match query finds any matching words [OK]
Hint: Match query finds any word in text [OK]
Common Mistakes:
Assuming match query searches exact phrase
Thinking it requires words in order
Confusing with exists query
4. You wrote this Elasticsearch query but it returns no results:
{
"query": {
"match": { "title": "" }
}
}
What is the likely problem?
medium
A. The match query cannot search empty strings
B. The field name "title" is invalid syntax
C. Elasticsearch requires a range query for text fields
D. The query must use term instead of match
Solution
Step 1: Check the query input
The match query is given an empty string "" to search, which means no words to match.
Step 2: Understand match query behavior with empty input
Match queries need some text to find matches; empty strings return no results.
Final Answer:
The match query cannot search empty strings -> Option A
Quick Check:
Empty match query = no results [OK]
Hint: Match query needs non-empty text [OK]
Common Mistakes:
Thinking field name syntax is wrong
Using range query for text fields
Confusing term and match query roles
5. You want to find documents where the text field "content" contains the word "apple" but not the word "fruit". Which Elasticsearch query structure should you use?