Skip to content

Commit

Permalink
enable commitWithin, overwrite parameters in struct Hui.U single doc …
Browse files Browse the repository at this point in the history
…update
  • Loading branch information
boonious committed Oct 5, 2018
1 parent 163fff9 commit 0b81527
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/hui/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule Hui.Request do
# Direct response, or exception in case of failture
bang = true
response = Hui.Request.update(url, bang, json_doc)
# Delete a document via XML message
headers = [{"Content-type", "application/xml"}]
url = %Hui.URL{url: "http://localhost:8983/solr/collection", handler: "update", headers: headers}
Expand Down
14 changes: 10 additions & 4 deletions lib/hui/u.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ defmodule Hui.U do
waitSearcher: boolean, expungeDeletes: boolean, maxSegments: integer,
delete_id: binary | list(binary), delete_query: binary | list(binary)}

def encode(%__MODULE__{} = s), do: "{#{encode(doc: s.doc)}}"
def encode(%__MODULE__{} = s), do: "{#{encode(doc: s.doc, within: s.commitWithin, overwrite: s.overwrite)}}"
def encode(doc) when is_map(doc), do: Poison.encode!(doc)

def encode(doc: nil), do: ""
def encode(doc: doc) when is_map(doc), do: "\"add\":{\"doc\":#{encode(doc)}}"
def encode(doc: [head|tail]) when is_map(head), do: Enum.map([head]++tail, &encode(doc: &1))
def encode(doc: doc, within: w, overwrite: o) when is_map(doc), do: "\"add\":{#{encode(within: w)}#{encode(overwrite: o)}\"doc\":#{encode(doc)}}"
def encode(doc: [h|t], within: w, overwrite: o) when is_map(h), do: Enum.map([h]++t, &encode(doc: &1, within: w, overwrite: o))
def encode(doc: _, within: _, overwrite: _), do: ""

def encode(within: w) when is_integer(w), do: "\"commitWithin\":#{w},"
def encode(within: _), do: ""

def encode(overwrite: o) when is_boolean(o), do: "\"overwrite\":#{o},"
def encode(overwrite: _), do: ""

end
1 change: 1 addition & 0 deletions test/data/delete_doc4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "delete": "tt0078748"}
1 change: 1 addition & 0 deletions test/data/update_doc4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"add":{"commitWithin":5000,"doc":{"name":"Alien","initial_release_date":"1979-06-22","id":"tt0078748","genre":["Sci-Fi","Horror"],"directed_by":["Ridley Scott"],"desc":"After a space merchant vessel perceives an unknown transmission as a distress call, its landing on the source moon finds one of the crew attacked by a mysterious lifeform, and they soon realize that its life cycle has merely begun.","actor_ss":["Tom Skerritt","Sigourney Weaver","Veronica Cartwright","Harry Dean Stanton"]}}}
1 change: 1 addition & 0 deletions test/data/update_doc5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"add":{"commitWithin":10,"overwrite":true,"doc":{"name":"Alien","initial_release_date":"1979-06-22","id":"tt0078748","genre":["Sci-Fi","Horror"],"directed_by":["Ridley Scott"],"desc":"After a space merchant vessel perceives an unknown transmission as a distress call, its landing on the source moon finds one of the crew attacked by a mysterious lifeform, and they soon realize that its life cycle has merely begun.","actor_ss":["Tom Skerritt","Sigourney Weaver","Veronica Cartwright","Harry Dean Stanton"]}}}
1 change: 1 addition & 0 deletions test/data/update_doc6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"add":{"overwrite":false,"doc":{"name":"Alien","initial_release_date":"1979-06-22","id":"tt0078748","genre":["Sci-Fi","Horror"],"directed_by":["Ridley Scott"],"desc":"After a space merchant vessel perceives an unknown transmission as a distress call, its landing on the source moon finds one of the crew attacked by a mysterious lifeform, and they soon realize that its life cycle has merely begun.","actor_ss":["Tom Skerritt","Sigourney Weaver","Veronica Cartwright","Harry Dean Stanton"]}}}
16 changes: 16 additions & 0 deletions test/struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,22 @@ defmodule HuiStructTest do
x = %Hui.U{doc: [doc_map1, doc_map2]}
assert Hui.U.encode(x) == File.read!("./test/data/update_doc3.json")
end

