Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up #124

Merged
merged 13 commits into from
May 15, 2014
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ notifications:
recipients:
- [email protected]
otp_release:
- R16B
- 17.0
before_install:
- git clone https://github.com/elixir-lang/elixir
- cd elixir && git checkout v0.11.2 && make && cd ..
- cd elixir && git checkout stable && make && cd ..
before_script: "export PATH=`pwd`/elixir/bin:$PATH"
script: "MIX_ENV=test mix do deps.get, compile, travis"
6 changes: 3 additions & 3 deletions lib/tirexs/bulk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ defmodule Tirexs.Bulk do
end

def get_id_from_document(document) do
document[:id] || document["id"] || document[:_id] || document["_id"]
document[:id] || document[:_id]
end

def convert_document_to_json(document) do
JSEX.encode!(document)
end

def get_type_from_document(document) do
document[:_type] || document["_type"] || document[:type] || document["type"] || "document"
document[:_type] || document[:type] || "document"
end

def match(document) do
Expand All @@ -96,4 +96,4 @@ defmodule Tirexs.Bulk do
end
end

end
end
7 changes: 4 additions & 3 deletions lib/tirexs/elastic_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Tirexs.ElasticSearch do

def post(query_url, body, config) do
unless body == [], do: body = to_string(body)
do_request(make_url(query_url, config), :post, body)
url = make_url(query_url, config)
do_request(url, :post, body)
end

@doc false
Expand Down Expand Up @@ -79,7 +80,7 @@ defmodule Tirexs.ElasticSearch do
end
end

def get_body_json(body), do: JSEX.decode!(to_string(body))
def get_body_json(body), do: JSEX.decode!(to_string(body), [{:labels, :atom}])

def make_url(query_url, config) do
if config.port == nil || config.port == 80 do
Expand All @@ -90,4 +91,4 @@ defmodule Tirexs.ElasticSearch do
end

defp make_headers, do: [{'Content-Type', 'application/json'}]
end
end
23 changes: 15 additions & 8 deletions lib/tirexs/manage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Tirexs.Manage do
import Tirexs.DSL.Logic

def count(options, settings) do
body = JSEX.encode!(options[:filter] || options[:query] || [])
Tirexs.ElasticSearch.post(make_url("_count", options), body, settings)
execute_get_if_body_empty_and_post_otherwise("_count", options, settings)
end

def delete_by_query(options, settings) do
Expand All @@ -17,12 +16,7 @@ defmodule Tirexs.Manage do
end

def validate(options, settings) do
body = JSEX.encode!(options[:filter] || options[:query] || [])
if body == JSEX.encode!([]) do
Tirexs.ElasticSearch.get(make_url("_validate/query", options), settings)
else
Tirexs.ElasticSearch.post(make_url("_validate/query", options), body, settings)
end
execute_get_if_body_empty_and_post_otherwise("_validate/query", options, settings)
end

def explain(options, settings) do
Expand Down Expand Up @@ -78,4 +72,17 @@ defmodule Tirexs.Manage do
options = Dict.delete(options, h)
delete_options(t, options)
end

defp execute_get_if_body_empty_and_post_otherwise(url_suffix, options, settings) do
body =
cond do
options[:filter] -> options[:filter]
options[:query] -> [query: options[:query]]
true -> []
end
case JSEX.encode!(body) do
"[]" -> Tirexs.ElasticSearch.get(make_url(url_suffix, options), settings)
body -> Tirexs.ElasticSearch.post(make_url(url_suffix, options), body, settings)
end
end
end
2 changes: 1 addition & 1 deletion lib/tirexs/percolator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Tirexs.Percolator do
"#{definition[:index]}/#{definition[:name]}"
end

{ url, json } = { "_percolator/#{url}", to_resource_json(definition) }
{ url, json } = { ".percolator/#{url}", to_resource_json(definition) }
Tirexs.ElasticSearch.put(url, json, settings)
end

Expand Down
12 changes: 6 additions & 6 deletions lib/tirexs/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,16 @@ defmodule Tirexs.Query do
{ url, json } = { "#{url}/_search" <> to_param(opts, ""), to_resource_json(definition) }
case Tirexs.ElasticSearch.post(url, json, settings) do
{:ok, _, result} ->
count = result["hits"]["total"]
hits = result["hits"]["hits"]
facets = result["facets"]
max_score = result["hits"]["max_score"]
scroll_id = result["_scroll_id"]
count = result[:hits][:total]
hits = result[:hits][:hits]
facets = result[:facets]
max_score = result[:hits][:max_score]
scroll_id = result[:_scroll_id]
Result.new(count: count, hits: hits, facets: facets, max_score: max_score, _scroll_id: scroll_id)
result -> result
end
end

