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

Adding browsing and get_objects #13

Open
wants to merge 4 commits into
base: master
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
43 changes: 40 additions & 3 deletions lib/algolia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ defmodule Algolia do
send_request(:read, :get, path)
end

# Browse all index content
# {:ok, %{"hits" => hits, "cursor" => cursor}} =
# Algolia.browse(index_name, query: "search", hitsPerPage: 2)
# …
# Algolia.browse_from(index_name, cursor)
def browse(index, params \\ []) do
path = index <> "/browse?" <> URI.encode_query(params)
send_request(:read, :get, path)
end

def browse_from(index, cursor) do
opts = [cursor: cursor]
path = index <> "/browse?" <> URI.encode_query(opts)
send_request(:read, :get, path)
end

defp send_request(read_or_write, method, path) do
send_request(read_or_write, method, path, "", 0)
end
Expand All @@ -107,15 +123,24 @@ defmodule Algolia do
{:error, "Unable to connect to Algolia"}
end
defp send_request(read_or_write, method, path, body, curr_retry) do
proto_host =
case Application.get_env(:algolia, :api_endpoint) do
nil ->
"https://"
|> Path.join(host(read_or_write, curr_retry))
endpoint ->
endpoint
end

url =
"https://"
|> Path.join(host(read_or_write, curr_retry))
proto_host
|> Path.join("/1/indexes")
|> Path.join(path)

headers = [
"X-Algolia-API-Key": api_key(),
"X-Algolia-Application-Id": application_id()
"X-Algolia-Application-Id": application_id(),
"Content-Type": "application/json; charset=UTF-8"
]

:hackney.request(method, url, headers, body, [
Expand Down Expand Up @@ -145,6 +170,18 @@ defmodule Algolia do
|> inject_index_into_response(index)
end

@doc """
Get multiple objects, potentially from different indices

Algolia.get_objects([indexName: "…", objectID: "…"])
"""
def get_objects(objects) do
body = %{ requests: objects } |> Poison.encode!
path = "*/objects"

send_request(:read, :post, path, body)
end

@doc """
Add an Object
"""
Expand Down
36 changes: 30 additions & 6 deletions test/algolia_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule AlgoliaTest do
"test", "test_1", "test_2", "test_3",
"multi_test_1", "multi_test_2",
"move_index_test_src", "move_index_test_dst",
"copy_index_src", "copy_index_dst"
"copy_index_src", "copy_index_dst",
"browse_test_index"
]

@settings_test_index "settings_test"
Expand Down Expand Up @@ -35,13 +36,15 @@ defmodule AlgoliaTest do
test "add multiple objects" do
assert {:ok, %{"objectIDs" => ids}} =
"test_1"
|> add_objects([%{text: "add multiple test"}, %{text: "add multiple test"}, %{text: "add multiple test"}])
|> add_objects([%{text: "first"}, %{text: "second"}, %{text: "third"}])
|> wait

for id <- ids do
assert {:ok, %{"text" => "add multiple test"}} =
get_object("test_1", id)
end
{:ok, %{"results" => objects}} =
ids
|> Enum.map(fn id -> %{indexName: "test_1", objectID: id} end)
|> Algolia.get_objects()

assert Enum.map(objects, &(&1["text"])) == ~w(first second third)
end

test "list all indexes" do
Expand Down Expand Up @@ -281,4 +284,25 @@ defmodule AlgoliaTest do
all_indexes = Enum.map(items, & &1["name"])
refute index in all_indexes
end

test "browsing an index" do
index = "browse_test_index"

assert {:ok, _} =
index
|> set_settings(%{customRanking: ["asc(order)"]})
|> wait()

assert {:ok, _} =
index
|> add_objects([%{order: 0, text: "first"}, %{order: 1, text: "second"}, %{order: 2, text: "third"}])
|> wait()

assert {:ok, %{"hits" => [%{"text" => "first"}], "cursor" => cursor}}
= browse(index, hitsPerPage: 1, attributesToRetrieve: "text")
assert {:ok, %{"hits" => [%{"text" => "second"}], "cursor" => cursor}}
= browse_from(index, cursor)
assert {:ok, %{"hits" => [%{"text" => "third"}]}}
= browse_from(index, cursor)
end
end