Orkestra.ES.Schema.Casting (orkestra v0.2.0)

Copy Markdown View Source

Converts between an Orkestra.ES.Schema struct and its Elasticsearch document representation.

to_doc/4 turns a struct into a string-keyed map ready for indexing; from_hit/5 rebuilds the struct from an _source map. The two are inverse for valid values, so from_hit(to_doc(struct)) == struct.

Casting rules:

  • Date / DateTime values serialize to ISO8601 and are parsed back based on the runtime value (a string containing "T" is read as a DateTime, otherwise as a Date). A field declared with a custom format: keeps its raw string on the way back, since the library cannot interpret arbitrary ES date patterns.
  • nil values are preserved as null, keeping the round-trip faithful.
  • The facets slot is flattened on write (one entry per value) and regrouped on read (by attr_code, preserving the first-seen order and attr_name); read values always get count: nil.
  • Embeds recurse through the embedded schema's own to_doc/1 / from_hit/1 (multi-level embeds included): an embeds_one value of nil stays null; an embeds_many list maps element-wise, and a missing/null array reads back as [].

The module is pure and has no dependency on Snap.

Summary

Functions

Rebuilds a schema struct of module from an Elasticsearch _source map.

Converts a schema struct into a string-keyed Elasticsearch document.

Functions

from_hit(source, module, field_meta, facets_field, embeds_meta \\ [])

Rebuilds a schema struct of module from an Elasticsearch _source map.

to_doc(struct, field_meta, facets_field, embeds_meta \\ [])

Converts a schema struct into a string-keyed Elasticsearch document.