Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created July 14, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oivoodoo/845b857b28e24bc1acdc13c18e1b32d6 to your computer and use it in GitHub Desktop.
Save oivoodoo/845b857b28e24bc1acdc13c18e1b32d6 to your computer and use it in GitHub Desktop.
elixir elasticsearch example
env = Application.get_env(:requesters, :environment)
if env == "prod" do
env = "production"
end
index = [index: "timelines-#{env}", type: "Timeline"]
use Tirexs.Mapping
mappings do
indexes "name", type: "string"
indexes "artist", type: "string"
indexes "description", type: "string"
end
Tirexs.Mapping.create_resource(index)
defmodule Aggregator.Workers.StorageWorker.ElasticServer do
use GenServer
require Logger
defmodule Indexer do
import Tirexs.Bulk
def run(timeline) do
env = Application.get_env(:requesters, :environment)
if env == "prod" do
env = "production"
end
payload = bulk([index: "timelines-#{env}", type: "Timeline"]) do
index [
[
id: timeline.id,
name: timeline.name,
artist: timeline.artist,
description: timeline.description,
]
]
end
Tirexs.bump!(payload)._bulk
end
end
def start_link do
GenServer.start_link(__MODULE__, [], [name: __MODULE__])
end
def init(_opts) do
{:ok, nil}
end
def index(timeline) do
GenServer.cast(__MODULE__, {:index, timeline})
end
def handle_cast({:index, timeline}, state) do
Indexer.run(timeline)
{:noreply, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment