View Source ExTypesense.Document (ExTypesense v0.6.0)
Module for CRUD operations for documents. Refer to this doc guide.
Summary
Functions
Indexes a single document using struct or map. When using struct, the pk maps to document's id as string.
Deletes all documents in a collection.
Deletes a document by id or struct.
Deletes documents in a collection by query.
Get a document from a collection.
Indexes multiple documents via maps.
Updates a single document using struct or map.
Updates multiple documents via maps.
Upserts a single document using struct or map.
Upserts multiple documents via maps. Same with update_multiple_documents/1
with some
difference: creates one if not existed, otherwise updates it.
Types
Functions
@spec create_document( ExTypesense.Connection.t(), struct() | map() | [struct()] | [map()] ) :: response()
Indexes a single document using struct or map. When using struct, the pk maps to document's id as string.
Note: when using maps as documents, you should pass a key named "collection_name".
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "34",
...> collection_name: "posts",
...> posts_id: 34,
...> title: "the quick brown fox",
...> description: "jumps over the lazy dog"
...> }
iex> ExTypesense.create_document(post)
{:ok,
%{
"id" => "34",
"collection_name" => "posts",
"posts_id" => 34,
"title" => "the quick brown fox",
"description" => "jumps over the lazy dog"
}
}
delete_all_documents(conn \\ Connection.new(), collection_name)
View Source (since 0.5.0)@spec delete_all_documents(ExTypesense.Connection.t(), module() | String.t()) :: response()
Deletes all documents in a collection.
On using this function
As of this writing (v0.5.0), there's no built-in way of deleting all documents via Typesense docs. This function uses
delete_by_query
under the hood.
delete_document(conn \\ Connection.new(), struct_or_tuple)
View Source (since 0.3.0)@spec delete_document(ExTypesense.Connection.t(), struct() | {String.t(), integer()}) :: response()
Deletes a document by id or struct.
Deleting a document by id
If you are deleting by id, pass it as a tuple (
{"collection_name", 23}
)
Examples
iex> ExTypesense.create_collection(Post)
iex> post = Post |> limit(1) |> Repo.one()
iex> ExTypesense.create_collection(post)
iex> ExTypesense.delete_document(post)
{:ok,
%{
"id" => "1",
"posts_id" => 1,
"title" => "our first post",
"collection_name" => "posts"
}
}
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
...> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "12",
...> collection_name: "posts",
...> posts_id: 22,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> ExTypesense.delete_document({"posts", 12})
{:ok,
%{
"id" => "12",
"posts_id" => 22,
"title" => "the quick brown fox",
"collection_name" => "posts"
}
}
delete_documents_by_query(conn \\ Connection.new(), collection_name, query)
View Source (since 0.5.0)@spec delete_documents_by_query( ExTypesense.Connection.t(), module() | String.t(), %{filter_by: String.t(), batch_size: integer() | nil} ) :: response()
Deletes documents in a collection by query.
Filter and batch size
To delete all documents in a collection, you can use a filter that matches all documents in your collection. For eg, if you have an int32 field called popularity in your documents, you can use
filter_by: "popularity:>0"
to delete all documents. Or if you have a bool field calledin_stock
in your documents, you can usefilter_by: "in_stock:[true,false]"
to delete all documents.Use the
batch_size
to control the number of documents that should deleted at a time. A larger value will speed up deletions, but will impact performance of other operations running on the server.Filter parameters can be found here: https://typesense.org/docs/latest/api/search.html#filter-parameters
Examples
iex> query = %{
...> filter_by: "num_employees:>100",
...> batch_size: 100
...> }
iex> ExTypesense.delete_documents_by_query(Employee, query)
{:ok, %{}}
get_document(conn \\ Connection.new(), collection_name, document_id)
View Source (since 0.1.0)@spec get_document(ExTypesense.Connection.t(), module() | String.t(), integer()) :: response()
Get a document from a collection.
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
...> ExTypesense.create_collection(schema)
...> post = %{
...> id: "444",
...> collection_name: "posts",
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> ExTypesense.get_document("posts", 444)
{:ok,
%{
"id" => "444",
"collection_name" => "posts",
"title" => "the quick brown fox",
}
}
index_multiple_documents(conn \\ Connection.new(), list_of_structs)
View Source (since 0.1.0)@spec index_multiple_documents(ExTypesense.Connection.t(), [struct()] | map()) :: response()
Indexes multiple documents via maps.
Note: when using maps as documents, you should pass a key named collection_name
and with the lists of documents named documents
(example shown below).
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
...> ExTypesense.create_collection(schema)
...> posts = %{
...> collection_name: "posts",
...> documents: [
...> %{title: "the quick brown fox"},
...> %{title: "jumps over the lazy dog"}
...> ]
...> }
iex> ExTypesense.index_multiple_documents(posts)
{:ok, [%{"success" => true}, %{"success" => true}]}
@spec update_document(ExTypesense.Connection.t(), struct() | map()) :: response()
Updates a single document using struct or map.
Note: when using maps as documents, you should pass a key named "collection_name".
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "94",
...> collection_name: "posts",
...> posts_id: 94,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> updated_post =
...> %{
...> id: "94",
...> collection_name: "posts",
...> posts_id: 94,
...> title: "test"
...> }
iex> ExTypesense.update_document(updated_post)
{:ok,
%{
"id" => "94",
"collection_name" => "posts",
"posts_id" => 94,
"title" => "sample post"
}
}
update_multiple_documents(conn \\ Connection.new(), list_of_structs)
View Source (since 0.3.0)@spec update_multiple_documents(ExTypesense.Connection.t(), [struct()] | map()) :: response()
Updates multiple documents via maps.
Note: when using maps as documents, you should pass a key named collection_name
and with the lists of documents named documents
(example shown below). Also add the id
for each documents.
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> posts = %{
...> collection_name: "posts",
...> documents: [
...> %{id: "5", title: "the quick brown fox"},
...> %{id: "6", title: "jumps over the lazy dog"}
...> ]
...> }
iex> {:ok, _} = ExTypesense.index_multiple_documents(posts)
iex> updated_posts = %{
...> collection_name: "posts",
...> documents: [
...> %{id: "5", title: "the quick"},
...> %{id: "6", title: "jumps over"}
...> ]
...> }
iex> ExTypesense.update_multiple_documents(updated_posts)
{:ok, [%{"success" => true}, %{"success" => true}]}
@spec upsert_document(ExTypesense.Connection.t(), map() | struct()) :: response()
Upserts a single document using struct or map.
Note: when using maps as documents, you should pass a key named "collection_name".
upsert_multiple_documents(conn \\ Connection.new(), map)
View Source (since 0.3.0)@spec upsert_multiple_documents(ExTypesense.Connection.t(), map()) :: response()
Upserts multiple documents via maps. Same with update_multiple_documents/1
with some
difference: creates one if not existed, otherwise updates it.
Note: when using maps as documents, you should pass a key named collection_name
and with the lists of documents named documents
(example shown below). When id
is added,
it will update, otherwise creates a new document.
for each documents.
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> posts = %{
...> collection_name: "posts",
...> documents: [
...> %{id: "0", title: "the quick"},
...> %{id: "1", title: "jumps over"}
...> ]
...> }
iex> ExTypesense.upsert_multiple_documents(posts)
{:ok, [%{"success" => true}, %{"success" => true}]}