Featured image of post Elasticsearch In A Nutshell

Elasticsearch In A Nutshell

Elasticsearch with Examples

Elastigirl

A Quick History of Elasticsearch

Elasticsearch is built on Apache Lucene, a powerful full-text search library. While Lucene is great, it’s also complicated—like trying to assemble IKEA furniture without a manual.

In 2010, Shay Banon created Elasticsearch to make Lucene more user-friendly. Instead of dealing with complex configurations, Elasticsearch lets you send simple JSON queries over HTTP. And boom! You get lightning-fast search results.

Fast forward to today, Elasticsearch is used by companies like Netflix, Uber, Slack, and Wikipedia for real-time search, log analysis, and more.

Why Use Elasticsearch?

Pros

  • Speed Demon – Handles millions of records in milliseconds.
  • Scalable – Distributed architecture makes it easy to scale.
  • Schema-less – Just throw JSON at it, and it figures things out.
  • Powerful Querying – Full-text search, aggregations, filters—it’s got it all.
  • RESTful API – Easy to use over HTTP with JSON.

Cons

  • Resource Hungry – Eats RAM and CPU like it’s at an all-you-can-eat buffet.
  • Cluster Complexity – Managing clusters can be challenging.
  • Requires Tuning – Defaults are okay, but real performance requires tweaking.
  • Not a Traditional Database – Not designed for transactional workloads.

10 Examples of Elasticsearch in Action

1. Installing Elasticsearch

1
docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.0.0

2. Indexing Data

1
2
3
4
5
6
curl -X POST "localhost:9200/movies/_doc/1" -H "Content-Type: application/json" -d'
{
  "title": "The Matrix",
  "year": 1999,
  "genre": ["Action", "Sci-Fi"]
}'

3. Searching for a Movie

1
curl -X GET "localhost:9200/movies/_search?q=title:Matrix&pretty"

4. Filtering Results

1
2
3
4
5
6
7
{
  "query": {
    "range": {
      "year": { "gte": 2000 }
    }
  }
}
1
2
3
4
5
6
7
8
9
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "Matrox"
      }
    }
  }
}

6. Aggregations

1
2
3
4
5
6
7
{
  "aggs": {
    "genres_count": {
      "terms": { "field": "genre.keyword" }
    }
  }
}

7. Updating a Document

1
2
3
4
curl -X POST "localhost:9200/movies/_update/1" -H "Content-Type: application/json" -d'
{
  "doc": { "year": 1998 }
}'

8. Deleting a Document

1
curl -X DELETE "localhost:9200/movies/_doc/1"

9. Bulk Indexing

1
2
3
4
5
6
curl -X POST "localhost:9200/movies/_bulk" -H "Content-Type: application/json" -d'
{ "index": { "_index": "movies", "_id": "2" }}
{ "title": "Inception", "year": 2010, "genre": ["Sci-Fi", "Thriller"] }
{ "index": { "_index": "movies", "_id": "3" }}
{ "title": "Interstellar", "year": 2014, "genre": ["Sci-Fi", "Drama"] }
'

10. Deleting an Index

1
curl -X DELETE "localhost:9200/movies"

Alternatives Compared

MethodProsCons
ElasticsearchFast, scalable, powerful searchResource-intensive, complex to manage
PostgreSQL Full-Text SearchBuilt into PostgreSQL, good for structured dataSlower for large datasets
MySQL Full-Text SearchSimple, easy to useLess flexible, limited features
Apache SolrAlso built on Lucene, highly customizableMore complex setup
AlgoliaExtremely fast, easy to useExpensive for large datasets

Conclusion

Elasticsearch is a powerful search engine that’s great for full-text search, log analysis, and big data applications.

But it’s not a one-size-fits-all solution. If you need transactional consistency or a simple setup, alternatives like PostgreSQL or MySQL full-text search might be a better fit.

If you love speed, scalability, and JSON, Elasticsearch is your best friend. Just keep an eye on your cluster—because Elasticsearch loves RAM more than you love coffee.

Key Ideas

TopicSummary
ElasticsearchA fast, scalable search engine based on Lucene
Pros & ConsFast and powerful but resource-hungry
10 ExamplesCovers installation, indexing, searching, and more
AlternativesCompared with PostgreSQL, MySQL, Solr, and Algolia

References