0Pricing
Elasticsearch & Full Text Search Systems · レッスン

Nested型とObject型のフィールド

ElasticsearchがJSONオブジェクトと配列を扱う仕組み、デフォルトのobject型がデータをフラット化する理由、そしてnested型でオブジェクト配列内の関係を保持する方法を学びます。

「Nested型とObject型のフィールド」はCoddyKit上の無料Elasticsearch & Full Text Search Systemsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「Nested型とObject型のフィールド」レッスンは無料ですか?

はい。「Nested型とObject型のフィールド」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elasticsearch & Full Text Search Systemsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elasticsearch & Full Text Search Systemsコースには全4レッスンが含まれています。

「Nested型とObject型のフィールド」で何を学びますか?

ElasticsearchがJSONオブジェクトと配列を扱う仕組み、デフォルトのobject型がデータをフラット化する理由、そしてnested型でオブジェクト配列内の関係を保持する方法を学びます。 ブラウザで直接実行するハンズオンコードでElasticsearch & Full Text Search Systemsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Elasticsearch & Full Text Search Systemsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのElasticsearch & Full Text Search Systemsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Nested型とObject型のフィールド」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このElasticsearch & Full Text Search Systemsレッスンでコードを書いて実行できますか?

はい。すべてのElasticsearch & Full Text Search Systemsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. フィールドマッピングのカスタマイズ
  2. 動的マッピングと明示的マッピング
  3. インデックステンプレートとエイリアス
  4. Nested型とObject型のフィールド
← Elasticsearch & Full Text Search Systemsに戻る