View Source ExTypesense.Collection (ExTypesense v0.2.2)

Module for creating, listing and deleting collections and aliases.

In Typesense, a Collection is a group of related Documents that is roughly equivalent to a table in a relational database. When we create a collection, we give it a name and describe the fields that will be indexed when a document is added to the collection.

Link to this section Summary

Functions

Create collection from map or from an ecto schema module.

Deletes a collection using collection name.

Deletes a collection alias. The collection itself is not affected by this action.

Get a specific collection using collection name.

Get a specific collection alias.

List all aliases and the corresponding collections that they map to.

Lists all collections.

Make changes in a collection's fields: adding, removing or updating an existing field(s). Key name is drop to indicate which field is removed (example described below). Only fields can only be updated at the moment.

Link to this section Types

@type response() ::
  {:ok,
   %ExTypesense.Collection{
     created_at: term(),
     default_sorting_field: term(),
     fields: term(),
     name: term(),
     num_documents: term(),
     symbols_to_index: term(),
     token_separators: term()
   }}
  | {:ok, map()}
  | {:error, map()}
@type t() :: %ExTypesense.Collection{
  created_at: String.t(),
  default_sorting_field: String.t(),
  fields: ExTypesense.Collection.Schema.t(),
  name: String.t(),
  num_documents: integer(),
  symbols_to_index: list(),
  token_separators: list()
}

Link to this section Functions

Link to this function

create_collection(schema)

View Source (since 0.1.0)
@spec create_collection(
  schema ::
    map()
    | %ExTypesense.Collection{
        created_at: term(),
        default_sorting_field: term(),
        fields: term(),
        name: term(),
        num_documents: term(),
        symbols_to_index: term(),
        token_separators: term()
      }
) :: response()

Create collection from map or from an ecto schema module.

examples

Examples

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

iex> ExTypesense.Parser.struct_to_map(AppModule, "title") |> ExTypesense.create_collection()
{:ok,
  %ExTypesense.Collection{
    "created_at" => 1234567890,
    "default_sorting_field" => "num_employees",
    "fields" => [...],
    "name" => "companies",
    "num_documents" => 0,
    "symbols_to_index" => [],
    "token_separators" => []
  }
}
Link to this function

delete_collection(collection_name)

View Source (since 0.1.0)
@spec delete_collection(String.t()) :: response()

Deletes a collection using collection name.

Link to this function

delete_collection_alias(alias_name)

View Source (since 0.1.0)
@spec delete_collection_alias(String.t()) :: response()

Deletes a collection alias. The collection itself is not affected by this action.

Link to this function

get_collection(collection_name)

View Source (since 0.1.0)
@spec get_collection(String.t()) :: response()

Get a specific collection using collection name.

Link to this function

get_collection_alias(alias_name)

View Source (since 0.1.0)
@spec get_collection_alias(String.t()) :: response()

Get a specific collection alias.

Link to this function

list_collection_aliases()

View Source (since 0.1.0)
@spec list_collection_aliases() :: response()

List all aliases and the corresponding collections that they map to.

Link to this function

list_collections()

View Source (since 0.1.0)
@spec list_collections() :: response()

Lists all collections.

Link to this function

map_to_struct(collection)

View Source
@spec map_to_struct(map()) :: %ExTypesense.Collection{
  created_at: term(),
  default_sorting_field: term(),
  fields: term(),
  name: term(),
  num_documents: term(),
  symbols_to_index: term(),
  token_separators: term()
}
Link to this function

update_collection(collection_name, collection)

View Source (since 0.1.0)
@spec update_collection(
  String.t(),
  collection ::
    map()
    | %ExTypesense.Collection{
        created_at: term(),
        default_sorting_field: term(),
        fields: term(),
        name: term(),
        num_documents: term(),
        symbols_to_index: term(),
        token_separators: term()
      }
) :: response()

Make changes in a collection's fields: adding, removing or updating an existing field(s). Key name is drop to indicate which field is removed (example described below). Only fields can only be updated at the moment.

Note: Typesense supports updating all fields except the id field (since it's a special field within Typesense).

examples

Examples

 new_schema =
  %{
   fields: [
     %{name: "num_employees", drop: true},
     %{name: "company_category", type: "string"},
   ],
  }

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

upsert_collection_alias(alias_name, collection_name)

View Source (since 0.1.0)
@spec upsert_collection_alias(String.t(), String.t()) :: response()

Upserts a collection alias.