Dowser.Elasticsearch.TypeCodec (Dowser.Elasticsearch v0.1.0)

View Source

Whole-body type casting for Elasticsearch documents and search results — a Dowser.Client.Codec implementation, set as :codec_adapter (see Dowser.Client) to cast values (dates, IPs, ...) per field, based on each document's own index mapping:

Dowser.Client.Config.new(
  endpoint: "http://localhost:9200",
  codec_adapter: Dowser.Elasticsearch.TypeCodec
)

Once configured, every API function in Dowser.Elasticsearch.Document and Dowser.Elasticsearch.Search casts automatically — no per-call option needed. Field-level casting itself is delegated to Dowser.Elasticsearch.Codec; mappings are fetched (and cached) through Dowser.Elasticsearch.MappingCacher.

decode/2

Finds and casts every document in a response body, at any nesting depth: a bare document (Document.get/3), hits.hits[] (Search.search/2), responses[].hits.hits[] (Search.msearch/2), and so on — each hit's own _index selects its mapping, so mixed-index results (e.g. msearch/2 across different indices) are cast correctly. opts[:source], alongside opts[:index], casts a bare _source document with no _index of its own (Document.get_source/3).

If no mapping can be found for a document's index (no Dowser.Elasticsearch.MappingCacher running, or the fetch fails), its values pass through unchanged; keys are still cast per opts[:key_fn].

encode/2

Casts a request body against opts[:index]'s mapping — a no-op when opts[:index] is absent. opts[:doc_key] casts only that sub-key instead of the whole body (Document.update/4's %{doc: ...} shape). A list body is treated as a Document.bulk/2 NDJSON action list: index/create actions cast their whole payload, update actions cast only doc, and delete actions (which carry no payload) are left alone; a per-action _index overrides opts[:index].