View Source ExTypesense.Document (ExTypesense v0.4.1)

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 a document by struct.

Deletes a document by id.

Deletes a document by collection name and document id.

Updates a single document using struct or map.

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

Link to this type

response()

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

Functions

Link to this function

create_document(conn \\ Connection.new(), struct)

View Source (since 0.3.0)
@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",
...>    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.3.0)
This function is deprecated. use delete_document_by_struct/2.
@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)
This function is deprecated. use delete_document_by_id/3.
@spec delete_document(String.t(), integer()) :: response()

Deletes a document by id.

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

delete_document_by_id(conn \\ Connection.new(), collection_name, document_id)

View Source (since 0.4.0)
@spec delete_document_by_id(ExTypesense.Connection.t(), String.t(), integer()) ::
  response()

Deletes a document by collection name and document id.

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

delete_document_by_struct(conn \\ Connection.new(), struct)

View Source (since 0.4.0)
@spec delete_document_by_struct(
  ExTypesense.Connection.t(),
  struct()
) :: response()

Deletes a document by struct.

Link to this function

get_document(conn \\ Connection.new(), module_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",
  }
}
Link to this function

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

update_document(conn \\ Connection.new(), struct)

View Source (since 0.3.0)
@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",
...>    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(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}]}
Link to this function

upsert_document(conn \\ Connection.new(), struct)

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

Link to this function

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