Skip to content

Commit

Permalink
Merge pull request #198 from Zatvobor/resources
Browse files Browse the repository at this point in the history
`Tirexs.Resources` module
  • Loading branch information
Zatvobor committed Mar 9, 2016
2 parents fe3839d + 13e43f6 commit e8e28a3
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/tirexs/manage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Tirexs.Manage do

@doc false
def count(options, settings) do
IO.write :stderr, "warning: `Tirexs.ElasticSearch.count/2` is deprecated, please use `Tirexs.Resources.APIs._count/2` instead\n" <> Exception.format_stacktrace
execute_get_if_body_empty_and_post_otherwise("_count", options, settings)
end

Expand All @@ -23,11 +24,13 @@ defmodule Tirexs.Manage do

@doc false
def validate(options, settings) do
IO.write :stderr, "warning: `Tirexs.ElasticSearch.validate/2` is deprecated, please use `Tirexs.Resources.APIs._validate_query/2` instead\n" <> Exception.format_stacktrace
execute_get_if_body_empty_and_post_otherwise("_validate/query", options, settings)
end

@doc false
def explain(options, settings) do
IO.write :stderr, "warning: `Tirexs.ElasticSearch.explain/2` is deprecated, please use `Tirexs.Resources.APIs._explain/2` instead\n" <> Exception.format_stacktrace
Tirexs.ElasticSearch.get(make_url("_explain", options), settings)
end

Expand Down
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
25 changes: 17 additions & 8 deletions lib/tirexs/resources.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,30 @@ defmodule Tirexs.Resources do
iex> bump._refresh(["bear_test", "duck_test"], { [force: false] })
{ :ok, 200, ... }
It is also available for bumping some resources with request body:
iex> search = [query: [ term: [ user: "zatvobor" ] ] ]
iex> bump(search)._count("bear_test", "my_type")
Play with resources you have and see what kind of HTTP verb is used.
"""
def bump(), do: __t(:bump)
def bump(uri), do: __t(:bump, uri)
def bump(%URI{} = uri), do: __t(:bump, [], uri)
def bump(body), do: __t(:bump, body)
def bump(body, %URI{} = uri), do: __t(:bump, body, uri)
def bump!(), do: __t(:bump!)
def bump!(uri), do: __t(:bump!, uri)
def bump!(%URI{} = uri), do: __t(:bump!, [], uri)
def bump!(body), do: __t(:bump!, body)
def bump!(body, %URI{} = uri), do: __t(:bump!, body, uri)


@doc false
def __c(urn, meta) do
def __c(urn, meta) when is_binary(urn) do
if ctx = Process.delete(:tirexs_resources_chain) do
args = case urn do
urn when is_binary(urn) -> [ urn, ctx[:uri] ]
[ urn, body ] -> [ urn, ctx[:uri], body ]
args = case { urn, ctx[:body] } do
{ urn, [] } -> [ urn, ctx[:uri] ]
{ urn, body } -> [ urn, ctx[:uri], body ]
end
Kernel.apply(Tirexs.HTTP, meta[ctx[:label]], args)
else
Expand All @@ -102,8 +111,8 @@ defmodule Tirexs.Resources do
end

@doc false
defp __t(label, uri \\ Tirexs.ENV.get_uri_env()) do
Process.put(:tirexs_resources_chain, [label: label, uri: uri])
defp __t(label, body \\ [], %URI{} = uri \\ Tirexs.ENV.get_uri_env()) do
Process.put(:tirexs_resources_chain, [label: label, body: body, uri: uri])
Tirexs.Resources.APIs
end
end
14 changes: 13 additions & 1 deletion lib/tirexs/resources/apis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ defmodule Tirexs.Resources.APIs do
"""

alias Tirexs.Resources.Indices
alias Tirexs.Resources.Search


@doc false
defdelegate [ _explain(a), _explain(a,b), _explain(a,b,c), _explain(a,b,c,d) ], to: Search
defdelegate [ _search_shards(a), _search_shards(a,b), _search_shards(a,b,c) ], to: Search
defdelegate [ _field_stats(), _field_stats(a), _field_stats(a,b) ], to: Search
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

## Mapping Management

Expand Down
63 changes: 63 additions & 0 deletions lib/tirexs/resources/search.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule Tirexs.Resources.Search do
@moduledoc false

import Tirexs.Resources, only: [urn: 1, __c: 2]


@doc false
@r [action: "/_explain", bump: :post, bump!: :post!]
def _explain(a, b, c, d), do: __c(urn([a, b, c, @r[:action], d]), @r)
def _explain(a, b, c), do: __c(urn([a, b, c, @r[:action]]), @r)
def _explain(a, b), do: __c(urn([a, @r[:action], b]), @r)
def _explain(a), do: __c(urn([a, @r[:action]]), @r)

@doc false
@r [action: "/_search_shards", bump: :get, bump!: :get!]
def _search_shards(a, b, c), do: __c(urn([a, b, @r[:action], c]), @r)
def _search_shards(a, {b}), do: __c(urn([a, @r[:action], {b}]), @r)
def _search_shards(a, b), do: __c(urn([a, b, @r[:action]]), @r)
def _search_shards(a), do: __c(urn([a, @r[:action]]), @r)

@doc false
@r [action: "/_field_stats", bump: :post, bump!: :post!]
def _field_stats(a, b), do: __c(urn([a, @r[:action], b]), @r)
def _field_stats({a}), do: __c(urn([@r[:action], {a}]), @r)
def _field_stats(a), do: __c(urn([a, @r[:action]]), @r)
def _field_stats(), do: __c(urn([@r[:action]]), @r)

