Skip to content

Commit

Permalink
implement delete/3, delete!/3 functions for deleting Solr docs by ID(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
boonious committed Oct 9, 2018
1 parent 8369911 commit 8d9915e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/hui.ex
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ defmodule Hui do
```
See `Hui.Request.update/3` for more advanced update options.
"""
@spec update(binary | Hui.URL.t, binary | map | list(map), boolean) :: {:ok, HTTPoison.Response.t} | {:error, Hui.Error.t}
def update(url, docs, commit \\ true)
Expand All @@ -406,4 +405,11 @@ defmodule Hui do
def update!(url, docs, _commit) when is_binary(docs), do: Request.update(url, true, docs)
def update!(url, docs, commit) when is_map(docs) or is_list(docs), do: Request.update(url, true, %Hui.U{doc: docs, commit: commit})

@spec delete(binary | Hui.URL.t, binary | list(binary), boolean) :: {:ok, HTTPoison.Response.t} | {:error, Hui.Error.t}
def delete(url, ids, commit \\ true)
def delete(url, ids, commit) when is_binary(ids) or is_list(ids), do: Request.update(url, %Hui.U{delete_id: ids, commit: commit})

@spec delete!(binary | Hui.URL.t, binary | list(binary), boolean) :: HTTPoison.Response.t
def delete!(url, ids, commit \\ true)
def delete!(url, ids, commit) when is_binary(ids) or is_list(ids), do: Request.update(url, true, %Hui.U{delete_id: ids, commit: commit})
end
8 changes: 8 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ defmodule TestHelpers do
for x <- id, do: assert Enum.member? docs, x
end

def verify_docs_missing(url, id) do
ids = if is_list(id), do: Enum.join(id, " OR "), else: id
resp = Hui.search!(url, q: "*", fq: ["id:(#{ids})"])
assert resp.body["response"]["numFound"] == 0
docs = resp.body["response"]["docs"] |> Enum.map(&(Map.get(&1, "id")))
for x <- id, do: refute Enum.member? docs, x
end

end
45 changes: 45 additions & 0 deletions test/update_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ defmodule HuiUpdateLiveTest do
verify_docs_exist(:default, ["tt0062622"])
end

test "shoud delete documents by ID" do
url = %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "update", headers: [{"Content-type", "application/json"}]}
doc_map1 = %{
"actor_ss" => ["Toni Servillo", "Carlo Verdone", "Sabrina Ferilli"],
"desc" => "Jep Gambardella has seduced his way through the lavish nightlife of Rome for decades, but after his 65th birthday and a shock from the past, Jep looks past the nightclubs and parties to find a timeless landscape of absurd, exquisite beauty.",
"directed_by" => ["Paolo Sorrentino"],
"genre" => ["Drama"],
"id" => "tt2358891",
"initial_release_date" => "2013-09-06",
"name" => "La grande bellezza"
}
doc_map2 = %{
"actor_ss" => ["Jean-Louis Trintignant", "Emmanuelle Riva", "Isabelle Huppert"],
"desc" => "Georges and Anne are an octogenarian couple. They are cultivated, retired music teachers. Their daughter, also a musician, lives in Britain with her family. One day, Anne has a stroke, and the couple's bond of love is severely tested.",
"directed_by" => ["Michael Haneke"],
"genre" => ["Drama", "Romance"],
"id" => "tt1602620",
"initial_release_date" => "2012-11-16",
"name" => "Amour"
}
Hui.update(url, [doc_map1, doc_map2])
verify_docs_exist(:default, ["tt2358891", "tt1602620"])

Hui.delete(url, ["tt2358891", "tt1602620"])
verify_docs_missing(:default, ["tt2358891", "tt1602620"])
end

end

describe "update (live / bang)" do
Expand Down Expand Up @@ -134,6 +161,24 @@ defmodule HuiUpdateLiveTest do
verify_docs_exist(:default, ["9781910701874"])
end

test "shoud delete documents by ID" do
url = %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "update", headers: [{"Content-type", "application/json"}]}
doc_map1 = %{
"actor_ss" => ["Charlotte Rampling", "Charles Dance", "Ludivine Sagnier"],
"desc" => "A British mystery author visits her publisher's home in the South of France, where her interaction with his unusual daughter sets off some touchy dynamics.",
"directed_by" => ["François Ozon"],
"genre" => ["Drama", "Crime", "Mystery"],
"id" => "tt0324133",
"initial_release_date" => "2003-08-22",
"name" => "Swimming Pool"
}
Hui.update(url, doc_map1)
verify_docs_exist(:default, ["tt0324133"])

Hui.delete!(url, "tt0324133")
verify_docs_missing(:default, ["tt0324133"])
end

end

end
16 changes: 15 additions & 1 deletion test/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ defmodule HuiUpdateTest do
Hui.update(:update_test, update_doc)
end

test "should delete docs by ID", context do
url = %Hui.URL{url: "http://localhost:#{context.bypass.port}", handler: "update", headers: [{"Content-type", "application/json"}]}
expected_data = %Hui.U{delete_id: ["tt1650453", "tt1650453"], commit: true} |> Hui.U.encode
check_post_data_bypass_setup(context.bypass, expected_data)
Hui.delete(url, ["tt1650453", "tt1650453"])
end

test "should handle missing or malformed URL", context do
assert {:error, context.error_einval} == Hui.update(nil, context.update_doc)
assert {:error, context.error_einval} == Hui.update("", context.update_doc)
Expand Down Expand Up @@ -176,6 +183,13 @@ defmodule HuiUpdateTest do
assert resp.body == update_resp |> Poison.decode!
end

test "should delete docs by ID", context do
url = %Hui.URL{url: "http://localhost:#{context.bypass.port}", handler: "update", headers: [{"Content-type", "application/json"}]}
expected_data = %Hui.U{delete_id: ["tt1650453", "tt1650453"], commit: true} |> Hui.U.encode
check_post_data_bypass_setup(context.bypass, expected_data)
Hui.delete!(url, ["tt1650453", "tt1650453"])
end

test "should handle missing or malformed URL", context do
assert_raise Hui.Error, ":einval", fn -> Hui.update!(nil, context.update_doc) end
assert_raise Hui.Error, ":einval", fn -> Hui.update!("", context.update_doc) end
Expand All @@ -188,7 +202,7 @@ defmodule HuiUpdateTest do
assert_raise Hui.Error, ":einval", fn -> Hui.Request.update([], bang, context.update_doc) end
assert_raise Hui.Error, ":nxdomain", fn -> Hui.Request.update(:url_in_config, bang, context.update_doc) end
end

end

end

0 comments on commit 8d9915e

Please sign in to comment.