Skip to content
All essays
WebMarch 27, 202510 min

Elasticsearch Query DSL: Master Search Queries

Master Elasticsearch Query DSL. Build powerful search queries with filters, aggregations, and analytics.

Ü
Ümit Uz
Mobile & Full Stack Developer

Understanding Query DSL

Elasticsearch Query DSL (Domain Specific Language) enables powerful full-text search and analytics. It's JSON-based and flexible.

json
{
  "query": {
    "match": {
      "title": "search engines"
    }
  }
}

Query Types

Match Query

json
// Full-text search
{
  "query": {
    "match": {
      "content": "elasticsearch tutorial"
    }
  }
}

// With operator
{
  "query": {
    "match": {
      "content": {
        "query": "elasticsearch tutorial",
        "operator": "and"
      }
    }
  }
}

Term Query

json
// Exact match
{
  "query": {
    "term": {
      "status": "published"
    }
  }
}

// Multiple terms
{
  "query": {
    "terms": {
      "tags": ["javascript", "elasticsearch"]
    }
  }
}

Range Query

json
{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 500
      }
    }
  }
}

// Date range
{
  "query": {
    "range": {
      "created_at": {
        "gte": "2025-01-01",
        "lte": "2025-12-31"
      }
    }
  }
}

Compound Queries

Bool Query

json
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "search" } }
      ],
      "should": [
        { "match": { "tags": "elasticsearch" } },
        { "match": { "tags": "lucene" } }
      ],
      "must_not": [
        { "term": { "status": "archived" } }
      ],
      "filter": [
        { "range": { "created_at": { "gte": "2025-01-01" } } }
      ]
    }
  }
}

Aggregations

Metrics Aggregation

json
{
  "size": 0,
  "aggs": {
    "avg_price": {
      "avg": { "field": "price" }
    },
    "stats": {
      "stats": { "field": "rating" }
    }
  }
}

Bucket Aggregation

json
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": { "field": "category.keyword" },
      "aggs": {
        "avg_rating": {
          "avg": { "field": "rating" }
        }
      }
    }
  }
}

Date Histogram

json
{
  "size": 0,
  "aggs": {
    "over_time": {
      "date_histogram": {
        "field": "created_at",
        "calendar_interval": "month"
      },
      "aggs": {
        "total_sales": {
          "sum": { "field": "price" }
        }
      }
    }
  }
}

Filters

Post Filter

json
{
  "query": {
    "match": { "content": "laptop" }
  },
  "post_filter": {
    "term": { "brand": "Apple" }
  },
  "aggs": {
    "all_brands": {
      "terms": { "field": "brand.keyword" }
    }
  }
}

Sorting

Multiple Sort

json
{
  "query": { "match_all": {} },
  "sort": [
    { "price": { "order": "asc" } },
    { "rating": { "order": "desc" } },
    "_score"
  ]
}

Pagination

From/Size

json
{
  "from": 0,
  "size": 10,
  "query": { "match_all": {} }
}

Search After

json
{
  "size": 10,
  "query": { "match_all": {} },
  "sort": [
    { "created_at": "desc" },
    "_id"
  ],
  "search_after": ["2025-01-01T00:00:00", "doc123"]
}

Best Practices

  1. 1Use filter instead of must for exact matches
  2. 2Limit result size with size parameter
  3. 3Use source filtering to return only needed fields
  4. 4Cache frequently used filters
  5. 5Optimize aggregations with size parameter

Conclusion

Elasticsearch Query DSL enables powerful search and analytics. Master queries, aggregations, and filters for production-ready search.

Build powerful search with Elasticsearch!

Enjoyed this essay? Share it.

Related essays

Next essay
Tech Interview Prep: Land Your Dream Job