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.

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 type

response()

View Source (since 0.1.0)
@type response() :: :ok | {:ok, map()} | {:error, map()}

Link to this section Functions

Link to this function

create_document(struct)

View Source (since 0.3.0)
@spec create_document(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

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"
  }
}
Link to this function

delete_document(struct)

View Source (since 0.1.0)
@spec delete_document(struct()) :: response()

Deletes a document by struct.

Link to this function

delete_document(collection_name, document_id)

View Source (since 0.3.0)
@spec delete_document(String.t(), integer()) :: response()

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"
  }
}
Link to this function

get_document(module_name, document_id)

View Source (since 0.1.0)
@spec get_document(module() | String.t(), integer()) :: response()

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",
  }
}
Link to this function

index_multiple_documents(list_of_structs)

View Source (since 0.1.0)
@spec index_multiple_documents([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

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}]}
Link to this function

update_document(struct)

View Source (since 0.3.0)
@spec update_document(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

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"
  }
}
Link to this function

update_multiple_documents(list_of_structs)

View Source (since 0.3.0)
@spec update_multiple_documents([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

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}]}
Link to this function

upsert_document(struct)

View Source (since 0.3.0)
@spec upsert_document(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".

Link to this function

upsert_multiple_documents(map)

View Source (since 0.3.0)
@spec upsert_multiple_documents(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

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