-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented multisearch endpoint support
- Loading branch information
1 parent
825a3b5
commit 27a7b72
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule ExTypesense.Multisearch do | ||
@moduledoc since: "0.4.3" | ||
@moduledoc """ | ||
Module for performing multiple searches at once. This module is also convenient for vector searches, | ||
as it works over POST and allows bigger payloads than GET requests. | ||
""" | ||
|
||
alias ExTypesense.Connection | ||
alias ExTypesense.HttpClient | ||
|
||
|
||
@multi_search_path "/multi_search" | ||
@type response :: {:ok, map()} | {:error, map()} | ||
|
||
@doc """ | ||
Perform multiple searches at once. | ||
## Examples | ||
iex> searches = [ | ||
...> %{collection: "companies", params: %{q: "umbrella"}}, | ||
...> %{collection: "companies", params: %{q: "rain"}}, | ||
...> %{collection: "companies", params: %{q: "sun"}} | ||
...> ] | ||
iex> ExTypesense.multi_search(searches) | ||
{:ok, | ||
[ | ||
%{ | ||
"facet_counts" => [], | ||
"found" => 0, | ||
"hits" => [], | ||
"out_of" => 0, | ||
"page" => 1, | ||
"request_params" => %{ | ||
"collection_name" => "companies", | ||
"per_page" => 10, | ||
"q" => "umbrella" | ||
}, | ||
"search_cutoff" => false, | ||
"search_time_ms" => 5 | ||
}, | ||
%{ | ||
"facet_counts" => [], | ||
"found" => 0, | ||
"hits" => [], | ||
"out_of" => 0, | ||
"page" => 1, | ||
"request_params" => %{ | ||
"collection_name" => "companies", | ||
"per_page" => 10, | ||
"q" => "rain" | ||
}, | ||
"search_cutoff" => false, | ||
"search_time_ms" => 5 | ||
}, | ||
%{ | ||
"facet_counts" => [], | ||
"found" => 0, | ||
"hits" => [], | ||
"out_of" => 0, | ||
"page" => 1, | ||
"request_params" => %{ | ||
"collection_name" => "companies", | ||
"per_page" => 10, | ||
"q" => "sun" | ||
}, | ||
"search_cutoff" => false, | ||
"search_time_ms" => 5 | ||
} | ||
] | ||
} | ||
""" | ||
@doc since: "0.4.3" | ||
@spec multi_search(Connection.t(), [map()]) :: response() | ||
def multi_search(conn, searches) do | ||
path = @multi_search_path | ||
|
||
response = HttpClient.request( | ||
conn, | ||
%{ | ||
method: :post, | ||
path: path, | ||
body: Jason.encode!(%{searches: searches}) | ||
} | ||
) | ||
|
||
response | ||
end | ||
end |
Oops, something went wrong.