Dowser.Elasticsearch.Codec (Dowser.Elasticsearch v0.1.0)

View Source

The built-in field codec: dispatches a value to the Dowser.Client.Field module matching its mapping entry, built with Dowser.Client.CodecBuilder.

load/2 and dump/2 receive a single field value and its mapping entry (%{"type" => ..., "format" => ...}) and return the cast value directly. Only the field types JSON can't natively represent are cast; any other mapping entry — and nil values — fall back to identity.

mapping typefieldElixir term
date, date_nanosDowser.Elasticsearch.Fields.DateDateTime
date_rangeDowser.Elasticsearch.Fields.DateRangeDate.Range
integer_rangeDowser.Elasticsearch.Fields.RangeRange
ipDowser.Elasticsearch.Fields.IP:inet tuple
binaryDowser.Elasticsearch.Fields.Binaryraw binary
geo_pointDowser.Elasticsearch.Fields.GeoPoint{lat, lon}

Whole documents are cast against an index mapping by Dowser.Elasticsearch.TypeCodec, which walks a document via the Dowser.Elasticsearch.Mappable protocol and dispatches each field's value to this module's load/2/dump/2.

Custom codecs

Build your own codec to add field types, inheriting the built-in casts:

defmodule MyApp.Codec do
  use Dowser.Client.CodecBuilder, inherit: Dowser.Elasticsearch.Codec

  cast %{"type" => "scaled_float"}, MyApp.Fields.ScaledFloat
end

Inherited casts are matched first, so to replace a built-in cast (e.g. handle a custom date format), declare every cast yourself instead of inheriting.

Summary

Functions

dump(value, field)

Callback implementation for Dowser.Client.Field.dump/2.

load(value, field)

Callback implementation for Dowser.Client.Field.load/2.