diff --git a/lib/hui.ex b/lib/hui.ex index 09dd39b..6a42727 100644 --- a/lib/hui.ex +++ b/lib/hui.ex @@ -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. @@ -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} diff --git a/test/update_live_test.exs b/test/update_live_test.exs index 7c656ef..1bc87aa 100644 --- a/test/update_live_test.exs +++ b/test/update_live_test.exs @@ -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 @@ -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 \ No newline at end of file diff --git a/test/update_test.exs b/test/update_test.exs index 989839f..3f06051 100644 --- a/test/update_test.exs +++ b/test/update_test.exs @@ -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 @@ -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