Back to blog
February 21, 2024
4 min read

Elasticsearch: Beyond Just Search

A practical guide to understanding Elasticsearch: when to use it, how it works, and common patterns for real-world applications.

Elasticsearch: Beyond Just Search

While “search” is in the name, Elasticsearch is much more than a search engine. Let’s break down what it is, when to use it, and how to get started.

What is Elasticsearch?

At its core, Elasticsearch is a distributed, RESTful analytics engine. Think of it like a very fast, document-oriented database that excels at:

  • Full-text search
  • Log analysis
  • Metrics aggregation
  • Application monitoring

When to Use Elasticsearch

  1. Complex Search Requirements

    • Need fuzzy matching
    • Multiple language support
    • Faceted search
    • Geospatial queries
  2. Analytics and Metrics

    • Log analysis
    • Performance monitoring
    • Business intelligence
    • Real-time dashboards
  3. Large-Scale Data

    • Billions of documents
    • Terabytes of data
    • Need for real-time results

Key Concepts

Documents and Indices

Index (like a database)
├── Document 1 (like a row)
│   ├── Field 1: value
│   ├── Field 2: value
│   └── Field 3: value
├── Document 2
│   ├── Field 1: value
│   └── Field 2: value
└── Document 3
    └── Field 1: value

Basic Operations

Index a Document

// Using the @elastic/elasticsearch client
const document = {
  title: 'Understanding Elasticsearch',
  tags: ['search', 'database', 'tutorial'],
  views: 1000,
  published: true,
};

await client.index({
  index: 'blog-posts',
  document,
});

Search Documents

// Basic search
const result = await client.search({
  index: 'blog-posts',
  query: {
    match: {
      title: 'elasticsearch tutorial',
    },
  },
});

// With filters
const result = await client.search({
  index: 'blog-posts',
  query: {
    bool: {
      must: [{ match: { title: 'elasticsearch' } }],
      filter: [{ term: { published: true } }, { range: { views: { gte: 100 } } }],
    },
  },
});

Common Patterns

1. Search-as-You-Type

// Create an index with autocomplete
await client.indices.create({
  index: 'products',
  body: {
    mappings: {
      properties: {
        name: {
          type: 'text',
          fields: {
            suggest: {
              type: 'completion',
            },
          },
        },
      },
    },
  },
});

// Use it
const suggestions = await client.search({
  index: 'products',
  suggest: {
    product_suggestions: {
      prefix: 'ela', // User input
      completion: {
        field: 'name.suggest',
      },
    },
  },
});
// Get counts by category
const result = await client.search({
  index: 'products',
  aggs: {
    categories: {
      terms: { field: 'category.keyword' },
    },
    avg_price: {
      avg: { field: 'price' },
    },
  },
});
// Find stores near a location
const result = await client.search({
  index: 'stores',
  query: {
    geo_distance: {
      distance: '10km',
      location: {
        lat: 40.7128,
        lon: -74.006,
      },
    },
  },
});

Best Practices

  1. Index Design

    • One index per use case
    • Use aliases for zero-downtime reindexing
    • Plan your mappings carefully
  2. Performance

    • Use filters before queries
    • Limit field indexing
    • Use scroll API for large result sets
  3. Reliability

    • Always use multiple nodes
    • Regular backups
    • Monitor cluster health

Common Pitfalls

  1. Memory Usage

    DON'T: Store everything as keyword
    ┌────────────────┐
    │ field: keyword │ → Huge memory usage
    └────────────────┘
    
    DO: Use text with sub-fields
    ┌───────────────────────┐
    │ field: text          │
    │   └─ keyword: subset │ → Efficient
    └───────────────────────┘
    
  2. Query Design

    • Avoid deep pagination
    • Don’t use scripts in high-volume queries
    • Be careful with wildcard queries
  3. Data Modeling

    • Denormalize when it makes sense
    • But don’t go overboard with nested objects

Bottom Line

  1. Use Elasticsearch when:

    • Complex search is crucial
    • You need real-time analytics
    • Traditional DBs struggle with scale
  2. Don’t use Elasticsearch when:

    • ACID compliance is required
    • You need complex transactions
    • Your data is highly relational

Remember: Elasticsearch excels at search and analytics but isn’t a replacement for your primary database. Use it alongside your existing data store for best results.