0
0
Elasticsearchquery~30 mins

Why search is Elasticsearch's core purpose - See It in Action

Choose your learning style9 modes available
Why Search is Elasticsearch's Core Purpose
📖 Scenario: Imagine you have a large collection of documents, like a library of books or a big list of customer reviews. You want to find specific information quickly, like all books about cooking or reviews mentioning "great service". Elasticsearch helps you do this fast and easily by searching through all your data.
🎯 Goal: You will create a simple Elasticsearch index with some documents, configure a search query, run the search, and see the results. This will show why search is the main reason people use Elasticsearch.
📋 What You'll Learn
Create an Elasticsearch index called library with sample documents
Add a configuration variable for the search term
Write a search query to find documents matching the search term
Print the search results showing matched documents
💡 Why This Matters
🌍 Real World
Elasticsearch is used in many websites and apps to let users quickly find relevant information from large amounts of data.
💼 Career
Knowing how to create indexes and write search queries is essential for roles like data engineer, backend developer, and search specialist.
Progress0 / 4 steps
1
Create the Elasticsearch index with sample documents
Create an Elasticsearch index called library with these exact documents: {"title": "Cooking Basics", "content": "Learn to cook delicious meals."}, {"title": "Gardening Tips", "content": "Grow your own vegetables."}, and {"title": "Travel Guide", "content": "Explore new places around the world."}. Use the index API to add these documents.
Elasticsearch
Need a hint?

Use the PUT method to add documents to the library index with IDs 1, 2, and 3.

2
Add a search term variable
Create a variable called search_term and set it to the string "cook". This will be the word you want to find in the documents.
Elasticsearch
Need a hint?

Just write search_term = "cook" below the existing code.

3
Write a search query using the search term
Write a search query using the search_term variable to find documents in the library index where the content field matches the search term. Use the match query inside the query object.
Elasticsearch
Need a hint?

Use GET /library/_search with a query object containing match on content with the value of search_term.

4
Print the search results
Print the search results showing the titles of documents that matched the search term "cook". Assume the search response is stored in a variable called response. Use a loop to print each matched document's _source.title.
Elasticsearch
Need a hint?

Loop over response["hits"]["hits"] and print each _source["title"].