Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 0 additions & 143 deletions .credo.exs

This file was deleted.

3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Used by "mix format"
[
inputs: ["{mix,.credo,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 90
line_length: 120,
plugins: [Styler]
]
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ jobs:
elixir-version: ${{matrix.elixir}}
- run: mix deps.get
- run: mix compile --warnings-as-errors
- run: mix test
- name: Check formatting
run: mix format --check-formatted
- name: Run credo
run: mix credo --strict
- name: Run tests
run: mix test
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.18.2-otp-27
erlang 27.2.3
10 changes: 7 additions & 3 deletions lib/elastix/alias.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ defmodule Elastix.Alias do
[Aliases documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html)
"""
import Elastix.HTTP, only: [prepare_url: 2]
alias Elastix.{HTTP, JSON}

alias Elastix.HTTP
alias Elastix.JSON

@doc """
Excepts a list of actions for the `actions` parameter.
Expand All @@ -17,7 +19,8 @@ defmodule Elastix.Alias do
"""
@spec post(elastic_url :: String.t(), actions :: list) :: HTTP.resp()
def post(elastic_url, actions) do
prepare_url(elastic_url, ["_aliases"])
elastic_url
|> prepare_url(["_aliases"])
|> HTTP.post(JSON.encode!(%{actions: actions}))
end

Expand All @@ -30,7 +33,8 @@ defmodule Elastix.Alias do
"""
@spec get(elastic_url :: String.t(), name :: String.t()) :: HTTP.resp()
def get(elastic_url, name \\ "") do
prepare_url(elastic_url, ["_alias", name])
elastic_url
|> prepare_url(["_alias", name])
|> HTTP.get()
end
end
51 changes: 21 additions & 30 deletions lib/elastix/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ defmodule Elastix.Bulk do
[Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html)
"""
import Elastix.HTTP, only: [prepare_url: 2]
alias Elastix.{HTTP, JSON}

alias Elastix.HTTP
alias Elastix.JSON

@doc """
Excepts a list of actions and sources for the `lines` parameter.
Expand All @@ -15,20 +17,17 @@ defmodule Elastix.Bulk do
iex> Elastix.Bulk.post("http://localhost:9200", [%{index: %{_id: "1"}}, %{user: "kimchy"}], index: "twitter", type: "tweet")
{:ok, %HTTPoison.Response{...}}
"""
@spec post(
elastic_url :: String.t(),
lines :: list,
opts :: Keyword.t(),
query_params :: Keyword.t()
) :: HTTP.resp()
@spec post(elastic_url :: String.t(), lines :: list, opts :: Keyword.t(), query_params :: Keyword.t()) :: HTTP.resp()
def post(elastic_url, lines, options \\ [], query_params \\ []) do
data =
Enum.reduce(lines, [], fn l, acc -> ["\n", JSON.encode!(l) | acc] end)
lines
|> Enum.reduce([], fn l, acc -> ["\n", JSON.encode!(l) | acc] end)
|> Enum.reverse()
|> IO.iodata_to_binary()

path =
Keyword.get(options, :index)
options
|> Keyword.get(:index)
|> make_path(Keyword.get(options, :type), query_params)

httpoison_options = Keyword.get(options, :httpoison_options, [])
Expand All @@ -41,22 +40,15 @@ defmodule Elastix.Bulk do
@doc """
Deprecated: use `post/4` instead.
"""
@spec post_to_iolist(
elastic_url :: String.t(),
lines :: list,
opts :: Keyword.t(),
query_params :: Keyword.t()
) :: HTTP.resp()
@spec post_to_iolist(elastic_url :: String.t(), lines :: list, opts :: Keyword.t(), query_params :: Keyword.t()) ::
HTTP.resp()
def post_to_iolist(elastic_url, lines, options \\ [], query_params \\ []) do
IO.warn(
"This function is deprecated and will be removed in future releases; use Elastix.Bulk.post/4 instead."
)
IO.warn("This function is deprecated and will be removed in future releases; use Elastix.Bulk.post/4 instead.")

httpoison_options = Keyword.get(options, :httpoison_options, [])

(elastic_url <>
make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params))
|> HTTP.put(
HTTP.put(
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params),
Enum.map(lines, fn line -> JSON.encode!(line) <> "\n" end),
[],
httpoison_options
Expand All @@ -67,18 +59,17 @@ defmodule Elastix.Bulk do
Same as `post/4` but instead of sending a list of maps you must send raw binary data in
the format described in the [Elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html).
"""
@spec post_raw(
elastic_url :: String.t(),
raw_data :: String.t(),
opts :: Keyword.t(),
query_params :: Keyword.t()
) :: HTTP.resp()
@spec post_raw(elastic_url :: String.t(), raw_data :: String.t(), opts :: Keyword.t(), query_params :: Keyword.t()) ::
HTTP.resp()
def post_raw(elastic_url, raw_data, options \\ [], query_params \\ []) do
httpoison_options = Keyword.get(options, :httpoison_options, [])

(elastic_url <>
make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params))
|> HTTP.put(raw_data, [], httpoison_options)
HTTP.put(
elastic_url <> make_path(Keyword.get(options, :index), Keyword.get(options, :type), query_params),
raw_data,
[],
httpoison_options
)
end

