-
-
Save parallel588/89df08f79d550239497f7361a13e87cc to your computer and use it in GitHub Desktop.
ElasticSearch Elixir client using Mojito/Mint ๐น
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 characters
| # 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 | |
| @headers [ | |
| {"content-type", "application/json"} | |
| ] | |
| def put(index, id, doc) do | |
| request(:put, [index, id], doc) | |
| end | |
| def post(index, doc) do | |
| request(:post, index, 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 |
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 characters
| {: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} |
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 characters
| 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment