elasticsearch v0.1.0 Elasticsearch.Document protocol View Source

A protocol for converting a struct into an Elasticsearch document.

Example

defimpl Elasticsearch.Document, for: MyStruct do
  def id(struct), do: struct.id
  def type(_struct), do: "struct"
  def parent(_struct), do: false
  def encode(struct) do
    %{
      id: struct.id,
      name: struct.name
    }
  end
end

Link to this section Summary

Functions

Returns a map of fields, which will be converted to JSON and stored in Elasticsearch as a document

Returns the Elasticsearch _id for the item

Returns the parent ID of the document, or false if there is no parent

Returns the Elasticsearch _type for the object

Link to this section Types

Link to this section Functions

Link to this function encode(item) View Source
encode(any()) :: map()

Returns a map of fields, which will be converted to JSON and stored in Elasticsearch as a document.

Example

def encode(item) do
  %{
    title: item.title,
    author: item.author
  }
end
Link to this function id(item) View Source
id(any()) :: any()

Returns the Elasticsearch _id for the item.

Example

def id(item), do: item.id
Link to this function parent(item) View Source
parent(any()) :: false | any()

Returns the parent ID of the document, or false if there is no parent.

Examples

# For structs that have parents
def parent(%{parent_id: id}) when id != nil, do: id

# For structs that don't have parents
def parent(_item), do: false

Returns the Elasticsearch _type for the object.

Example

def type(_item), do: "item"