@doc false
def to_resource_json(definition), do: JSEX.encode!(definition[:search])
end
end
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[ "jsex": {:git, "git://github.com/talentdeficit/jsex.git", "65aa6ae7c1543a0c4e83032c04626af7be7a7f3e", []},
"jsx": {:git, "git://github.com/talentdeficit/jsx.git", "e50af6e109cb03bd26acf715cbc77de746507d1d", [tag: "v1.4.3"]} ]
%{"jsex": {:git, "git://github.com/talentdeficit/jsex.git", "03ad4ff0967331afd01464857d41e5e497ed198c", []},
"jsx": {:git, "git://github.com/talentdeficit/jsx.git", "507fa4c41db33c81e925ab53f4d789d234aaff2f", [tag: "v2.0.1"]}}
4 changes: 2 additions & 2 deletions test/acceptances/bulk_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Acceptances.BulkTest do

Tirexs.Manage.refresh("bear_test", settings)
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/_count", settings)
assert body["count"] == 11
assert body[:count] == 11
end

test :update do
Expand All @@ -41,7 +41,7 @@ defmodule Acceptances.BulkTest do

Tirexs.Manage.refresh("bear_test", settings)
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/_count", settings)
assert body["count"] == 2
assert body[:count] == 2

Tirexs.Bulk.store [index: "bear_test", type: "document", id: 1, retry_on_conflict: 3], settings do
update doc: [title: "updated_title"]
Expand Down
10 changes: 5 additions & 5 deletions test/acceptances/elastic_search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ defmodule Acceptances.ElasticSearchTest do
{:error, _, _} = get("missing_index", settings)
{:ok, _, body} = get("", settings)

assert body["tagline"] == "You Know, for Search"
assert body[:tagline] == "You Know, for Search"

end

test :create_index do
settings = Tirexs.ElasticSearch.Config.new()
delete("bear_test", settings)
{:ok, _, body} = put("bear_test", settings)
assert body["acknowledged"] == true
assert body[:acknowledged] == true
delete("bear_test", settings)
end

test :delete_index do
settings = Tirexs.ElasticSearch.Config.new()
put("bear_test", settings)
{:ok, _, body} = delete("bear_test", settings)
assert body["acknowledged"] == true
assert body[:acknowledged] == true
end


Expand Down Expand Up @@ -69,7 +69,7 @@ defmodule Acceptances.ElasticSearchTest do

{:ok, _, body} = Tirexs.Mapping.create_resource(index, settings)

assert body["acknowledged"] == true
assert body[:acknowledged] == true

delete("bear_test", settings)
end
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule Acceptances.ElasticSearchTest do
result = Tirexs.Query.create_resource(s, settings)

assert result.count == 1
assert List.first(result.hits)["_source"]["id"] == 2
assert List.first(result.hits)[:_source][:id] == 2

end
end
68 changes: 31 additions & 37 deletions test/acceptances/manage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ Code.require_file "../../test_helper.exs", __ENV__.file

defmodule Acceptances.ManageTest do
use ExUnit.Case

import Tirexs.ElasticSearch
import TestHelpers

import Tirexs.Bulk

Expand All @@ -12,26 +11,31 @@ defmodule Acceptances.ManageTest do

@settings Tirexs.ElasticSearch.Config.new()

setup do
create_index("bear_test", @settings)
:ok
end

teardown do
remove_index("bear_test", @settings)
:ok
end

test :count do
put("bear_test", @settings)
{_, _, body} = Tirexs.Manage.count([index: "bear_test"], @settings)
assert body["count"] == 0
delete("bear_test", @settings)
{:ok, 200, body} = repeat fn -> Tirexs.Manage.count([index: "bear_test"], @settings) end
assert Dict.get(body, :count) == 0
end

test :count_with_query do
query = query do
term "user", "kim"
end

put("bear_test", @settings)
{_, _, body} = Tirexs.Manage.count([index: "bear_test"] ++ query, @settings)
assert body["count"] == 0
delete("bear_test", @settings)
{:ok, 200, body} = repeat fn -> Tirexs.Manage.count([index: "bear_test"] ++ query, @settings) end
assert Dict.get(body, :count) == 0
end

test :delete_by_query do
delete("bear_test", @settings)
index = [index: "bear_test"]
mappings do
index "id", type: "integer"
Expand All @@ -47,21 +51,18 @@ defmodule Acceptances.ManageTest do

{_, _, body} = Tirexs.ElasticSearch.get("bear_test/_count", @settings)

assert body["count"] == 2
assert Dict.get(body, :count) == 2

query = query do
term "id", 1
end

Tirexs.Manage.delete_by_query([index: "bear_test", q: "id:1"] ++ query, @settings)
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/_count", @settings)
assert body["count"] == 1

delete("bear_test", @settings)
assert Dict.get(body, :count) == 1
end

test :more_like_this do
delete("bear_test", @settings)
Tirexs.Bulk.store [index: "bear_test", refresh: false], @settings do
create id: 1, name: "bar1", description: "foo bar test", type: "my_type"
create id: 2, name: "bar2", description: "foo bar test", type: "my_type"
Expand All @@ -70,14 +71,12 @@ defmodule Acceptances.ManageTest do
Tirexs.Manage.refresh(["bear_test"], @settings)

