📖 Scenario: Imagine you run an online bookstore. Customers want to find books quickly and easily. Basic search only looks for exact words, but advanced search can find books even if customers type different words or want to filter by price or author.
🎯 Goal: You will create a simple Elasticsearch query that shows how advanced search features like filtering and matching multiple fields improve the search experience for users.
📋 What You'll Learn
Create an Elasticsearch index with sample book data
Add a filter to search only books cheaper than a certain price
Use a multi-match query to search both title and author fields
Print the final Elasticsearch query JSON
💡 Why This Matters
🌍 Real World
Online stores and libraries use advanced search to help users find products or books quickly by filtering and searching multiple fields.
💼 Career
Understanding how to build advanced search queries is important for roles in data engineering, backend development, and search engine optimization.
Progress0 / 4 steps
1
Create sample book data
Create a variable called books that holds a list of three dictionaries. Each dictionary should have these keys and values exactly: 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'price': 10; 'title': '1984', 'author': 'George Orwell', 'price': 15; 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'price': 12.
Elasticsearch
Hint
Use a list with three dictionaries. Each dictionary has keys 'title', 'author', and 'price' with the exact values given.
2
Set a price filter
Create a variable called max_price and set it to 12. This will be used to filter books cheaper than or equal to this price.
Elasticsearch
Hint
Just create a variable named max_price and assign it the number 12.
3
Build the advanced search query
Create a variable called search_query that holds a dictionary representing an Elasticsearch query. Use a bool query with a filter for price less than or equal to max_price, and a multi_match query inside must that searches the text 'great' in both title and author fields.
Elasticsearch
Hint
Use a dictionary with 'query' key, inside it 'bool' with 'filter' for price range and 'must' for multi_match query on title and author.
4
Print the final search query
Write a print statement to display the search_query variable.
Elasticsearch
Hint
Use print(search_query) to show the final query dictionary.
Practice
(1/5)
1. Why does advanced search improve user experience in Elasticsearch?
easy
A. It helps users find more precise and relevant results quickly.
B. It slows down the search process to check more data.
C. It shows all data without filtering to avoid missing anything.
D. It removes all search filters to simplify the interface.
Solution
Step 1: Understand the purpose of advanced search
Advanced search uses multiple conditions to narrow down results, making them more relevant.
Step 2: Connect relevance to user experience
More precise results mean users find what they want faster, improving satisfaction.
Final Answer:
It helps users find more precise and relevant results quickly. -> Option A
Step 1: Identify correct JSON structure for multiple conditions
{"query": {"bool": {"must": [{"match": {"title": "book"}}, {"range": {"price": {"lt": 20}}}]}}} uses a bool query with must array, combining match and range correctly.
Step 2: Check other options for syntax errors or wrong logic
Options A and C have invalid syntax; D uses should with price greater than 20, which is different logic.
A. Using 'match' instead of 'term' for exact status match.
B. Missing closing braces for the JSON object.
C. Range query should use 'gte' instead of 'gt'.
D. Must clause should be replaced with should.
Solution
Step 1: Check JSON syntax completeness
The query JSON is missing closing braces at the end, causing syntax error.
Step 2: Validate other parts
Using match for status is acceptable; range gt 50 is valid; must is correct for AND logic.
Final Answer:
Missing closing braces for the JSON object. -> Option B
Quick Check:
JSON syntax error = missing braces [OK]
Hint: Count opening and closing braces to spot JSON errors [OK]
Common Mistakes:
Ignoring missing braces causing parse errors
Confusing match and term queries
Changing must to should incorrectly
5. You want to improve user experience by filtering products that are either in category 'electronics' or have a rating above 4.5, but not both. Which Elasticsearch query achieves this advanced search?
Step 1: Understand the requirement for exclusive OR (XOR)
The query must match either category electronics or rating > 4.5, but not both.
Step 2: Analyze options for XOR logic
{"query": {"bool": {"should": [{"term": {"category": "electronics"}}, {"range": {"rating": {"gt": 4.5}}}], "minimum_should_match": 1, "must_not": [{"bool": {"must": [{"term": {"category": "electronics"}}, {"range": {"rating": {"gt": 4.5}}}]}}]}}} uses should with minimum_should_match 1 and excludes documents matching both conditions using must_not with must inside, implementing XOR.
Step 3: Verify other options
{"query": {"bool": {"should": [{"term": {"category": "electronics"}}, {"range": {"rating": {"gt": 4.5}}}], "minimum_should_match": 1}}} matches either or both (OR), B matches both (AND), D filters both (AND). Only C excludes both.