test "should encode doc with commitWithin and overwrite parameters" do
expected_data = File.read!("./test/data/update_doc4.json")
update_doc = expected_data |> Poison.decode!
doc_map = update_doc["add"]["doc"]

x = %Hui.U{doc: doc_map, commitWithin: 5000}
assert Hui.U.encode(x) == expected_data

x = %Hui.U{doc: doc_map, commitWithin: 10, overwrite: true}
assert Hui.U.encode(x) == File.read!("./test/data/update_doc5.json")

x = %Hui.U{doc: doc_map, overwrite: false}
assert Hui.U.encode(x) == File.read!("./test/data/update_doc6.json")
end

end

end
137 changes: 81 additions & 56 deletions test/struct_update_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,68 +1,93 @@
defmodule HuiStructUpdateLiveTest do
use ExUnit.Case, async: true

describe "Hui.U struct update (live)" do
describe "structured update via Hui.U (live)" do
@describetag live: false

test "should post a single doc" do
default_url = Hui.URL.default_url!
url = %Hui.URL{default_url | handler: "update", headers: [{"Content-type", "application/json"}]}
# change the following update calls from binary to struct-based later when 'delete', 'commit' ops exist
Hui.Request.update(url, File.read!("./test/data/delete_doc2.json"))
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:tt0083658"])
assert resp.body["response"]["numFound"] == 0

update_doc = File.read!("./test/data/update_doc2.json") |> Poison.decode!
doc_map = update_doc["add"]["doc"]
x = %Hui.U{doc: doc_map}

Hui.Request.update(url, x)
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:tt0083658"])

assert resp.body["response"]["numFound"] == 1
doc = resp.body["response"]["docs"] |> hd
assert doc["id"] == "tt0083658"
default_url = Hui.URL.default_url!
url = %Hui.URL{default_url | handler: "update", headers: [{"Content-type", "application/json"}]}
# change the following update calls from binary to struct-based later when 'delete', 'commit' ops exist
Hui.Request.update(url, File.read!("./test/data/delete_doc2.json"))
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:tt0083658"])
assert resp.body["response"]["numFound"] == 0

update_doc = File.read!("./test/data/update_doc2.json") |> Poison.decode!
doc_map = update_doc["add"]["doc"]
x = %Hui.U{doc: doc_map}

Hui.Request.update(url, x)
Hui.Request.update(url, File.read!("./test/data/commit.json"))

resp = Hui.search!(:default, q: "*", fq: ["id:tt0083658"])
assert resp.body["response"]["numFound"] == 1
doc = resp.body["response"]["docs"] |> hd
assert doc["id"] == "tt0083658"
end

test "should post a single doc with commitWithin and overwrite parameters" do
default_url = Hui.URL.default_url!
url = %Hui.URL{default_url | handler: "update", headers: [{"Content-type", "application/json"}]}
# change the following update calls from binary to struct-based later when 'delete', 'commit' ops exist
Hui.Request.update(url, File.read!("./test/data/delete_doc4.json"))
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:tt0078748"])
assert resp.body["response"]["numFound"] == 0

update_doc = File.read!("./test/data/update_doc5.json") |> Poison.decode!
doc_map = update_doc["add"]["doc"]
commitWithin = update_doc["add"]["commitWithin"]
overwrite = update_doc["add"]["overwrite"]
x = %Hui.U{doc: doc_map, commitWithin: commitWithin, overwrite: overwrite}

Hui.Request.update(url, x)
:timer.sleep(100)

resp = Hui.search!(:default, q: "*", fq: ["id:tt0078748"])
assert resp.body["response"]["numFound"] == 1
doc = resp.body["response"]["docs"] |> hd
assert doc["id"] == "tt0078748"
end

test "should post multiple docs" do
default_url = Hui.URL.default_url!
url = %Hui.URL{default_url | handler: "update", headers: [{"Content-type", "application/json"}]}
# change the following update calls from binary to struct-based later with 'delete', 'commit' ops exist
Hui.Request.update(url, File.read!("./test/data/delete_doc3.json"))
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:(tt1316540 OR tt1650453)"])
assert resp.body["response"]["numFound"] == 0

