0Pricing
Elasticsearch & Full Text Search Systems · 课时

嵌套字段与对象字段类型

学习 Elasticsearch 如何处理 JSON 对象和数组,了解默认对象类型为何会将数据扁平化,以及嵌套类型如何保留对象数组内部的关系。

嵌套字段与对象字段类型 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。

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

Storing Structured Data

Real-world documents often contain nested structures: a blog post with comments, a product with variants, or an order with line items. Elasticsearch must decide how to index these JSON objects so they stay searchable.

This lesson covers the two main approaches: the default object type and the specialized nested type.

The Default object Type

By default, any JSON object inside a document is mapped as the object type. Elasticsearch flattens the inner fields into dotted paths.

A field author.name simply becomes a normal Lucene field. This is efficient and works perfectly for single objects.

PUT my_index/_doc/1
{
  "author": { "first": "Jane", "last": "Doe" }
}

How Flattening Looks

Internally the object above is stored as two flat fields: author.first = Jane and author.last = Doe. The hierarchy is only conceptual; Lucene sees independent fields.

This is fine until you have an array of objects.

The Flattening Problem

Consider an array of users. After flattening, Elasticsearch loses the link between which first name belongs to which last name.

The arrays become user.first = [Alice, John] and user.last = [White, Smith] separately.

PUT my_index/_doc/2
{
  "user": [
    { "first": "Alice", "last": "White" },
    { "first": "John",  "last": "Smith" }
  ]
}

Why It Matters

A query for first = Alice AND last = Smith would incorrectly match the document above, because the cross-object relationship is gone. The values are pooled together.

The nested type solves this.

Declaring a Nested Field

Set the field type to nested in the mapping. Each object in the array is then indexed as a hidden, separate Lucene document, preserving its internal field relationships.

PUT my_index
{
  "mappings": {
    "properties": {
      "user": { "type": "nested" }
    }
  }
}

Querying Nested Fields

You must use a nested query and specify the path. Conditions inside are evaluated against a single sub-document, so cross-object false matches disappear.

GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": { "must": [
          { "match": { "user.first": "Alice" }},
          { "match": { "user.last":  "Smith" }}
        ]}
      }
    }
  }
}

Inner Hits

Add inner_hits to a nested query to return which specific sub-document(s) matched, not just the parent document. This is essential for highlighting the relevant array element.

"nested": {
  "path": "user",
  "inner_hits": {},
  "query": { "match": { "user.first": "Alice" } }
}

Costs of Nested

Nested fields are powerful but have trade-offs:

  • Each array element is a separate Lucene doc, increasing index size.
  • Updating one element re-indexes the whole parent document.
  • Deeply nested or large arrays can hurt performance.

Use the index.mapping.nested_objects.limit setting to cap counts.

Nested vs join

For tightly coupled data that updates together, nested is ideal. For independently updated, high-cardinality relationships, consider the join (parent-child) field type instead, which decouples updates at a higher query cost.

When to Choose Which

Use object when arrays do not require cross-field correlation. Use nested when you must match multiple fields within the same array element. Defaulting to nested for everything wastes resources.

Quick Check

Test your understanding of nested mappings.

Recap

You learned how Elasticsearch indexes JSON objects:

  • The default object type flattens fields and pools array values.
  • The nested type indexes each array element separately to preserve relationships.
  • Query nested fields with the nested query plus a path, and use inner_hits to find matching elements.
  • Nested types cost more storage and require full re-indexing on element updates.

常见问题解答

「嵌套字段与对象字段类型」课时是免费的吗?

是的 — 「嵌套字段与对象字段类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。

「嵌套字段与对象字段类型」这节课中我会学到什么?

学习 Elasticsearch 如何处理 JSON 对象和数组,了解默认对象类型为何会将数据扁平化,以及嵌套类型如何保留对象数组内部的关系。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elasticsearch & Full Text Search Systems 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「嵌套字段与对象字段类型」课时需要多长时间?

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

我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?

能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自定义字段映射
  2. 动态映射与显式映射
  3. 索引模板与别名
  4. 嵌套字段与对象字段类型
← 返回 Elasticsearch & Full Text Search Systems