View Source ExlasticSearch.Response (exlasticsearch v2.3.0)

Base module for ES response parsing.

Works off a few macros, schema/1, field/1, has_many/2, has_one/2.

The usage is more or less:

use ExlasticSearch.Response

schema do
  field :total

  has_many :hits, HitsModule
end

This will define:

  • a struct for carrying the response
  • parse/2 - converts a json decoded map from ES to the given response struct, and converting any models appropriately
  • to_model/2 - performs model conversion if possible (defaults to no-op)

Summary

Functions

Safe conversion of string keyed ES response maps to structifiable atom keyed maps.

Adds a simple field attribute.

Adds a has_many relation or the parser, which assumes a list value.

Adds a has_one relation or the parser.

Utility for recursively parsing response associations.

Opens up the schema definition macro. Once closed, the following will be defined

Functions

convert_keys(conversion_table, map)

Safe conversion of string keyed ES response maps to structifiable atom keyed maps.

field(field)

(macro)

Adds a simple field attribute.

has_many(field, parser)

(macro)

Adds a has_many relation or the parser, which assumes a list value.

Accepts:

  • field - the name of the relation
  • parser - module of the responsible parser for parsing it

has_one(field, parser)

(macro)

Adds a has_one relation or the parser.

Accepts:

  • field - the name of the relation
  • parser - module of the responsible parser for parsing it

parse_associations(response, associations, model, index_type)

Utility for recursively parsing response associations.

schema(block)

(macro)

Opens up the schema definition macro. Once closed, the following will be defined:

  • __schema__(:parse_spec) - A table for converting string keyed maps to atom keyed
  • __schema__(:attributes) - basic field attributes
  • __schema__(:associations) - a table of associations for the response, along with the responsible parser