Course contentsShow
← Node.js
Lesson 1672 of 1,98015. Logging, Observability, and MetricsFree lesson

Log Query Optimization

Understand how to index log fields in search backends to maximize retrieval performance.

In this lesson, you will learn how to strategically index log fields in your search backend to transform slow, full-text scans into sub-second retrieval operations.

As your Node.js application scales, Log Aggregation Patterns ensure all your data ends up in a central store like Elasticsearch or OpenSearch. However, simply pushing JSON logs isn't enough; if your search backend isn't configured to understand the structure of your logs, every query results in a "full-text scan," which is resource-intensive and painfully slow as your indices grow into the terabytes.

The Strategy: Explicit Field Mapping

When you use Contextual Metadata Injection to attach specific fields—like userId, correlationId, or serviceName—your log aggregator sees these as arbitrary text by default. To optimize retrieval, you must explicitly define these fields in your backend's index template.

By setting these fields as keyword types rather than text types, you instruct the database to store the exact value rather than creating an inverted index for every word in the string. This makes filtering operations instantaneous.

Example Scenario: Imagine you are searching for logs related to a specific user.

Instead of a broad query that scans every message, a configured index allows for a targeted filter:

// Instead of searching for the string "user_123" inside the full message
{
  "query": {
    "term": {
      "user_id": "user_123"
    }
  }
}

Choosing What to Index

You cannot index everything; excessive indexing increases storage costs and slows down write throughput. Focus on optimizing fields that appear in your correlation IDs or frequently used dashboard filters.

  1. High-Cardinality Fields: Fields like correlationId or orderId are essential for high-speed lookups and should always be indexed as keywords.
  2. Low-Cardinality Fields: Fields like logLevel (info, warn, error) are excellent candidates for indexing to allow for rapid dashboard aggregation.
  3. Avoid Indexing Blobs: Never index raw stack traces or massive request bodies; keep those as searchable-but-not-aggregatable text to save space.

By shifting from "searching for text" to "filtering on indexed fields," you ensure your observability tools remain performant even under heavy production load.

Key Takeaway: Optimize retrieval by explicitly defining high-cardinality metadata fields as indexed keywords in your search backend, transforming slow full-text scans into precise, high-speed data lookups.