diff --git a/.travis.yml b/.travis.yml index 7a8d246..6d1e02d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,9 @@ notifications: recipients: - support@roundscope.com 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" diff --git a/lib/tirexs/bulk.ex b/lib/tirexs/bulk.ex index 7b0d36a..a8848ff 100644 --- a/lib/tirexs/bulk.ex +++ b/lib/tirexs/bulk.ex @@ -76,7 +76,7 @@ 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 @@ -84,7 +84,7 @@ defmodule Tirexs.Bulk do 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 @@ -96,4 +96,4 @@ defmodule Tirexs.Bulk do end end -end \ No newline at end of file +end diff --git a/lib/tirexs/elastic_search.ex b/lib/tirexs/elastic_search.ex index 33fc3ed..911a577 100644 --- a/lib/tirexs/elastic_search.ex +++ b/lib/tirexs/elastic_search.ex @@ -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 @@ -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 @@ -90,4 +91,4 @@ defmodule Tirexs.ElasticSearch do end defp make_headers, do: [{'Content-Type', 'application/json'}] -end \ No newline at end of file +end diff --git a/lib/tirexs/manage.ex b/lib/tirexs/manage.ex index 83a0879..6160a43 100644 --- a/lib/tirexs/manage.ex +++ b/lib/tirexs/manage.ex @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/tirexs/percolator.ex b/lib/tirexs/percolator.ex index 9338fd4..674525d 100644 --- a/lib/tirexs/percolator.ex +++ b/lib/tirexs/percolator.ex @@ -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 diff --git a/lib/tirexs/query.ex b/lib/tirexs/query.ex index 1b42bd1..ce8c79a 100644 --- a/lib/tirexs/query.ex +++ b/lib/tirexs/query.ex @@ -422,11 +422,11 @@ 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 @@ -434,4 +434,4 @@ defmodule Tirexs.Query do @doc false def to_resource_json(definition), do: JSEX.encode!(definition[:search]) -end \ No newline at end of file +end diff --git a/mix.lock b/mix.lock index 1604871..08e1808 100644 --- a/mix.lock +++ b/mix.lock @@ -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"]}} diff --git a/test/acceptances/bulk_test.exs b/test/acceptances/bulk_test.exs index c5ed9d6..c38d408 100644 --- a/test/acceptances/bulk_test.exs +++ b/test/acceptances/bulk_test.exs @@ -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 @@ -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"] diff --git a/test/acceptances/elastic_search_test.exs b/test/acceptances/elastic_search_test.exs index 856d238..4d81fb5 100644 --- a/test/acceptances/elastic_search_test.exs +++ b/test/acceptances/elastic_search_test.exs @@ -14,7 +14,7 @@ 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 @@ -22,7 +22,7 @@ defmodule Acceptances.ElasticSearchTest 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 @@ -30,7 +30,7 @@ defmodule Acceptances.ElasticSearchTest 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 @@ -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 @@ -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 diff --git a/test/acceptances/manage_test.exs b/test/acceptances/manage_test.exs index 04e5b97..0683e37 100644 --- a/test/acceptances/manage_test.exs +++ b/test/acceptances/manage_test.exs @@ -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 @@ -12,11 +11,19 @@ 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 @@ -24,14 +31,11 @@ defmodule Acceptances.ManageTest 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" @@ -47,7 +51,7 @@ 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 @@ -55,13 +59,10 @@ defmodule Acceptances.ManageTest do 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" @@ -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 @@ -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 diff --git a/test/acceptances/percolator_test.exs b/test/acceptances/percolator_test.exs index e33d204..cbeab4d 100644 --- a/test/acceptances/percolator_test.exs +++ b/test/acceptances/percolator_test.exs @@ -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 diff --git a/test/acceptances/warmer_test.exs b/test/acceptances/warmer_test.exs index 3af66c0..5602421 100644 --- a/test/acceptances/warmer_test.exs +++ b/test/acceptances/warmer_test.exs @@ -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 @@ -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 diff --git a/test/test_helper.exs b/test/test_helper.exs index 2df156f..4d5a754 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,27 @@ ExUnit.start +defmodule TestHelpers do + import :timer, only: [ sleep: 1 ] + + def create_index(name, settings), do: Tirexs.ElasticSearch.put(name, settings) + def remove_index(name, settings), do: Tirexs.ElasticSearch.delete(name, settings) + + def repeat(func) do + repeat_receiving_until(503, 200, 10, func) + end + + defp repeat_receiving_until(_status, _result_status, 1, func) do + func.() + end + + defp repeat_receiving_until(status, result_status, retries, func) do + case func.() do + {str_res, ^result_status, body} -> {str_res, result_status, body} + {_, ^status, _} -> + sleep 100 + repeat_receiving_until(status, result_status, retries - 1, func) + result -> result + end + end + +end diff --git a/test/tirexs/bulk_test.exs b/test/tirexs/bulk_test.exs index bd6adf1..5d0b8ff 100644 --- a/test/tirexs/bulk_test.exs +++ b/test/tirexs/bulk_test.exs @@ -10,13 +10,13 @@ defmodule Tirexs.BulkTest do document = [id: "id", title: "Hello"] assert get_id_from_document(document) == "id" - document = [{"id", "id"}, {"title", "Hello"}] + document = [{:id, "id"}, {:title, "Hello"}] assert get_id_from_document(document) == "id" document = [_id: "_id", title: "Hello"] assert get_id_from_document(document) == "_id" - document = [{"_id", "_id"}, {"title", "Hello"}] + document = [{:_id, "_id"}, {:title, "Hello"}] assert get_id_from_document(document) == "_id" end