Created
July 14, 2016 18:18
-
-
Save oivoodoo/845b857b28e24bc1acdc13c18e1b32d6 to your computer and use it in GitHub Desktop.
elixir elasticsearch example
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
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) |
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
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