@doc false
Expand Down
28 changes: 17 additions & 11 deletions lib/elastix/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ defmodule Elastix.Document do
[Elastic documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html)
"""
import Elastix.HTTP, only: [prepare_url: 2]
alias Elastix.{HTTP, JSON}

alias Elastix.HTTP
alias Elastix.JSON

@doc """
(Re)Indexes a document with the given `id`.
Expand All @@ -24,7 +26,8 @@ defmodule Elastix.Document do
query_params :: Keyword.t()
) :: HTTP.resp()
def index(elastic_url, index_name, type_name, id, data, query_params \\ []) do
prepare_url(elastic_url, make_path(index_name, type_name, query_params, id))
elastic_url
|> prepare_url(make_path(index_name, type_name, query_params, id))
|> HTTP.put(JSON.encode!(data))
end

Expand All @@ -44,7 +47,8 @@ defmodule Elastix.Document do
query_params :: Keyword.t()
) :: HTTP.resp()
def index_new(elastic_url, index_name, type_name, data, query_params \\ []) do
prepare_url(elastic_url, make_path(index_name, type_name, query_params))
elastic_url
|> prepare_url(make_path(index_name, type_name, query_params))
|> HTTP.post(JSON.encode!(data))
end

Expand All @@ -64,7 +68,8 @@ defmodule Elastix.Document do
query_params :: Keyword.t()
) :: HTTP.resp()
def get(elastic_url, index_name, type_name, id, query_params \\ []) do
prepare_url(elastic_url, make_path(index_name, type_name, query_params, id))
elastic_url
|> prepare_url(make_path(index_name, type_name, query_params, id))
|> HTTP.get()
end

Expand All @@ -86,7 +91,8 @@ defmodule Elastix.Document do
|> Enum.join("/")

url =
prepare_url(elastic_url, [path, "_mget"])
elastic_url
|> prepare_url([path, "_mget"])
|> HTTP.append_query_string(query_params)

# HTTPoison does not provide an API for a GET request with a body.
Expand All @@ -109,7 +115,8 @@ defmodule Elastix.Document do
query_params :: Keyword.t()
) :: HTTP.resp()
def delete(elastic_url, index_name, type_name, id, query_params \\ []) do
prepare_url(elastic_url, make_path(index_name, type_name, query_params, id))
elastic_url
|> prepare_url(make_path(index_name, type_name, query_params, id))
|> HTTP.delete()
end

Expand All @@ -124,7 +131,8 @@ defmodule Elastix.Document do
query_params :: Keyword.t()
) :: HTTP.resp()
def delete_matching(elastic_url, index_name, %{} = query, query_params \\ []) do
prepare_url(elastic_url, [index_name, "_delete_by_query"])
elastic_url
|> prepare_url([index_name, "_delete_by_query"])
|> HTTP.append_query_string(query_params)
|> HTTP.post(JSON.encode!(query))
end
Expand Down Expand Up @@ -181,13 +189,11 @@ defmodule Elastix.Document do

@doc false
def make_path(index_name, type_name, query_params) do
"/#{index_name}/#{type_name}"
|> HTTP.append_query_string(query_params)
HTTP.append_query_string("/#{index_name}/#{type_name}", query_params)
end

@doc false
def make_path(index_name, type_name, query_params, id, suffix \\ nil) do
"/#{index_name}/#{type_name}/#{id}/#{suffix}"
|> HTTP.append_query_string(query_params)
HTTP.append_query_string("/#{index_name}/#{type_name}/#{id}/#{suffix}", query_params)
end
end
Loading
Loading