-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement map-based mappings configuration
- Loading branch information
Sebastián González
committed
Jul 31, 2018
1 parent
c378e36
commit 8a9223c
Showing
9 changed files
with
167 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
defmodule Graveyard.Errors do | ||
defmodule WrongConfigModuleError do | ||
defmodule ConfigModuleError do | ||
defexception message: "Missing function" | ||
|
||
def full_message(error) do | ||
"Supplied module has no get_mappings/2 function" | ||
end | ||
end | ||
|
||
defmodule NoElasticSearchInstance do | ||
defmodule ElasticSearchInstanceError do | ||
defexception message: "No ElasticSearch found" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Graveyard.Mappings.Basic do | ||
@moduledoc """ | ||
Builds a mapping object from a user-supplied module | ||
""" | ||
alias Graveyard.Support | ||
alias Graveyard.Errors | ||
|
||
def get_mappings(index_name, type_name) do | ||
module = Support.mappings_module | ||
try do | ||
module.get_mappings(index_name, type_name) | ||
rescue | ||
e in UndefinedFunctionError -> | ||
raise Errors.ConfigModuleError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,49 @@ | ||
defmodule Graveyard.Mappings.Builder do | ||
|
||
@moduledoc """ | ||
Builds a mapping object from a user-supplied map schema using the Graveyard Mapping DSL | ||
""" | ||
|
||
alias Graveyard.Support | ||
alias Graveyard.Errors | ||
alias Graveyard.Utils | ||
|
||
def get_mappings(index_name, type_name) do | ||
build_recursively(Support.mappings) | ||
end | ||
|
||
defp build_recursively(config) do | ||
properties = Enum.map(config, fn({key, value}) -> | ||
if Map.has_key?(value, "schema") do | ||
{ | ||
Utils.to_indifferent_atom(key), | ||
Utils.to_keyword_list(graveyard_to_elastic(value["type"])) ++ build_recursively(value["schema"]) | ||
} | ||
else | ||
{Utils.to_indifferent_atom(key), graveyard_to_elastic(value["type"])} | ||
end | ||
end) | ||
[mapping: [properties: properties], index: Support.index(), type: Support.type()] | ||
end | ||
|
||
def timestamps() do | ||
Utils.to_keyword_list(%{ | ||
created_at: graveyard_to_elastic(:datetime), | ||
updated_at: graveyard_to_elastic(:datetime) | ||
}) | ||
end | ||
|
||
defp graveyard_to_elastic(type) do | ||
case type do | ||
:string -> %{type: "keyword"} | ||
:category -> %{type: "keyword"} | ||
:list -> %{type: "keyword"} | ||
:text -> %{type: "text", analyzer: "nGram_analyzer"} | ||
:date -> %{type: "date", format: "dd/MM/yyyy"} | ||
:datetime -> %{type: "date", format: "dd/MM/yyyy HH:mm:ss"} | ||
:integer -> %{type: "integer"} | ||
:number -> %{type: "float"} | ||
:object -> %{type: "object"} | ||
:oblist -> %{type: "nested"} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters