Skip to content

Commit

Permalink
Implemented multisearch endpoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertoledo committed Jul 2, 2024
1 parent 825a3b5 commit 27a7b72
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Typesense client for Elixir with support for your Ecto schemas.
## Todo

- creating collection using auto schema detection
- implement multisearch
- implement geosearch
- implement curation
- implement synonyms
Expand Down
2 changes: 2 additions & 0 deletions lib/ex_typesense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ defmodule ExTypesense do
# geo search

# multisearch
defdelegate multi_search(conn \\ Connection.new(), searches),
to: ExTypesense.Multisearch

# curation

Expand Down
88 changes: 88 additions & 0 deletions lib/ex_typesense/multisearch.ex
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
Loading

0 comments on commit 27a7b72

Please sign in to comment.