Skip to content

Commit

Permalink
struct update Hui.U: implement optimize, waitSearcher, maxSegments up…
Browse files Browse the repository at this point in the history
…date command
  • Loading branch information
boonious committed Oct 6, 2018
1 parent 36dd2eb commit ccd8189
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
9 changes: 8 additions & 1 deletion lib/hui/u.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ defmodule Hui.U do
def encode(%__MODULE__{} = s) do
a = "#{encode(doc: s.doc, within: s.commitWithin, overwrite: s.overwrite)}"
b = "#{encode(commit: s.commit, wait: s.waitSearcher, expunge: s.expungeDeletes)}"
c = "#{encode(optimize: s.optimize, wait: s.waitSearcher, max: s.maxSegments)}"

x = [a, b] |> Enum.filter(fn x -> x != "" end)
x = [a, b, c] |> Enum.filter(fn x -> x != "" end)
"{#{Enum.join(x, ",")}}"
end
def encode(doc) when is_map(doc), do: Poison.encode!(doc)
Expand All @@ -40,4 +41,10 @@ defmodule Hui.U do
def encode(commit: true, wait: nil, expunge: nil), do: "\"commit\":{}"
def encode(commit: _, wait: _, expunge: _), do: ""

def encode(optimize: true, wait: w, max: m) when is_boolean(w) and is_integer(m), do: "\"optimize\":{\"waitSearcher\":#{w},\"maxSegments\":#{m}}"
def encode(optimize: true, wait: w, max: nil) when is_boolean(w), do: "\"optimize\":{\"waitSearcher\":#{w}}"
def encode(optimize: true, wait: nil, max: m) when is_integer(m), do: "\"optimize\":{\"maxSegments\":#{m}}"
def encode(optimize: true, wait: nil, max: nil), do: "\"optimize\":{}"
def encode(optimize: _, wait: _, max: _), do: ""

end
1 change: 1 addition & 0 deletions test/data/update_doc10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"add":{"commitWithin":50,"overwrite":true,"doc":{"name":"Autumn Sonata","initial_release_date":"1978-10-08","id":"tt0077711","genre":["Drama","Music"],"directed_by":["Ingmar Bergman"],"desc":"A married daughter who longs for her mother's love is visited by the latter, a successful concert pianist.","actor_ss":["Ingrid Bergman","Liv Ullmann","Lena Nyman","Halvar Björk"]}},"add":{"commitWithin":50,"overwrite":true,"doc":{"name":"Persona","initial_release_date":"1967-09-21","id":"tt0060827","genre":["Drama","Thriller"],"directed_by":["Ingmar Bergman"],"desc":"A nurse is put in charge of a mute actress and finds that their personas are melding together.","actor_ss":["Bibi Andersson","Liv Ullmann","Margaretha Krook"]}},"commit":{"waitSearcher":true,"expungeDeletes":false},"optimize":{"waitSearcher":true,"maxSegments":20}}
25 changes: 21 additions & 4 deletions test/struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ defmodule HuiStructTest do
assert Hui.U.encode(x) == expected_data
end

test "should encode commit message with waitSearcher and expungeDeletes parameters" do
test "should encode commit command with waitSearcher and expungeDeletes parameters" do
x = %Hui.U{commit: true}
assert x |> Hui.U.encode == "{\"commit\":{}}"

Expand All @@ -632,7 +632,24 @@ defmodule HuiStructTest do
assert x |> Hui.U.encode == "{\"commit\":{\"waitSearcher\":true,\"expungeDeletes\":false}}"
end

test "should encode multiple bundled update commands (docs, optimize etc.)" do
test "should encode optimize command with waitSearcher and maxSegment parameters" do
x = %Hui.U{optimize: true}
assert x |> Hui.U.encode == "{\"optimize\":{}}"

x = %Hui.U{optimize: true, waitSearcher: true}
assert x |> Hui.U.encode == "{\"optimize\":{\"waitSearcher\":true}}"

x = %Hui.U{optimize: true, waitSearcher: false}
assert x |> Hui.U.encode == "{\"optimize\":{\"waitSearcher\":false}}"

x = %Hui.U{optimize: true, maxSegments: 20}
assert x |> Hui.U.encode == "{\"optimize\":{\"maxSegments\":20}}"

x = %Hui.U{optimize: true, waitSearcher: true, maxSegments: 20}
assert x |> Hui.U.encode == "{\"optimize\":{\"waitSearcher\":true,\"maxSegments\":20}}"
end

test "should encode multiple bundled update commands (docs, commit, optimize etc.)" do
doc_map1 = %{
"actor_ss" => ["Ingrid Bergman", "Liv Ullmann", "Lena Nyman", "Halvar Björk"],
"desc" => "A married daughter who longs for her mother's love is visited by the latter, a successful concert pianist.",
Expand All @@ -653,8 +670,8 @@ defmodule HuiStructTest do
}

x = %Hui.U{doc: [doc_map1, doc_map2], commitWithin: 50, overwrite: true}
x = %Hui.U{x | commit: true, waitSearcher: true, expungeDeletes: false}
assert x |> Hui.U.encode == File.read!("./test/data/update_doc9.json")
x = %Hui.U{x | commit: true, waitSearcher: true, expungeDeletes: false, optimize: true, maxSegments: 20}
assert x |> Hui.U.encode == File.read!("./test/data/update_doc10.json")
end

end
Expand Down

0 comments on commit ccd8189

Please sign in to comment.