![]() |
VOOZH | about |
Elasticsearch is a powerful search engine that provides a flexible and powerful query language called Query DSL (Domain Specific Language). Query DSL allows you to write complex search queries to retrieve the most relevant data from your Elasticsearch indices. This article will guide you through the basics and advanced features of Query DSL, with detailed examples and outputs, to help you master complex search queries in Elasticsearch.
Query DSL in Elasticsearch is a JSON-based query language that enables you to construct complex and precise search queries. It is composed of two types of clauses:
Before diving into complex queries, let's start with a basic example using the match query, which is a type of leaf query clause.
GET /products/_search
{
"query": {
"match": {
"description": "wireless headphones"
}
}
}
In this example:
The bool query is a compound query clause that allows you to combine multiple queries using boolean logic. It consists of four clauses:
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "description": "wireless headphones" } }
],
"filter": [
{ "term": { "brand": "BrandA" } }
],
"should": [
{ "range": { "price": { "lte": 100 } } }
],
"must_not": [
{ "term": { "color": "red" } }
]
}
}
}
In this example:
Sometimes, you need to query nested objects. Nested queries allow you to search within objects that are embedded within other objects.
Consider a document structure where a product has nested reviews:
{
"name": "Wireless Headphones",
"brand": "BrandA",
"reviews": [
{ "user": "John", "rating": 5, "comment": "Excellent!" },
{ "user": "Jane", "rating": 4, "comment": "Very good." }
]
}
To search for products with a specific review.rating, you can use a nested query.
GET /products/_search
{
"query": {
"nested": {
"path": "reviews",
"query": {
"bool": {
"must": [
{ "match": { "reviews.rating": 5 } }
]
}
}
}
}
}
In this example:
Aggregations allow you to summarize and analyze your data. They can be used to perform arithmetic, create histograms, compute statistics, and more.
Let's aggregate the average rating of products by brand.
GET /products/_search
{
"size": 0,
"aggs": {
"avg_rating_by_brand": {
"terms": {
"field": "brand"
},
"aggs": {
"avg_rating": {
"avg": {
"field": "reviews.rating"
}
}
}
}
}
}
In this example:
Scripted queries allow you to use scripts to customize how documents are scored or filtered. This is useful for advanced calculations and custom relevance scoring.
Let's create a query that boosts products based on a custom formula using a script.
GET /products/_search
{
"query": {
"function_score": {
"query": {
"match": { "description": "wireless headphones" }
},
"functions": [
{
"script_score": {
"script": {
"source": "doc['reviews.rating'].value * doc['popularity'].value"
}
}
}
]
}
}
}
In this example:
Elasticsearch supports geospatial data, allowing you to perform queries based on geographical locations.
Let's find products available within a certain distance from a specific location.
GET /stores/_search
{
"query": {
"bool": {
"must": [
{ "match": { "product": "wireless headphones" } }
],
"filter": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}
}
}
In this example:
Date queries allow you to filter and search based on date and time ranges.
Let's search for products added within the last month.
GET /products/_search
{
"query": {
"range": {
"date_added": {
"gte": "now-1M/M",
"lte": "now/M"
}
}
}
}
In this example:
Let's combine multiple features into a complex query.
We want to find products that are highly rated, affordable, from a specific brand, available within a certain distance, and added within the last month.
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "description": "wireless headphones" } }
],
"filter": [
{ "term": { "brand": "BrandA" } },
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
},
{
"range": {
"date_added": {
"gte": "now-1M/M",
"lte": "now/M"
}
}
},
{
"range": {
"price": {
"lte": 100
}
}
}
],
"should": [
{
"nested": {
"path": "reviews",
"query": {
"bool": {
"must": [
{ "range": { "reviews.rating": { "gte": 4 } } }
]
}
}
}
}
],
"must_not": [
{ "term": { "color": "red" } }
]
}
},
"aggs": {
"avg_rating_by_brand": {
"terms": {
"field": "brand"
},
"aggs": {
"avg_rating": {
"avg": {
"field": "reviews.rating"
}
}
}
}
}
}
In this example:
Using Query DSL in Elasticsearch allows you to construct complex and powerful search queries. By combining various query clauses and leveraging features like nested queries, aggregations, scripted queries, geo queries, and date queries, you can retrieve the most relevant data tailored to your needs.
This guide provided an overview of how to use Query DSL for complex search queries, with detailed examples and outputs. With these tools at your disposal, you can effectively harness the full power of Elasticsearch to build sophisticated search applications.