Practical E-commerce Search
Let’s create a practical example of an e-commerce search that combines multiple filtering techniques.
Imagine we have an e-commerce website with a variety of products. We want to create a search feature that allows users to find products based on the following criteria:
- The product name should contain the term “phone“.
- The category should be “electronics“.
- The price should be between 200 and 1000.
- The product should have a discount.
- The brand should be either “BrandA” or “BrandB“.
Here’s how we can achieve this using Elasticsearch filters:
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "phone"
}
}
],
"filter": [
{
"term": {
"category": "electronics"
}
},
{
"range": {
"price": {
"gte": 200,
"lte": 1000
}
}
},
{
"exists": {
"field": "discount"
}
},
{
"terms": {
"brand": ["BrandA", "BrandB"]
}
}
]
}
}
}
In this example:
- The must clause ensures the name field contains “phone“.
- The filter clauses restrict the results based on category, price range, existence of discount, and brand.
Filtering Documents in Elasticsearch
Filtering documents in Elasticsearch is a crucial skill for efficiently narrowing down search results to meet specific criteria. Whether you’re building a search engine for an application or performing detailed data analysis, understanding how to use filters can greatly enhance your ability to find relevant documents quickly.
This guide will walk you through the basics and advanced techniques of filtering documents in Elasticsearch with detailed explanations, examples, and outputs.