doc_map1 = %{
"actor_ss" => ["János Derzsi", "Erika Bók", "Mihály Kormos", "Ricsi"],
"desc" => "A rural farmer is forced to confront the mortality of his faithful horse.",
"directed_by" => ["Béla Tarr", "Ágnes Hranitzky"],
"genre" => ["Drama"],
"id" => "tt1316540",
"initial_release_date" => "2011-03-31",
"name" => "The Turin Horse"
}
doc_map2 = %{
"actor_ss" => ["Masami Nagasawa", "Hiroshi Abe", "Kanna Hashimoto",
"Yoshio Harada"],
"desc" => "Twelve-year-old Koichi, who has been separated from his brother Ryunosuke due to his parents' divorce, hears a rumor that the new bullet trains will precipitate a wish-granting miracle when they pass each other at top speed.",
"directed_by" => ["Hirokazu Koreeda"],
"genre" => ["Drame"],
"id" => "tt1650453",
"initial_release_date" => "2011-06-11",
"name" => "I Wish"
}
x = %Hui.U{doc: [doc_map1, doc_map2]}

Hui.Request.update(url, x)
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:(tt1316540 OR tt1650453)"])
assert resp.body["response"]["numFound"] == 2
docs = resp.body["response"]["docs"] |> Enum.map(&(Map.get(&1, "id")))
assert Enum.member? docs, "tt1316540"
assert Enum.member? docs, "tt1650453"
default_url = Hui.URL.default_url!
url = %Hui.URL{default_url | handler: "update", headers: [{"Content-type", "application/json"}]}
# change the following update calls from binary to struct-based later with 'delete', 'commit' ops exist
Hui.Request.update(url, File.read!("./test/data/delete_doc3.json"))
Hui.Request.update(url, File.read!("./test/data/commit.json"))
resp = Hui.search!(:default, q: "*", fq: ["id:(tt1316540 OR tt1650453)"])
assert resp.body["response"]["numFound"] == 0

doc_map1 = %{
"actor_ss" => ["János Derzsi", "Erika Bók", "Mihály Kormos", "Ricsi"],
"desc" => "A rural farmer is forced to confront the mortality of his faithful horse.",
"directed_by" => ["Béla Tarr", "Ágnes Hranitzky"],
"genre" => ["Drama"],
"id" => "tt1316540",
"initial_release_date" => "2011-03-31",
"name" => "The Turin Horse"
}
doc_map2 = %{
"actor_ss" => ["Masami Nagasawa", "Hiroshi Abe", "Kanna Hashimoto",
"Yoshio Harada"],
"desc" => "Twelve-year-old Koichi, who has been separated from his brother Ryunosuke due to his parents' divorce, hears a rumor that the new bullet trains will precipitate a wish-granting miracle when they pass each other at top speed.",
"directed_by" => ["Hirokazu Koreeda"],
"genre" => ["Drame"],
"id" => "tt1650453",
"initial_release_date" => "2011-06-11",
"name" => "I Wish"
}
x = %Hui.U{doc: [doc_map1, doc_map2]}

Hui.Request.update(url, x)
Hui.Request.update(url, File.read!("./test/data/commit.json"))

resp = Hui.search!(:default, q: "*", fq: ["id:(tt1316540 OR tt1650453)"])
assert resp.body["response"]["numFound"] == 2
docs = resp.body["response"]["docs"] |> Enum.map(&(Map.get(&1, "id")))
assert Enum.member? docs, "tt1316540"
assert Enum.member? docs, "tt1650453"
end

end
Expand Down
15 changes: 15 additions & 0 deletions test/struct_update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ defmodule HuiStructUpdateTest do
Hui.Request.update(:update_struct_test, x)
end

test "update should post doc with commitWithin and overwrite parameters", context do
url = %Hui.URL{url: "http://localhost:#{context.bypass.port}", handler: "update", headers: [{"Content-type", "application/json"}]}
expected_data = File.read!("./test/data/update_doc5.json")

update_doc = expected_data |> Poison.decode!
doc_map = update_doc["add"]["doc"]
commitWithin = update_doc["add"]["commitWithin"]
overwrite = update_doc["add"]["overwrite"]

check_post_data_bypass_setup(context.bypass, expected_data)

x = %Hui.U{doc: doc_map, commitWithin: commitWithin, overwrite: overwrite}
Hui.Request.update(url, x)
end

end

end

0 comments on commit 0b81527

Please sign in to comment.