liveBook · Manning

Object data type

As explained in Elasticsearch in Action, Second Edition

The Elasticsearch object data type represents an inner JSON object whose properties are mapped as fields within a document.

Use the object data type to model hierarchical JSON, such as an employee address or an email with attachments. An inner object contains its own properties, which Elasticsearch maps as fields within the document. section 4.3.1 section 4.6.2

Keep these behaviors in mind:

  • Define inner fields inside a properties object; Elasticsearch infers the object type for inner objects by default, so address or attachments need not declare type: object. section 4.3.1 section 4.6.2
  • Search inner fields with dotted paths such as attachments.filename. Use a match query for analyzed text or a term query on a keyword field for exact values. section 4.6.2
  • Remember that object arrays are flattened rather than stored as individual documents, so relationships between fields inside separate array elements are lost. section 4.6.2

Map and search email attachments

Create an emails index with attachments.filename and attachments.filetype as inner text fields. Index an email containing {"attachments":{"filename":"file1.txt","filetype":"confidential"}}, then search with {"query":{"match":{"attachments.filename":"file1.txt"}}}. The dotted path addresses the inner field, while the match query finds the indexed filename.

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines nested properties, dotted-field searches, and flattening limits, but stops short of preserving relationships between array elements.
Examples
Uses employee addresses and email attachments, including single and multiple attachment documents and their REST searches.
What it assumes
Builds from JSON objects, document fields, and basic mapping concepts while explaining inner properties from the beginning.
What you can do after
Apply it via Elasticsearch REST APIs to map, index, retrieve, and search hierarchical document fields.
How it connects
Connects JSON objects with mappings, properties, text fields, keyword fields, and match or term queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.