0Pricing
System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) · 课时

Elasticsearch 查询语言(DSL)

深入学习功能强大的 Elasticsearch 查询 DSL,处理复杂的数据检索与聚合。学习编写高级搜索查询。

Elasticsearch 查询语言(DSL) 是 CoddyKit 上的免费 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Unlocking Elasticsearch Query Power

Welcome to the world of Elasticsearch Query DSL! DSL stands for Domain Specific Language. It's the powerful, flexible way to search and analyze data in Elasticsearch.

Instead of simple keywords, DSL lets you build complex queries using a JSON-based structure. This gives you fine-grained control over how your data is found and processed.

Basic Text Search: Match Query

The match query is your go-to for full-text searches. It analyzes the search text and the field content, making it great for finding relevant documents even with slight variations.

Here's how to find products containing the word 'laptop':

{
  "query": {
    "match": {
      "product_name": "laptop"
    }
  }
}

Exact Phrase Search: Match Phrase

Sometimes you need to find an exact sequence of words. That's where the match_phrase query comes in handy. It ensures all words in your query appear in the field, in the specified order.

Let's search for the exact phrase 'high performance' in a product description:

{
  "query": {
    "match_phrase": {
      "description": "high performance"
    }
  }
}

Exact Value Search: Term Query

The term query is used for finding exact values in fields that are not analyzed, like keywords, numbers, or dates. It won't break down your search term into individual words.

This is perfect for filtering by specific IDs, statuses, or categories. Note the .keyword suffix, often used for exact string matching:

{
  "query": {
    "term": {
      "status.keyword": "active"
    }
  }
}

Filtering by Range: Range Query

Need to find documents within a specific numerical or date range? The range query is what you need. It supports operators like gte (greater than or equal), gt (greater than), lte (less than or equal), and lt (less than).

Find products priced between $100 and $500:

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

Combining Queries: Boolean Logic

The bool query is the most powerful way to combine multiple queries using boolean logic:

  • must: All queries must match.
  • should: At least one query should match (influences relevance score).
  • must_not: Queries must not match.
  • filter: Queries must match, but don't affect the relevance score (good for caching).

Boolean Query in Action

Let's find 'electronics' products priced under $1000, but specifically exclude any from the 'obsolete' brand. Notice how filter is used for the price range, as it doesn't need to contribute to the score.

{
  "query": {
    "bool": {
      "must": [
        { "match": { "category": "electronics" } }
      ],
      "filter": [
        { "range": { "price": { "lte": 1000 } } }
      ],
      "must_not": [
        { "match": { "brand": "obsolete" } }
      ]
    }
  }
}

Beyond Search: Aggregations

Elasticsearch DSL isn't just for searching; it's also for powerful analytics using aggregations. Aggregations allow you to group your data, calculate metrics, and gain insights from large datasets.

Think of them like the GROUP BY clause in SQL, but much more flexible and efficient for large-scale data.

Grouping Data: Terms Aggregation

The terms aggregation is used to group documents by the values of a specific field, similar to facets. It's great for understanding the distribution of data, like finding the most popular categories or brands.

Here's how to get the top 5 product categories:

{
  "aggs": {
    "top_categories": {
      "terms": {
        "field": "category.keyword",
        "size": 5
      }
    }
  }
}

Calculating Metrics: Avg Aggregation

Metric aggregations compute statistics over numeric fields. Common examples include avg, sum, min, max, and count.

You can combine them with terms aggregations to get statistics per group. Let's find the average price for each product category:

{
  "aggs": {
    "avg_price_by_category": {
      "terms": {
        "field": "category.keyword"
      },
      "aggs": {
        "average_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

Quick Check on Queries

You want to find all documents where the status field is exactly 'pending' AND the priority is 'high'. Which combination of queries would you primarily use?

Recap: DSL's Power Unleashed

Congratulations! You've dived into the powerful world of Elasticsearch Query DSL.

  • You learned how to use match and match_phrase for text searching.
  • You explored term for exact value lookups and range for filtering.
  • You mastered combining queries with the flexible bool query.
  • And you got an introduction to aggregations like terms and avg for deep data analysis.

Keep exploring the DSL to unlock even more insights from your data!

常见问题解答

「Elasticsearch 查询语言(DSL)」课时是免费的吗?

是的 — 「Elasticsearch 查询语言(DSL)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程的其余内容,请升级到 CoddyKit PRO。 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程共包含 4 节课。

「Elasticsearch 查询语言(DSL)」这节课中我会学到什么?

深入学习功能强大的 Elasticsearch 查询 DSL,处理复杂的数据检索与聚合。学习编写高级搜索查询。 你通过在浏览器中直接运行的动手代码来练习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Elasticsearch 查询语言(DSL)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课中编写并运行代码吗?

能。每节 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Elasticsearch 查询语言(DSL)
  2. Logstash 过滤器与管道
  3. Kibana Discover 与 Lens
  4. 索引生命周期管理(ILM)
← 返回 System Observability: Logging, Metrics & Tracing (ELK + OpenTelemetry)