TantivyEx.IndexWriter (TantivyEx v0.4.1)

View Source

IndexWriter for adding documents to a TantivyEx index.

The IndexWriter is responsible for adding documents to an index and committing changes to make them searchable.

Summary

Functions

Adds a document to the index.

Commits all pending changes to the index.

Deletes all documents in the index.

Deletes all documents matching the given query.

Creates a new IndexWriter for the given index.

Rolls back any pending changes and cancels the current operation.

Types

t()

@type t() :: reference()

Functions

add_document(writer, document)

@spec add_document(t(), map()) :: :ok | {:error, String.t()}

Adds a document to the index.

The document should be a map where keys are field names and values are the field values. The field names should match those defined in the schema.

Parameters

  • writer: The IndexWriter
  • document: A map representing the document to add

Examples

iex> # Assuming writer is already created
iex> document = %{"title" => "Hello World", "body" => "This is a test document"}
iex> :ok = TantivyEx.IndexWriter.add_document(writer, document)
:ok

commit(writer)

@spec commit(t()) :: :ok | {:error, String.t()}

Commits all pending changes to the index.

After calling commit, all added documents become searchable. This operation flushes the current segment to disk.

Parameters

  • writer: The IndexWriter

Examples

iex> :ok = TantivyEx.IndexWriter.commit(writer)
:ok

delete_all_documents(writer)

@spec delete_all_documents(t()) :: :ok | {:error, String.t()}

Deletes all documents in the index.

This operation marks all documents for deletion but does not make the deletions visible until the writer is committed.

Parameters

  • writer: The IndexWriter

Examples

iex> :ok = TantivyEx.IndexWriter.delete_all_documents(writer)
:ok
iex> :ok = TantivyEx.IndexWriter.commit(writer)
:ok

delete_documents(writer, query)

@spec delete_documents(t(), TantivyEx.Query.t()) :: :ok | {:error, String.t()}

Deletes all documents matching the given query.

This operation marks documents for deletion but does not make the deletions visible until the writer is committed.

Parameters

  • writer: The IndexWriter
  • query: A TantivyEx.Query to match documents to delete

Examples

iex> query = TantivyEx.Query.term(schema, "status", "inactive")
iex> :ok = TantivyEx.IndexWriter.delete_documents(writer, query)
:ok

new(index, memory_budget \\ 50_000_000)

@spec new(TantivyEx.Index.t(), pos_integer()) :: {:ok, t()} | {:error, String.t()}

Creates a new IndexWriter for the given index.

Parameters

  • index: The index to write to
  • memory_budget: Memory budget in bytes (default: 50MB)

Examples

iex> schema = TantivyEx.Schema.new()
iex> schema = TantivyEx.Schema.add_text_field(schema, "title", :text_stored)
iex> {:ok, index} = TantivyEx.Index.create_in_ram(schema)
iex> {:ok, writer} = TantivyEx.IndexWriter.new(index)
iex> is_reference(writer)
true

rollback(writer)

@spec rollback(t()) :: :ok | {:error, String.t()}

Rolls back any pending changes and cancels the current operation.

This should be called when errors occur during a batch index operation to avoid partial updates.

Parameters

  • writer: The IndexWriter

Examples

iex> :ok = TantivyEx.IndexWriter.rollback(writer)
:ok