Skip to content

Commit

Permalink
Search API
Browse files Browse the repository at this point in the history
  • Loading branch information
Zatvobor committed Mar 9, 2016
1 parent a1e57c5 commit 94fbe57
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tirexs/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down
1 change: 1 addition & 0 deletions lib/tirexs/resources/apis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/tirexs/resources/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/acceptances/resources/search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions test/tirexs/resources/search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 94fbe57

Please sign in to comment.