This repository was archived by the owner on Jun 2, 2025. It is now read-only.
forked from werbitzky/elastix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk.ex
More file actions
50 lines (41 loc) · 1.59 KB
/
bulk.ex
File metadata and controls
50 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
defmodule Elastix.Bulk do
@moduledoc """
"""
alias Elastix.HTTP
def post(elastic_url, lines, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(Enum.reduce(lines, "", fn (line, payload) -> payload <> Poison.encode!(line) <> "\n" end))
|> process_response
end
def post_to_iolist(elastic_url, lines, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(Enum.map(lines, fn line -> Poison.encode!(line) <> "\n" end))
|> process_response
end
@doc false
def post_raw(elastic_url, raw_data, options \\ [], query_params \\ []) do
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params)
|> HTTP.put(raw_data)
|> process_response
end
@doc false
def make_path(index_name, type_name, query_params) do
path = _make_base_path(index_name, type_name)
case query_params do
[] -> path
_ -> add_query_params(path, query_params)
end
end
defp _make_base_path(nil, nil), do: "/_bulk"
defp _make_base_path(index_name, nil), do: "/#{index_name}/_bulk"
defp _make_base_path(index_name, type_name), do: "/#{index_name}/#{type_name}/_bulk"
@doc false
defp add_query_params(path, query_params) do
query_string = Enum.map_join query_params, "&", fn(param) ->
"#{elem(param, 0)}=#{elem(param, 1)}"
end
"#{path}?#{query_string}"
end
@doc false
defp process_response({_, response}), do: response
end