-
-
Save parallel588/89df08f79d550239497f7361a13e87cc to your computer and use it in GitHub Desktop.
Revisions
-
joshnuss revised this gist
Jan 11, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ defmodule ElasticSearch do ] def put(index, id, doc) do request(:put, [index, id], doc) end def post(index, doc) do -
joshnuss revised this gist
Jan 11, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Mojito: https://github.com/appcues/mojito/ # Mint: https://github.com/elixir-mint/mint defmodule ElasticSearch do @config Application.get_env(:my_app, ElasticSearch) @root @config[:root] # ie http://localhost:9200 -
joshnuss revised this gist
Jan 11, 2020 . No changes.There are no files selected for viewing
-
joshnuss revised this gist
Jan 11, 2020 . No changes.There are no files selected for viewing
-
joshnuss renamed this gist
Jan 11, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joshnuss revised this gist
Jan 11, 2020 . 2 changed files with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ defmodule ElasticSearch do end def post(index, doc) do request(:post, index, doc) end def get(index, id) do This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ {:ok, id} = ElasticSearch.post("seinfeld/characters", %{ name: "Newman", occupation: "Postman" }) |> IO.inspect #> {:ok, "GdF7lW8BURdlkPkYij2g"} ElasticSearch.get("seinfeld/characters", id) |> IO.inspect #> {:ok, %{"name" => "Newman", "occupation" => "Postman"}} ElasticSearch.delete("seinfeld/characters", id) |> IO.inspect #> {:ok, :deleted} -
joshnuss created this gist
Jan 11, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ version: '2.1' services: elasticsearch: image: elasticsearch:7.4.2 volumes: - es_data:/usr/share/elasticsearch/data:rw environment: - discovery.type=single-node ports: - 9200:9200 - 9300:9300 networks: - es kibana: image: kibana:7.4.2 ports: - 5601:5601 environment: SERVER_NAME: kibana.example.org ELASTICSEARCH_URL: "http://elasticsearch:9200" networks: - es depends_on: - elasticsearch networks: es: driver: bridge volumes: es_data: driver: local
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ defmodule ElasticSearch do @config Application.get_env(:my_app, ElasticSearch) @root @config[:root] # ie http://localhost:9200 @headers [ {"content-type", "application/json"} ] def put(index, id, doc) do request(:put, [index, "_create", id], doc) end def post(index, doc) do request(:post, [index, "_doc"], doc) end def get(index, id) do request(:get, [index, id, "_source"]) end def delete(index, id) do request(:delete, [index, id]) end defp request(method, path, doc \\ %{}) do path = path |> List.wrap() |> Enum.join("/") url = "#{@root}/#{path}" body = Jason.encode!(doc) case Mojito.request(method, url, @headers, body) do {:ok, %Mojito.Response{status_code: 200, body: payload}} -> if method == :delete do {:ok, :deleted} else {:ok, Jason.decode!(payload)} end {:ok, %Mojito.Response{status_code: 201, body: payload}} -> id = payload |> Jason.decode!() |> Map.fetch!("_id") {:ok, id} {:ok, %Mojito.Response{status_code: 409}} -> {:error, :version_conflict} {:ok, %Mojito.Response{status_code: 404}} -> {:error, :not_found} other -> other end end end