From f2a9156114184005778e2f03065631c929b77cb3 Mon Sep 17 00:00:00 2001 From: evuez Date: Thu, 3 May 2018 16:27:03 +0200 Subject: [PATCH] Don't strip path from ES host Should fix #49. --- lib/elastix/http.ex | 10 ++++++++-- lib/elastix/search.ex | 6 ++---- test/elastix/http_test.exs | 9 +++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/elastix/http.ex b/lib/elastix/http.ex index 8f1c00b..71ca3e3 100644 --- a/lib/elastix/http.ex +++ b/lib/elastix/http.ex @@ -8,8 +8,14 @@ defmodule Elastix.HTTP do @type resp :: {:ok, HTTPoison.Response.t()} | {:error, HTTPoison.Error.t()} @doc false - def prepare_url(url, path) when is_binary(path), do: URI.merge(url, path) |> to_string - def prepare_url(url, parts) when is_list(parts), do: prepare_url(url, Path.join(parts)) + def prepare_url(url, path) when is_binary(path) do + "#{url}/" + |> URI.merge(String.trim_leading(path, "/")) + |> to_string + end + + def prepare_url(url, parts) when is_list(parts), + do: prepare_url("#{url}/", Path.join(parts)) @doc false def request(method, url, body \\ "", headers \\ [], options \\ []) do diff --git a/lib/elastix/search.ex b/lib/elastix/search.ex index 7b22260..f9ae153 100644 --- a/lib/elastix/search.ex +++ b/lib/elastix/search.ex @@ -118,12 +118,10 @@ defmodule Elastix.Search do @doc false def make_path(index, types, query_params, api_type \\ "_search") do - path_root = "/#{index}" - path = case types do - [] -> path_root - _ -> path_root <> "/" <> Enum.join(types, ",") + [] -> index + _ -> index <> "/" <> Enum.join(types, ",") end full_path = "#{path}/#{api_type}" diff --git a/test/elastix/http_test.exs b/test/elastix/http_test.exs index a21e475..c51820f 100644 --- a/test/elastix/http_test.exs +++ b/test/elastix/http_test.exs @@ -7,6 +7,15 @@ defmodule Elastix.HTTPTest do test "prepare_url/2 should concat url with path" do assert HTTP.prepare_url("http://127.0.0.1:9200/", "/some_path") == "http://127.0.0.1:9200/some_path" + + assert HTTP.prepare_url("http://127.0.0.1:9200/base_path", "/some_path") == + "http://127.0.0.1:9200/base_path/some_path" + + assert HTTP.prepare_url("http://127.0.0.1:9200/base_path", "some_path") == + "http://127.0.0.1:9200/base_path/some_path" + + assert HTTP.prepare_url("http://127.0.0.1:9200/base_path/", "/some_path") == + "http://127.0.0.1:9200/base_path/some_path" end test "prepare_url/2 should concat url with a list of path parts" do