0Pricing
Elasticsearch & Full Text Search Systems · Lekcja

Typy pól nested i object

Dowiedzą się Państwo, jak Elasticsearch obsługuje obiekty i tablice JSON, dlaczego domyślny typ object spłaszcza dane oraz jak typ nested zachowuje relacje w tablicach obiektów.

Typy pól nested i object to bezpłatna lekcja Elasticsearch & Full Text Search Systems na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Elasticsearch & Full Text Search Systems, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Typy pól nested i object” jest bezpłatna?

Tak — pełny tekst „Typy pól nested i object” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Elasticsearch & Full Text Search Systems, przejdź na CoddyKit PRO. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.

Co nauczysz się w „Typy pól nested i object”?

Dowiedzą się Państwo, jak Elasticsearch obsługuje obiekty i tablice JSON, dlaczego domyślny typ object spłaszcza dane oraz jak typ nested zachowuje relacje w tablicach obiektów. Ćwiczysz Elasticsearch & Full Text Search Systems z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Elasticsearch & Full Text Search Systems?

Nie wymagamy żadnego doświadczenia. Elasticsearch & Full Text Search Systems w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Typy pól nested i object”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Elasticsearch & Full Text Search Systems?

Tak. Każda lekcja Elasticsearch & Full Text Search Systems zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dostosowywanie mapowań pól
  2. Mapowania dynamiczne a jawne
  3. Szablony indeksów i aliasy
  4. Typy pól nested i object
← Powrót do Elasticsearch & Full Text Search Systems