Solution: Search on Elasticsearch
Verify your answers to the search challenge.
We'll cover the following...
- Solution 1: Products with the name field containing "Samsung"
- Solution 2: "Apple" products in the $500–$1000 price range
- Solution 3: Products with the description field containing the exact phrase "premium ultrabook"
- Solution 4: Products in the "Electronics" category with a price range of $500–$1000 and a minimum rating of 4.5
- Solution 5: Products with the terms "smartphone" and "android" in the description field
- Solution 6: Products containing the seller field that starts with the prefix "Apple"
- Solution 7: Products in the "Electronics" category with a $500–$1000 price range and an approximate match for "applle air"
- Kibana widget
Solution 1: Products with the name
field containing "Samsung"
To solve this query challenge, we can use the match
query, which performs a full-text search on the specified field, making it suitable for searching text-based fields like the name
field.
To construct the query, we need to send a search request to the products
index and specify the query type as match
for the name
field. The query text should be set as "Samsung"
.
GET /products/_search{"query": {"match": {"name": "Samsung"}}}
Solution 2: "Apple"
products in the $500–$1000 price range
To solve this query challenge, we can utilize a bool
query, and within its must
clause, include a range
query to filter the products based on the price range of $500 to $1000 and a term
query to search for an exact match of the brand "Apple"
.
GET /products/_search{"query": {"bool": {"must": [{"range": {"price": {"gte": 500,"lte": 1000}}},{"term": {"brand": "Apple"}}]}}}
Solution 3: Products with the description
field containing the exact phrase "premium ultrabook"
To solve this query challenge, we can utilize the match_phrase
query to ensure that the entire phrase ...