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

POST request support for search method #33

Open
wants to merge 2 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
erl_crash.dump
*.ez
search-*.tar
.elixir_ls
.elixir_ls
.env
3 changes: 3 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
config :logger,
backends: [:console],
compile_time_purge_level: :debug
12 changes: 11 additions & 1 deletion lib/algolia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ defmodule Algolia do

path = Paths.search(index, query, opts)

send_request(:read, %{method: :get, path: path, options: request_options})
{request_method, opts} = Keyword.pop(opts, :request_method)

request = if request_method == :post do
%{method: :post, path: path, body: opts ++ [query: query]
|> Enum.into(%{})
|> Jason.encode!()}
else
%{method: :get, path: path, options: request_options}
end

send_request(:read, request)
end

@doc """
Expand Down
8 changes: 6 additions & 2 deletions lib/algolia/paths.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ defmodule Algolia.Paths do
end

def search(index, query, opts) do
params = Keyword.put(opts, :query, query)
index(index) <> to_query(params)
if opts[:request_method] != :post do
params = Keyword.put(opts, :query, query)
index(index) <> to_query(params)
else
index(index) <> "/query"
end
end

def search_facet(index, facet) do
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Algolia.Mixfile do
def project do
[
app: :algolia,
version: "0.8.0",
version: "0.8.1",
description: "Elixir implementation of Algolia Search API",
elixir: "~> 1.5",
package: package(),
Expand All @@ -27,7 +27,7 @@ defmodule Algolia.Mixfile do

defp deps do
[
{:hackney, "~> 1.9 or ~> 1.10"},
{:hackney, "~> 1.15 and >= 1.15.2"},
{:jason, "~> 1.0"},
# Docs
{:ex_doc, "~> 0.19", only: :dev},
Expand Down
13 changes: 7 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
%{
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [:rebar3], [], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.3.0", "17f0c38eaafb4800f746b457313af4b2442a8c2405b49c645768680f900be603", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.9.0", "51c506afc0a365868469dcfc79a9d0b94d896ec741cfd5bd338f49a5ec515bfe", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.0.0", "0f7cfa9bdb23fed721ec05419bcee2b2c21a77e926bce0deda029b5adc716fe2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}
49 changes: 49 additions & 0 deletions test/algolia_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ defmodule AlgoliaTest do
assert length(hits1) === count
end

test "search single index as POST" do
:rand.seed(:exs1024, :erlang.timestamp)
count = :rand.uniform 10
docs = Enum.map(1..count, &(%{id: &1,
objectID: &1,
test: "search_single_index"}))

{:ok, _} = save_objects("test_3", docs, id_attribute: :id) |> wait

{:ok, %{"hits" => hits1}} = search("test_3",
"search_single_index",
[request_method: :post])
assert length(hits1) === count
end

test "search with list opts" do
:rand.seed(:exs1024, :erlang.timestamp)
count = :rand.uniform 10
Expand All @@ -114,6 +129,24 @@ defmodule AlgoliaTest do
refute response["page"]
end

test "search with list opts as POST" do
:rand.seed(:exs1024, :erlang.timestamp)
count = :rand.uniform 10
docs = Enum.map(1..count, &(%{id: &1, test: "search with list opts"}))

{:ok, _} = save_objects("test_3", docs, id_attribute: :id) |> wait

opts = [
responseFields: ["hits", "nbPages"],
request_method: :post
]
{:ok, response} = search("test_3", "search_with_list_opts", opts)

assert response["hits"]
assert response["nbPages"]
refute response["page"]
end

test "search > 1 pages" do
docs = Enum.map(1..40, &(%{id: &1, test: "search_more_than_one_pages"}))

Expand All @@ -126,6 +159,22 @@ defmodule AlgoliaTest do
assert length(hits) === 20
end

test "search > 1 pages as POST" do
docs = Enum.map(1..40, &(%{id: &1, test: "search_more_than_one_pages"}))

{:ok, _} = save_objects("test_3", docs, id_attribute: :id) |> wait

{:ok, %{"hits" => hits, "page" => page}} =
search("test_3",
"search_more_than_one_pages",
[page: 1,
request_method: :post])

assert page == 1
assert length(hits) === 20
end


test "search multiple indexes" do
:rand.seed(:exs1024, :erlang.timestamp)

Expand Down