Typesense client for Elixir with support for your Ecto schemas.
- creating collection using auto schema detection
- implement multisearch
- implement geosearch
- implement curation
- implement synonyms
If available in Hex, the package can be installed
by adding ex_typesense
to your list of dependencies in mix.exs
:
def deps do
[
{:ex_typesense, "~> 0.3.0"}
]
end
If you're adding this dep as a local path:
def deps do
[
{:ex_typesense, path: "/path/to/ex_typesense"}
]
end
then
mix deps.compile ex_typesense --force
docker container run --rm -it -d \
--name typesense \
-e TYPESENSE_DATA_DIR=/data \
-e TYPESENSE_API_KEY=xyz \
-v /tmp/typesense-server-data:/data \
-p 8108:8108 \
typesense/typesense:0.24.1
Config for setting up api key, host, etc.
You can also find api key and host in your dashboard if you're using cloud-hosted Typesense.
config :ex_typesense,
api_key: "xyz",
host: "localhost", # "111222333aaabbbcc-9.x9.typesense.net"
port: 8108, # 443
scheme: "http" # "https"
In this example, we're adding person_id
that points to the id of persons
schema.
defmodule Person do
use Ecto.Schema
defimpl Jason.Encoder, for: __MODULE__ do
def encode(value, opts) do
value
|> Map.take([:person_id, :name, :country])
|> Enum.map(fn {key, val} ->
if key === :person_id, do: {key, Map.get(value, :id)}, else: {key, val}
end)
|> Enum.into(%{})
|> Jason.Encode.map(opts)
end
end
schema "persons" do
field(:name, :string)
field(:country, :string)
field(:person_id, :integer, virtual: true)
end
def get_field_types do
%{
default_sorting_field: "person_id",
fields: [
%{name: "person_id", type: "int32"},
%{name: "name", type: "string"},
%{name: "country", type: "string"}
]
}
end
end
next, create the collection from a module name
ExTypesense.create_collection(Person)
schema = %{
name: "companies",
fields: [
%{name: "company_name", type: "string"},
%{name: "company_id", type: "int32"},
%{name: "country", type: "string"}
],
default_sorting_field: "company_id"
}
ExTypesense.create_collection(schema)
Post |> Repo.all() |> ExTypesense.index_multiple_documents()
Post |> Repo.get!(123) |> ExTypesense.create_document()
params = %{q: "John Doe", query_by: "name"}
ExTypesense.search(schema.name, params)
ExTypesense.search(Person, params)
Check cheatsheet for more examples
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ex_typesense.