View Source Cheatsheet Guide

Collections

Create using schema

iex> ExTypesense.create_collection(Company)
{:ok,
  %ExTypesense.Collection{
    "created_at" => 1234567890,
    "default_sorting_field" => "companies_id",
    "fields" => [...],
    "name" => "companies",
    "num_documents" => 0,
    "symbols_to_index" => [],
    "token_separators" => []
  }
}

Create using map

iex> schema =
...> %{
...>   name: "companies",
...>   fields: [
...>     %{name: "company_name", type: "string"},
...>     %{name: "num_employees", type: "int32"},
...>     %{name: "country", type: "string", facet: true}
...>   ],
...>   default_sorting_field: "num_employees"
...> }

iex> ExTypesense.create_collection(schema)
{:ok,
  %ExTypesense.Collection{
    "created_at" => 1234567890,
    "default_sorting_field" => "num_employees",
    "fields" => [...],
    "name" => "companies",
    "num_documents" => 0,
    "symbols_to_index" => [],
    "token_separators" => []
  }
}

List collections

iex> ExTypesense.list_collections()

Update a collection

iex> schema =
...> %{
...>   fields: [
...>     %{name: "num_employees", drop: true},
...>     %{name: "company_category", type: "string"},
...>   ],
...> }

iex> ExTypesense.update_collection("companies", schema)
{:ok,
  %ExTypesense.Collection{
    "created_at" => nil,
    "name" => nil,
    "default_sorting_field" => nil,
    "fields" => [...],
    "num_documents" => 0,
    "symbols_to_index" => [],
    "token_separators" => []
  }
}

Documents

Index a document from an Ecto schema

iex> post = Post |> limit(1) |> Repo.one()

iex> ExTypesense.create_document(post, :create)
{:ok,
  %{
    "id" => "12",
    "post_id" => 12,
    "title" => "the quick brown fox",
    "collection_name" => "posts"
  }
}

Update a document

iex> post = Post |> limit(1) |> Repo.one()

iex> ExTypesense.update_document(post, 0)
{:ok,
  %{
    "id" => "0",
    "collection_name" => "posts",
    "post_id" => 34,
    "title" => "test",
    "description" => "lorem ipsum"
  }
}

Delete a document

iex> ExTypesense.delete_document(Post, 0)
{:ok,
  %{
    "id" => "0",
    "collection_name" => "posts",
    "post_id" => 34,
    "title" => "test",
    "description" => "lorem ipsum"
  }
}

Indexes multiple documents

iex> posts = Post |> Repo.all()
iex> ExTypesense.index_multiple_documents(posts, :post_id, :upsert)
{:ok, [%{"success" => true}, %{"success" => true}]}
iex> params = %{q: "test", query_by: "title"}

iex> ExTypesense.search(Post, params)
{:ok,
  %{
    "id" => "0",
    "collection_name" => "posts",
    "post_id" => 34,
    "title" => "test",
    "description" => "lorem ipsum"
  }
}