{_, _, body} = Tirexs.Manage.more_like_this([id: 1, type: "my_type", index: "bear_test", mlt_fields: "name,description", min_term_freq: 1], @settings)
assert body["hits"]["hits"] == []
assert Dict.get(body, :hits) |> Dict.get(:hits) == []
end

test :validate_and_explain do
delete("bear_test", @settings)
put("bear_test/my_type", @settings)
doc = [user: "kimchy", post_date: "2009-11-15T14:12:12", message: "trying out Elastic Search"]
put("bear_test/my_type/1", JSEX.encode!(doc), @settings)
Tirexs.ElasticSearch.put("bear_test/my_type/1", JSEX.encode!(doc), @settings)

query = query do
filtered do
Expand All @@ -91,39 +90,34 @@ defmodule Acceptances.ManageTest do
end

{_, _, body} = Tirexs.Manage.validate([index: "bear_test"] ++ query, @settings)

assert body["valid"] == true
assert Dict.get(body, :valid) == true

{_, _, body} = Tirexs.Manage.validate([index: "bear_test", q: "user:foo"], @settings)

assert body["valid"] == true
assert Dict.get(body, :valid) == true

{_, _, body} = Tirexs.Manage.explain([index: "bear_test", type: "my_type", id: 1, q: "message:search"], @settings)
body = JSEX.decode!(to_string(body))
assert body["matched"] == false

delete("bear_test", @settings)
body = JSEX.decode!(to_string(body), [{:labels, :atom}])
assert Dict.get(body, :matched) == false
end

test :update do
delete("bear_test", @settings)
put("bear_test/my_type", @settings)
Tirexs.ElasticSearch.put("bear_test/my_type", @settings)
doc = [user: "kimchy", counter: 1, post_date: "2009-11-15T14:12:12", message: "trying out Elastic Search", id: 1]
put("bear_test/my_type/1", JSEX.encode!(doc), @settings)
Tirexs.ElasticSearch.put("bear_test/my_type/1", JSEX.encode!(doc), @settings)

{_, _, body} = get("bear_test/my_type/1", @settings)
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/my_type/1", @settings)

assert body["_source"]["counter"] == 1
assert body[:_source][:counter] == 1
update = [script: "ctx._source.counter += count", params: [count: 1]]
Tirexs.Manage.update([index: "bear_test", type: "my_type", id: "1"], update, @settings)

{_, _, body} = get("bear_test/my_type/1", @settings)
assert body["_source"]["counter"] == 2
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/my_type/1", @settings)
assert body[:_source][:counter] == 2

update_doc = [doc: [name: "new_name"]]
Tirexs.Manage.update([index: "bear_test", type: "my_type", id: "1"], update_doc, @settings)
{_, _, body} = get("bear_test/my_type/1", @settings)
assert body["_source"]["name"] == "new_name"
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/my_type/1", @settings)
assert body[:_source][:name] == "new_name"
end

end
2 changes: 1 addition & 1 deletion test/acceptances/percolator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ defmodule Tirexs.PerlocatorTest do

{_, _, body} = Tirexs.Percolator.create_resource(percolator, settings)

assert body["_id"] == "kuku"
assert body[:_id] == "kuku"
end
end
20 changes: 13 additions & 7 deletions test/acceptances/warmer_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
Code.require_file "../../test_helper.exs", __ENV__.file
defmodule Acceptances.WarmerTest do
use ExUnit.Case
import TestHelpers

import Tirexs.Search.Warmer

@settings Tirexs.ElasticSearch.Config.new()

teardown do
remove_index("bear_test", @settings)
:ok
end

test :create_warmer do
settings = Tirexs.ElasticSearch.Config.new()
Tirexs.ElasticSearch.delete("bear_test", settings)
Tirexs.ElasticSearch.delete("bear_test", @settings)

warmers = warmers do
warmer_1 [types: []] do
Expand All @@ -22,10 +30,8 @@ defmodule Acceptances.WarmerTest do
end
end

Tirexs.ElasticSearch.put("bear_test", JSEX.encode!(warmers), settings)
{_, _, body} = Tirexs.ElasticSearch.get("bear_test/my_type/_warmer/warmer_1", settings)

assert body["bear_test"]["warmers"] == [{"warmer_1",[{"types",[]},{"source",[{"query",[{"match_all",[]}]},{"facets",[{"facet_1",[{"terms",[{"field","field"}]}]}]}]}]}]
Tirexs.ElasticSearch.delete("bear_test", settings)
Tirexs.ElasticSearch.put("bear_test", JSEX.encode!(warmers), @settings)
{:ok, 200, body} = Tirexs.ElasticSearch.get("bear_test/_warmer/warmer_1", @settings)
assert Dict.get(body, :bear_test) |> Dict.get(:warmers) == [warmer_1: [types: [], source: [query: [match_all: []], facets: [facet_1: [terms: [field: "field"]]]]]]
end
end
Loading