Skip to content

Commit

Permalink
implement delete_by_query/3, delete_by_query!/3 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
boonious committed Oct 9, 2018
1 parent 0253a91 commit 1df5c2d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/hui.ex
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ defmodule Hui do
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})

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

@spec delete_by_query!(binary | Hui.URL.t, binary | list(binary), boolean) :: HTTPoison.Response.t
def delete_by_query!(url, queries, commit \\ true)
def delete_by_query!(url, queries, commit) when is_binary(queries) or is_list(queries), do: Request.update(url, %Hui.U{delete_query: queries, commit: commit})

@doc """
Commit any added or deleted Solr documents to the index.
Expand All @@ -460,7 +468,7 @@ defmodule Hui do
Hui.commit(url, false) # commits op only, new docs to be made available later
```
Use `Hui.Request.update/3` for other types of commit, e.g. expunge deleted docs to
Use `Hui.Request.update/3` for other types of commit and index optimisation, e.g. expunge deleted docs to
physically remove docs from the index, which could be a system-intensive operation.
"""
@spec commit(binary | Hui.URL.t, boolean) :: {:ok, HTTPoison.Response.t} | {:error, Hui.Error.t}
Expand Down
35 changes: 35 additions & 0 deletions test/update_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ defmodule HuiUpdateLiveTest do
verify_docs_missing(:default, ["tt2358891", "tt1602620"])
end

test "shoud delete documents by query" do
url = %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "update", headers: [{"Content-type", "application/json"}]}
doc_map1 = %{
"actor_ss" => ["Guy Pearce", "Carrie-Anne Moss"],
"desc" => "A man with short-term memory loss attempts to track down his wife's murderer.",
"directed_by" => ["Christopher Nolan"],
"genre" => ["Mystery", "Thriller"],
"id" => "tt0209144",
"initial_release_date" => "2000-10-20",
"name" => "Memento"
}
Hui.update(url, doc_map1)
verify_docs_exist(:default, ["tt0209144"])

Hui.delete_by_query(url, "name:Memento")
verify_docs_missing(:default, ["tt0209144"])
end

end

describe "update (live / bang)" do
Expand Down Expand Up @@ -179,6 +197,23 @@ defmodule HuiUpdateLiveTest do
verify_docs_missing(:default, ["tt0324133"])
end

test "shoud delete documents by query" do
url = %Hui.URL{url: "http://localhost:8983/solr/gettingstarted", handler: "update", headers: [{"Content-type", "application/json"}]}
doc_map1 = %{
"actor_ss" => ["Leonardo DiCaprio", "Joseph Gordon-Levitt", "Ellen Page"],
"desc" => "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a CEO.",
"directed_by" => ["Christopher Nolan"],
"genre" => ["Action", "Adventure", "Sci-Fi"],
"id" => "tt1375666",
"initial_release_date" => "2010-07-16",
"name" => "Inception"
}
Hui.update(url, doc_map1)
verify_docs_exist(:default, ["tt1375666"])

Hui.delete_by_query!(url, "name:Inception")
verify_docs_missing(:default, ["tt1375666"])
end
end

end
14 changes: 14 additions & 0 deletions test/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ defmodule HuiUpdateTest do
Hui.delete(url, ["tt1650453", "tt1650453"])
end

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

test "should commit docs", context do
url = %Hui.URL{url: "http://localhost:#{context.bypass.port}", handler: "update", headers: [{"Content-type", "application/json"}]}
expected_data = %Hui.U{commit: true, waitSearcher: true} |> Hui.U.encode
Expand Down Expand Up @@ -197,6 +204,13 @@ defmodule HuiUpdateTest do
Hui.delete!(url, ["tt1650453", "tt1650453"])
end

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

test "should commit docs", context do
url = %Hui.URL{url: "http://localhost:#{context.bypass.port}", handler: "update", headers: [{"Content-type", "application/json"}]}
expected_data = %Hui.U{commit: true, waitSearcher: true} |> Hui.U.encode
Expand Down

0 comments on commit 1df5c2d

Please sign in to comment.