-
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.
Merge pull request #1 from sebastialonso/dev
v4.0
- Loading branch information
Showing
30 changed files
with
962 additions
and
252 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
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,17 @@ | ||
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 | ||
defexception message: "No ElasticSearch found" | ||
defmodule ElasticSearchInstanceError do | ||
defexception message: "No ElasticSearch instance found" | ||
end | ||
|
||
defmodule BadArgumentError do | ||
defexception message: "Illegal arguments were supplied" | ||
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,63 @@ | ||
defmodule Graveyard.Mappings.Auxiliar do | ||
|
||
alias Graveyard.Support | ||
import Graveyard.Utils | ||
|
||
@doc """ | ||
Traverses recursively the configured mappings map for :oblists objects. | ||
For every oblist found, the :category and :list fields inside are extracted and turn into an object. | ||
Then a special auxiliar field with this object is added to the mappings. | ||
This is done to make easier the grouping process under nested fields | ||
""" | ||
def build_auxiliar_mappings(config \\ Support.mappings) do | ||
grouped_properties = find_fields_with_schema(config) | ||
|> Enum.group_by(fn(x) -> | ||
x.nested_key | ||
end) | ||
|
||
properties = Enum.map(grouped_properties, fn({key, val}) -> | ||
inner_properties = Enum.reduce(val, [], fn(x, acc) -> | ||
acc ++ [{to_indifferent_atom(x.name), [type: "keyword"]}] | ||
end) | ||
{to_indifferent_atom(key), [properties: inner_properties, type: "object"]} | ||
end) | ||
[__aux: [properties: properties, type: "object"]] | ||
end | ||
|
||
def find_fields_with_schema(mmap, parent_key \\ "", accumulator \\ []) do | ||
mmap |> Enum.reduce(accumulator, fn({key, val}, acc) -> | ||
if is_map(val) do | ||
if Map.has_key?(val, "schema") do | ||
parent_key = if parent_key == "" do | ||
key | ||
else | ||
Enum.join([parent_key, key], ".") | ||
end | ||
case val["type"] do | ||
:object -> | ||
find_fields_with_schema(val["schema"], parent_key, acc) | ||
:oblist -> | ||
extract_category_list_keys(key, val["schema"], acc) | ||
_ -> acc | ||
end | ||
else | ||
acc | ||
end | ||
else | ||
acc | ||
end | ||
end) | ||
end | ||
|
||
def extract_category_list_keys(key, schema, accumulator) do | ||
schema |> Enum.reduce(accumulator, fn({kkey, value}, acc) -> | ||
cond do | ||
Map.has_key?(value, "schema") -> | ||
find_fields_with_schema(%{kkey => value}, key, acc) | ||
Enum.member?([:category, :list], value["type"]) -> | ||
acc ++ [%{nested_key: to_indifferent_atom(key), name: to_indifferent_atom(kkey)}] | ||
true -> acc | ||
end | ||
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 |
---|---|---|
@@ -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.Utils | ||
|
||
def get_mappings(index_name, type_name) do | ||
[index: index_name, type: type_name, | ||
mapping: 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) | ||
}] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Graveyard.Maquiladoras.Find do | ||
def maquilate(result) do | ||
result | ||
|> Map.get(:_source) | ||
|> Map.put(:id, result._id) | ||
|> Map.delete(:__aux) | ||
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule Graveyard.Maquiladoras.Group do | ||
def maquilate(results) do | ||
case results do | ||
{:ok, 200, %{aggregations: aggs}} -> | ||
maquilate_recursively(aggs) | ||
|> List.flatten() | ||
{:error, status, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp maquilate_recursively(aggs, parent_bucket \\ %{}) do | ||
do_we_recurse = Map.has_key?(aggs, :aggregation) and Map.has_key?(aggs[:aggregation], :buckets) | ||
case do_we_recurse do | ||
false -> Map.put(parent_bucket, :data, process_leaf(aggs)) | ||
true -> | ||
Enum.map(aggs[:aggregation][:buckets], fn(bucket) -> | ||
parent_bucket = if Map.has_key?(parent_bucket, :source) do | ||
new_source = Map.get(parent_bucket, :source) ++ [%{field_name: aggs[:aggregation][:meta][:field_name], value: bucket[:key]}] | ||
Map.put(parent_bucket, :source, new_source) | ||
else | ||
Map.put(parent_bucket, :source, [%{field_name: aggs[:aggregation][:meta][:field_name], value: bucket[:key]}]) | ||
end | ||
maquilate_recursively(bucket, parent_bucket) | ||
end) | ||
end | ||
end | ||
|
||
defp process_leaf(leaf) do | ||
leaf | ||
|> Map.drop([:from, :to, :key, :doc_count, :key_as_string]) | ||
|> clean_leaf_values | ||
end | ||
|
||
defp clean_leaf_values(leaf) do | ||
Enum.map(leaf, fn({key, val}) -> | ||
if is_map(val) do | ||
{key, Map.get(val, :value)} | ||
end | ||
end) | ||
|> Enum.into(%{}) | ||
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
Oops, something went wrong.