Skip to content

Instantly share code, notes, and snippets.

@parallel588
Forked from joshnuss/elastic_search.ex
Created October 12, 2021 13:37
Show Gist options
  • Select an option

  • Save parallel588/89df08f79d550239497f7361a13e87cc to your computer and use it in GitHub Desktop.

Select an option

Save parallel588/89df08f79d550239497f7361a13e87cc to your computer and use it in GitHub Desktop.

Revisions

  1. @joshnuss joshnuss revised this gist Jan 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion elastic_search.ex
    Original 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, "_create", id], doc)
    request(:put, [index, id], doc)
    end

    def post(index, doc) do
  2. @joshnuss joshnuss revised this gist Jan 11, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions elastic_search.ex
    Original 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
  3. @joshnuss joshnuss revised this gist Jan 11, 2020. No changes.
  4. @joshnuss joshnuss revised this gist Jan 11, 2020. No changes.
  5. @joshnuss joshnuss renamed this gist Jan 11, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @joshnuss joshnuss revised this gist Jan 11, 2020. 2 changed files with 12 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion elastic_search.ex
    Original 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"], doc)
    request(:post, index, doc)
    end

    def get(index, id) do
    11 changes: 11 additions & 0 deletions usage.exs
    Original 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}
  7. @joshnuss joshnuss created this gist Jan 11, 2020.
    34 changes: 34 additions & 0 deletions docker-compose.yml
    Original 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

    50 changes: 50 additions & 0 deletions elastic_search.ex
    Original 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