Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table.Reader Support #93

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/snap/responses/search_response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,31 @@ defmodule Snap.SearchResponse do
def slice(_response), do: {:error, __MODULE__}
end
end

if Code.ensure_loaded?(Table.Reader) do
defimpl Table.Reader, for: Snap.SearchResponse do
def init(result) do
columns = get_columns(result)
rows = get_rows(result)
{:rows, %{columns: columns}, rows}
end

defp get_columns(response) do
if response.hits.hits |> Enum.empty?() do
[]
else
hits = response.hits.hits
hits |> List.first() |> Map.fetch!(:source) |> Map.keys()
end
end

defp get_rows(response) do
hits = response.hits.hits

hits
|> Enum.map(fn hit ->
hit.source |> Map.values()
end)
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule Snap.MixProject do
defp deps do
[
{:finch, "~> 0.8", optional: true},
{:table, "~> 0.1.2"},
{:castore, "~> 1.0"},
{:jason, "~> 1.0"},
{:telemetry, "~> 1.0 or ~> 0.4"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"nimble_pool": {:hex, :nimble_pool, "1.0.0", "5eb82705d138f4dd4423f69ceb19ac667b3b492ae570c9f5c900bb3d2f50a847", [:mix], [], "hexpm", "80be3b882d2d351882256087078e1b1952a28bf98d0a287be87e4a24a710b67a"},
"table": {:hex, :table, "0.1.2", "87ad1125f5b70c5dea0307aa633194083eb5182ec537efc94e96af08937e14a8", [:mix], [], "hexpm", "7e99bc7efef806315c7e65640724bf165c3061cdc5d854060f74468367065029"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}
4 changes: 4 additions & 0 deletions test/search_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ defmodule Snap.SearchTest do
first_hit = Enum.at(search_response, 0)
assert first_hit.id == "1"
assert first_hit.source["title"] == "Document 1"

table_reader = Table.to_rows(search_response)
# the number of documents we created
assert table_reader |> Enum.count() == 5
end

test "search with suggestion response" do
Expand Down