diff --git a/lib/tirexs/query.ex b/lib/tirexs/query.ex index adc6094..839239b 100644 --- a/lib/tirexs/query.ex +++ b/lib/tirexs/query.ex @@ -419,6 +419,7 @@ defmodule Tirexs.Query do "#{definition[:index]}" end + IO.write :stderr, "warning: `/_search` is deprecated, please use `Tirexs.Resources.APIs._search/2` instead\n" <> Exception.format_stacktrace { url, json } = { "#{url}/_search" <> to_param(opts, ""), to_resource_json(definition) } case Tirexs.ElasticSearch.post(url, json, settings) do {:ok, _, result} -> diff --git a/lib/tirexs/resources/apis.ex b/lib/tirexs/resources/apis.ex index 8ff6573..e9f88e5 100644 --- a/lib/tirexs/resources/apis.ex +++ b/lib/tirexs/resources/apis.ex @@ -47,6 +47,7 @@ defmodule Tirexs.Resources.APIs do defdelegate [ _validate_query(), _validate_query(a), _validate_query(a,b), _validate_query(a,b,c) ], to: Search defdelegate [ _count(), _count(a), _count(a,b), _count(a,b,c) ], to: Search defdelegate [ _search_exists(), _search_exists(a), _search_exists(a,b), _search_exists(a,b,c) ], to: Search + defdelegate [ _search(), _search(a), _search(a,b), _search(a,b,c) ], to: Search alias Tirexs.Resources.Indices diff --git a/lib/tirexs/resources/search.ex b/lib/tirexs/resources/search.ex index 32ab2dc..132da59 100644 --- a/lib/tirexs/resources/search.ex +++ b/lib/tirexs/resources/search.ex @@ -51,4 +51,13 @@ defmodule Tirexs.Resources.Search do def _search_exists({a}), do: __c(urn([@r[:action], {a}]), @r) def _search_exists(a), do: __c(urn([a, @r[:action]]), @r) def _search_exists(), do: __c(urn([@r[:action]]), @r) + + @doc false + @r [action: "/_search", bump: :post, bump!: :post!] + def _search(a, b, c), do: __c(urn([a, b, @r[:action], c]), @r) + def _search(a, {b}), do: __c(urn([a, @r[:action], {b}]), @r) + def _search(a, b), do: __c(urn([a, b, @r[:action]]), @r) + def _search({a}), do: __c(urn([@r[:action], {a}]), @r) + def _search(a), do: __c(urn([a, @r[:action]]), @r) + def _search(), do: __c(urn([@r[:action]]), @r) end diff --git a/test/acceptances/resources/search_test.exs b/test/acceptances/resources/search_test.exs index bc01c36..d9c014e 100644 --- a/test/acceptances/resources/search_test.exs +++ b/test/acceptances/resources/search_test.exs @@ -76,4 +76,11 @@ defmodule Acceptances.Resources.SearchTest do { :ok, 200, r } = Resources.bump(search)._search_exists("bear_test", "my_type") assert r[:exists] end + + test "_search/2" do + { :ok, 201, _ } = HTTP.put("/bear_test/my_type/2?refresh=true", [user: "zatvobor"]) + search = [query: [ term: [ user: "zatvobor" ] ]] + { :ok, 200, r } = Resources.bump(search)._search("bear_test", "my_type") + assert r[:hits][:total] == 1 + end end diff --git a/test/tirexs/resources/search_test.exs b/test/tirexs/resources/search_test.exs index 2fe4173..01ee1a4 100644 --- a/test/tirexs/resources/search_test.exs +++ b/test/tirexs/resources/search_test.exs @@ -103,4 +103,39 @@ defmodule Tirexs.Resources.SearchTest do actual = Search._search_exists("twitter", "tweet", { [q: "user:kimchy"] }) assert actual == "twitter/tweet/_search/exists?q=user%3Akimchy" end + + test ~S| functions like a '_search({ [q: "tag:wow"] })' | do + actual = Search._search({ [q: "tag:wow"] }) + assert actual == "_search?q=tag%3Awow" + end + + test ~S| functions like a '_search("twitter")' | do + actual = Search._search("twitter") + assert actual == "twitter/_search" + end + + test ~S| functions like a '_search("twitter", "tweet")' | do + actual = Search._search("twitter", "tweet") + assert actual == "twitter/tweet/_search" + end + + test ~S| functions like a '_search("twitter", ["tweet","user"])' | do + actual = Search._search("twitter", ["tweet","user"]) + assert actual == "twitter/tweet,user/_search" + end + + test ~S| functions like a '_search("twitter", ["tweet","user"], { [q: "user:kimchy"] })' | do + actual = Search._search("twitter", ["tweet","user"], { [q: "user:kimchy"] }) + assert actual == "twitter/tweet,user/_search?q=user%3Akimchy" + end + + test ~S| functions like a '_search("twitter/tweet", { [q: "user:kimchy"] })' | do + actual = Search._search("twitter/tweet", { [q: "user:kimchy"] }) + assert actual == "twitter/tweet/_search?q=user%3Akimchy" + end + + test ~S| functions like a '_search("twitter", "tweet", { [q: "user:kimchy"] })' | do + actual = Search._search("twitter", "tweet", { [q: "user:kimchy"] }) + assert actual == "twitter/tweet/_search?q=user%3Akimchy" + end end