@doc false
@r [action: "/_validate/query", bump: :post, bump!: :post!]
def _validate_query(a, b, c), do: __c(urn([a, b, @r[:action], c]), @r)
def _validate_query(a, {b}), do: __c(urn([a, @r[:action], {b}]), @r)
def _validate_query(a, b), do: __c(urn([a, b, @r[:action]]), @r)
def _validate_query({a}), do: __c(urn([@r[:action], {a}]), @r)
def _validate_query(a), do: __c(urn([a, @r[:action]]), @r)
def _validate_query(), do: __c(urn([@r[:action]]), @r)

@doc false
@r [action: "/_count", bump: :post, bump!: :post!]
def _count(a, b, c), do: __c(urn([a, b, @r[:action], c]), @r)
def _count(a, {b}), do: __c(urn([a, @r[:action], {b}]), @r)
def _count(a, b), do: __c(urn([a, b, @r[:action]]), @r)
def _count({a}), do: __c(urn([@r[:action], {a}]), @r)
def _count(a), do: __c(urn([a, @r[:action]]), @r)
def _count(), do: __c(urn([@r[:action]]), @r)

@doc false
@r [action: "/_search/exists", bump: :post, bump!: :post!]
def _search_exists(a, b, c), do: __c(urn([a, b, @r[:action], c]), @r)
def _search_exists(a, {b}), do: __c(urn([a, @r[:action], {b}]), @r)
def _search_exists(a, b), do: __c(urn([a, b, @r[:action]]), @r)
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
86 changes: 86 additions & 0 deletions test/acceptances/resources/search_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
defmodule Acceptances.Resources.SearchTest do
use ExUnit.Case

alias Tirexs.{HTTP, Resources}


setup do
HTTP.delete("bear_test") && :ok
end


test "_explain/4" do
{ :ok, 201, _ } = HTTP.put("/bear_test/my_type/1?refresh=true", [user: "kimchy"])
{ :ok, 200, r } = Resources.bump._explain("bear_test", "my_type", "1", { [q: "user:k*"] })
assert r[:matched]
end

test "_explain/3" do
{ :ok, 201, _ } = HTTP.put("/bear_test/my_type/1?refresh=true", [user: "kimchy"])
search = [query: [ term: [ user: "kimchy" ] ]]
{ :ok, 200, r } = Resources.bump(search)._explain("bear_test", "my_type", "1")
assert r[:matched]
end

test "_explain/2" do
{ :ok, 201, _ } = HTTP.put("/bear_test/my_type/2?refresh=true", [user: "zatvobor"])
{ :ok, 200, r } = Resources.bump._explain("bear_test/my_type/2", { [q: "user:z*"] })
assert r[:matched]
end

test "_explain/1" 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)._explain("bear_test/my_type/2")
assert r[:matched]
end

test "_search_shards/2" do
{ :ok, 200, _ } = HTTP.put("/bear_test")
{ :ok, 200, _ } = Resources.bump._search_shards("bear_test", { [local: true] })
end

test "_field_stats/0" do
{ :ok, 200, _ } = HTTP.put("/bear_test")
{ :ok, 200, _ } = Resources.bump([fields: ["rating"]])._field_stats()
end

test "_field_stats/2" do
{ :ok, 200, _ } = HTTP.put("/bear_test")
{ :ok, 200, _ } = Resources.bump._field_stats("bear_test", { [fields: "some"] })
end

test "_validate_query/2" do
{ :ok, 201, _ } = HTTP.put("/bear_test/my_type/1?refresh=true", [user: "kimchy"])
{ :ok, 200, r } = Resources.bump._validate_query("bear_test", { [q: "user1:z*"] })
assert r[:valid]
end

test "_validate_query/1" 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)._validate_query("bear_test")
assert r[:valid]
end

test "_count/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)._count("bear_test", "my_type")
assert r[:count] == 1
end

test "_search_exists/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_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
23 changes: 14 additions & 9 deletions test/tirexs/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,39 @@ defmodule Tirexs.HTTPTest do
assert actual == "http://127.0.0.1:92/articles/document/1?_source=false"
end

test ~S(decode/1 {"hello":"world"}) do
actual = decode("{\"hello\":\"world\"}")
test ~S'decode/1 {"hello":"world"}' do
actual = decode(~S'{"hello":"world"}')
assert actual == %{hello: "world"}
end

test ~S(decode/1 {"hello":"world"} as list) do
actual = decode("{\"hello\":\"world\"}")
test ~S'decode/1 {"hello":"world"} as list' do
actual = decode(~S'{"hello":"world"}')
assert Map.to_list(actual) == [hello: "world"]
end

test ~S(decode/1 "hello" string) do
test ~S'decode/1 "hello" string' do
actual = decode("\"hello\"")
assert actual == "hello"
end

test ~S(encode/1 %{hello: "world"} as map) do
test ~S'encode/1 %{hello: "world"} as map' do
actual = encode(%{hello: "world"})
assert actual == "{\"hello\":\"world\"}"
assert actual == ~S'{"hello":"world"}'
end

test "encode/1 %{hello: 'world'} as map" do
actual = encode(%{hello: 'world'})
assert actual == "{\"hello\":[119,111,114,108,100]}"
end

test ~S(encode/1 [hello: "world"] as list) do
test ~S'encode/1 [hello: "world"] as list' do
actual = encode([hello: "world"])
assert actual == "{\"hello\":\"world\"}"
assert actual == ~S'{"hello":"world"}'
end

test ~S'encode/1 [query: [ term: [ message: "search" ] ]] as list' do
actual = encode([query: [ term: [ message: "search" ] ]])
assert actual == ~S'{"query":{"term":{"message":"search"}}}'
end

test "encode/1 []" do
Expand Down
Loading

0 comments on commit e8e28a3

Please sign in to comment.