Skip to content

Commit

Permalink
Added ElasticsearchEx.Document struct
Browse files Browse the repository at this point in the history
  • Loading branch information
GRoguelon committed May 15, 2024
1 parent 1972846 commit 7297dad
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog


## v1.7.0

* **New features:**
* Added a module `ElasticsearchEx.Document` to host a document from Elasticsearch

## v1.6.0 (2024-05-15)

* **New features:**
Expand Down
58 changes: 58 additions & 0 deletions lib/elasticsearch_ex/document.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule ElasticsearchEx.Document do
@moduledoc """
Provides a struct to represent a document from Elasticsearch.
"""

defstruct [
:_id,
:_index,
:_primary_term,
:_seq_no,
:_source,
:_score,
:_version,
:found,
:result,
:_shards
]

## Typespecs

@type t :: %__MODULE__{
_id: nil | binary(),
_index: nil | binary(),
_primary_term: nil | pos_integer(),
_seq_no: nil | non_neg_integer(),
_source: map(),
_score: nil | float(),
_version: nil | pos_integer(),
found: nil | boolean(),
result: nil | binary(),
_shards: nil | %{required(atom() | binary()) => non_neg_integer()}
}

## Public functions

def new(attrs \\ %{})

@spec new([map()]) :: [t()]
def new(attrs_list) when is_list(attrs_list) do
Enum.map(attrs_list, &new/1)
end

@spec new(map()) :: t()
def new(attrs) when is_map(attrs) do
case Enum.at(attrs, 0) do
{key, _} when is_atom(key) ->
struct!(__MODULE__, attrs)

{key, _} when is_binary(key) ->
attrs = Map.new(attrs, fn {key, value} -> {String.to_existing_atom(key), value} end)

struct!(__MODULE__, attrs)

nil ->
__MODULE__.__struct__()
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ElasticsearchEx.MixProject do
use Mix.Project

@source_url "https://github.com/CoreCareinc/elasticsearch_ex"
@version "1.6.0"
@version "1.7.0"

def project do
[
Expand Down

0 comments on commit 7297dad

Please sign in to comment.