ex_typesense

Typesense client for Elixir with support for importing your Ecto schemas.


Keywords
ecto, ecto-schemas, elixir, typesense
License
MIT

Documentation

ExTypesense

Hex.pm Hexdocs.pm Hex.pm Typesense badge

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

Installation

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"}
  ]
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

Getting started

1. Spin up local typesense server

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

2. Add creds to config

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"

3. Create a collection

using Ecto

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([:id, :person_id, :name, :country])
      |> Enum.map(fn {key, val} ->
        cond do
          key === :id -> {key, to_string(Map.get(value, :id))}
          key === :person_id -> {key, Map.get(value, :id)}
          true -> {key, val}
        end
      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)

using maps

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)

4. Indexing documents

4.a via indexing multiple documents

Post |> Repo.all() |> ExTypesense.index_multiple_documents()

4.b or indexing single document

Post |> Repo.get!(123) |> ExTypesense.create_document()

5. Search

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.