Skip to content

Commit

Permalink
doctests and documentation for the JSON encoder of Hui.U update struct
Browse files Browse the repository at this point in the history
  • Loading branch information
boonious committed Oct 8, 2018
1 parent cb9ad3b commit 3cf0d5f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/hui/u.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,74 @@ defmodule Hui.U do
waitSearcher: boolean, expungeDeletes: boolean, maxSegments: integer,
delete_id: binary | list(binary), delete_query: binary | list(binary)}

@doc """
Encodes the `Hui.U.t` module struct into Solr binary commands for JSON-formatted update.
## Example
```
# Update / index 2 documents, commit them within 1s
iex> doc1 = %{"name" => "The Turin Horse", "directed_by" => ["Béla Tarr"], "genre" => ["Drama"], "id" => "tt1316540"}
%{
"directed_by" => ["Béla Tarr"],
"genre" => ["Drama"],
"id" => "tt1316540",
"name" => "The Turin Horse"
}
iex> doc2 = %{"name" => "I Wish", "directed_by" => ["Hirokazu Koreeda"], "genre" => ["Drama"], "id" => "tt1650453"}
%{
"directed_by" => ["Hirokazu Koreeda"],
"genre" => ["Drama"],
"id" => "tt1650453",
"name" => "I Wish"
}
iex> x = %Hui.U{doc: [doc1, doc2], commit: true, commitWithin: 1000}
%Hui.U{
commit: true,
commitWithin: 1000,
delete_id: nil,
delete_query: nil,
doc: [
%{
"directed_by" => ["Béla Tarr"],
"genre" => ["Drama"],
"id" => "tt1316540",
"name" => "The Turin Horse"
},
%{
"directed_by" => ["Hirokazu Koreeda"],
"genre" => ["Drama"],
"id" => "tt1650453",
"name" => "I Wish"
}
],
expungeDeletes: nil,
maxSegments: nil,
optimize: nil,
overwrite: nil,
rollback: nil,
waitSearcher: nil
}
iex> x |> Hui.U.encode
"{\\\"add\\\":{\\\"commitWithin\\\":1000,\\\"doc\\\":{\\\"name\\\":\\\"The Turin Horse\\\",\\\"id\\\":\\\"tt1316540\\\",\\\"genre\\\":[\\\"Drama\\\"],\\\"directed_by\\\":[\\\"Béla Tarr\\\"]}},\\\"add\\\":{\\\"commitWithin\\\":1000,\\\"doc\\\":{\\\"name\\\":\\\"I Wish\\\",\\\"id\\\":\\\"tt1650453\\\",\\\"genre\\\":[\\\"Drama\\\"],\\\"directed_by\\\":[\\\"Hirokazu Koreeda\\\"]}},\\\"commit\\\":{}}"
# Delete the documents by ID
iex> %Hui.U{delete_id: ["tt1316540", "tt1650453"]} |> Hui.U.encode
"{\\\"delete\\\":{\\\"id\\\":\\\"tt1316540\\\"},\\\"delete\\\":{\\\"id\\\":\\\"tt1650453\\\"}}"
# Delete the documents by filter query
iex> %Hui.U{delete_query: "id:tt*"} |> Hui.U.encode
"{\\\"delete\\\":{\\\"query\\\":\\\"id:tt*\\\"}}"
# Commits the docs, make them visible and remove previously deleted docs from the index
iex> %Hui.U{commit: true, waitSearcher: true, expungeDeletes: true} |> Hui.U.encode
"{\\\"commit\\\":{\\\"waitSearcher\\\":true,\\\"expungeDeletes\\\":true}}"
# Optimise the index, and keep the number of index segments 10 max
iex> %Hui.U{optimize: true, maxSegments: 10} |> Hui.U.encode
"{\\\"optimize\\\":{\\\"maxSegments\\\":10}}"
```
"""
@spec encode(Hui.U.t) :: binary
def encode(%__MODULE__{} = s) do
a = "#{_encode(doc: s.doc, within: s.commitWithin, overwrite: s.overwrite)}"
b = "#{_encode(delete_id: s.delete_id)}"
Expand Down
1 change: 1 addition & 0 deletions test/struct_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule HuiStructTest do
doctest Hui.F.Range
doctest Hui.F.Interval
doctest Hui.H
doctest Hui.U

describe "query struct Hui.Q" do

Expand Down

0 comments on commit 3cf0d5f

Please sign in to comment.