View Source ExTypesense.Document (ExTypesense v0.3.5)
Module for CRUD operations for documents. Refer to this doc guide.
Link to this section Summary
Functions
Indexes a single document using struct or map. When using struct, the pk maps to document's id as string.
Deletes a document by struct.
Deletes a document by id.
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.
Link to this section Types
Link to this section Functions
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
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "34",
...> collection_name: "posts",
...> post_id: 34,
...> title: "the quick brown fox",
...> description: "jumps over the lazy dog"
...> }
iex> ExTypesense.create_document(post)
{:ok,
%{
"id" => "34",
"collection_name" => "posts",
"post_id" => 34,
"title" => "the quick brown fox",
"description" => "jumps over the lazy dog"
}
}
Deletes a document by struct.
Deletes a document by id.
examples
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
...> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "12",
...> collection_name: "posts",
...> post_id: 22,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> ExTypesense.delete_document("posts", 12)
{:ok,
%{
"id" => "12",
"post_id" => 22,
"title" => "the quick brown fox",
"collection_name" => "posts"
}
}
Get a document from a collection.
examples
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",
}
}
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
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}]}
Updates a single document using struct or map.
Note: when using maps as documents, you should pass a key named "collection_name".
examples
Examples
iex> schema = %{
...> name: "posts",
...> fields: [
...> %{name: "title", type: "string"}
...> ],
...> }
iex> ExTypesense.create_collection(schema)
iex> post =
...> %{
...> id: "94",
...> collection_name: "posts",
...> post_id: 94,
...> title: "the quick brown fox"
...> }
iex> ExTypesense.create_document(post)
iex> updated_post =
...> %{
...> id: "94",
...> collection_name: "posts",
...> post_id: 94,
...> title: "test"
...> }
iex> ExTypesense.update_document(updated_post)
{:ok,
%{
"id" => "94",
"collection_name" => "posts",
"post_id" => 94,
"title" => "test"
}
}
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
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}]}
Upserts a single document using struct or map.
Note: when using maps as documents, you should pass a key named "collection_name".
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
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}]}